服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS 开发之swift中手势的实例详解

IOS 开发之swift中手势的实例详解

2021-03-28 18:35番薯大佬 IOS

这篇文章主要介绍了IOS 开发之swift中手势的实例详解的相关资料,希望通过本文大家能掌握IOS手势的使用方法,需要的朋友可以参考下

ios 开发之swift手势的实例详解

手势操作主要包括如下几类

 

手势 属性 说明
点击 uitapgesturerecognizer numberoftapsrequired:点击的次数;numberoftouchesrequired:点击时有手指数量 设置属性 numberoftapsrequired 可以实现单击,或双击的效果
滑动 uiswipegesturerecognizer direction:滑动方向 direction 滑动方向分为上up、下down、左left、右right
拖动 uipangesturerecognizer   在拖动过程中,通过方法 translationinview 获取拖动时的位移
长按 uilongpressgesturerecognizer minimumpressduration:长按最少时间  
旋转 uirotationgesturerecognizer    
缩放 uipinchgesturerecognizer    

 

注意:手势效果在实施过程中,存在几种状态:

* began
* ended
* cancelled
* failed
* possible

手势效果图

IOS 开发之swift中手势的实例详解

代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 点击
let label = uilabel(frame: cgrectmake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addsubview(label)
label.backgroundcolor = uicolor.lightgraycolor()
label.text = "手势操作-单指单击手势";
label.adjustsfontsizetofitwidth = true
label.textalignment = .center;
// 添加手势
let taprecognizer = uitapgesturerecognizer(target: self, action: selector("tapclick:"))
taprecognizer.numberoftapsrequired = 1
taprecognizer.numberoftouchesrequired = 1
label.userinteractionenabled = true
label.addgesturerecognizer(taprecognizer)
 
// 手势响应方法
func tapclick(recognizer:uitapgesturerecognizer)
{
    let label:uilabel = recognizer.view as! uilabel
    label.textcolor = uicolor.redcolor()
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 滑动
let label = uilabel(frame: cgrectmake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addsubview(label)
label.backgroundcolor = uicolor.lightgraycolor()
label.text = "手势操作-左滑手势";
label.adjustsfontsizetofitwidth = true
label.textalignment = .center;
// 添加手势
let swipeleftrecognizer = uiswipegesturerecognizer(target: self, action: selector("swipeleftclick:"))
swipeleftrecognizer.direction = .left
swipeleftrecognizer.numberoftouchesrequired = 1
label.userinteractionenabled = true
label.addgesturerecognizer(swipeleftrecognizer)
 
// 手势响应方法
func swipeleftclick(recognizer:uiswipegesturerecognizer)
{
    let label = recognizer.view
    label!.backgroundcolor = uicolor.orangecolor()
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 拖动
let label = uilabel(frame: cgrectmake(10.0, 10.0, 100.0, 100.0))
self.view.addsubview(label)
label.backgroundcolor = uicolor.lightgraycolor()
label.text = "手势操作-拖动手势";
label.adjustsfontsizetofitwidth = true
label.textalignment = .center;
// 添加手势
let panrecognizer = uipangesturerecognizer(target: self, action: selector("panclick:"))
label.userinteractionenabled = true
label.addgesturerecognizer(panrecognizer)
 
var pointvalue:cgpoint! = cgpointzero // 移动
// 手势响应方法
func panclick(recognizer:uipangesturerecognizer)
{
    let label:uilabel = recognizer.view as! uilabel
 
    let point = recognizer.translationinview(label)
    print("pan point = \(point)")
 
    // 移动
    label.transform = cgaffinetransformmaketranslation(point.x + self.pointvalue.x, point.y + self.pointvalue.y)
 
    if recognizer.state == .began
    {
      label.backgroundcolor = uicolor.yellowcolor()
      self.view.bringsubviewtofront(label)
    }
    else if recognizer.state == .ended
    {
      label.backgroundcolor = uicolor.lightgraycolor()
 
      self.pointvalue.x += point.x
      self.pointvalue.y += point.y
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 缩放
let label = uilabel(frame: cgrectmake(10.0, 10.0, 200.0, 200.0))
self.view.addsubview(label)
label.backgroundcolor = uicolor.lightgraycolor()
label.text = "手势操作-捏合手势";
label.adjustsfontsizetofitwidth = true
label.textalignment = .center;
// 添加手势
let pinchrecognizer = uipinchgesturerecognizer(target: self, action: selector("pinchclick:"))
label.userinteractionenabled = true
label.addgesturerecognizer(pinchrecognizer)
 
// 手势响应方法
var scalevalue:cgfloat! = 1.0 // 缩放
func pinchclick(recognizer:uipinchgesturerecognizer)
{
    let label:uilabel = recognizer.view as! uilabel
 
    let scale = recognizer.scale;
    if scale > 1.0
    {
      // 放大
      label.transform = cgaffinetransformmakescale(self.scalevalue + scale - 1.0, self.scalevalue + scale - 1.0)
    }
    else
    {
      // 缩小
      label.transform = cgaffinetransformmakescale(self.scalevalue * scale, self.scalevalue * scale)
    }
 
    if recognizer.state == .began
    {
      label.backgroundcolor = uicolor.greencolor()
      self.view.bringsubviewtofront(label)
    }
    else if recognizer.state == .ended
    {
      label.backgroundcolor = uicolor.lightgraycolor()
 
      if scale > 1.0
      {
        self.scalevalue = self.scalevalue + scale - 1.0;
      }
      else
      {
        self.scalevalue = self.scalevalue * scale
      }
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 旋转
let label = uilabel(frame: cgrectmake(10.0, 10.0, 200.0, 200.0))
self.view.addsubview(label)
label.backgroundcolor = uicolor.lightgraycolor()
label.text = "手势操作-旋转手势";
label.adjustsfontsizetofitwidth = true
label.textalignment = .center;
// 添加手势
let rotationrecognizer = uirotationgesturerecognizer(target: self, action: selector("ratotionclick:"))
label.userinteractionenabled = true
label.addgesturerecognizer(rotationrecognizer)
 
var rotationvalue:cgfloat! = 1.0 // 旋转
// 手势响应方法
func ratotionclick(recognizer:uirotationgesturerecognizer)
{
    let label:uilabel = recognizer.view as! uilabel
 
    let rotation = recognizer.rotation
    label.transform = cgaffinetransformmakerotation(rotation + self.rotationvalue)
 
    if recognizer.state == .began
    {
      label.backgroundcolor = uicolor.greencolor()
      self.view.bringsubviewtofront(label)
    }
    else if recognizer.state == .ended
    {
      label.backgroundcolor = uicolor.lightgraycolor()
 
      self.rotationvalue = self.rotationvalue + rotation
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 长按
let label = uilabel(frame: cgrectmake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addsubview(label)
label.backgroundcolor = uicolor.lightgraycolor()
label.text = "手势操作-长按手势";
label.adjustsfontsizetofitwidth = true
label.textalignment = .center;
// 添加手势
let pressrecognizer = uilongpressgesturerecognizer(target: self, action: selector("pressclick:"))
    pressrecognizer.minimumpressduration = 3.0
    label.userinteractionenabled = true
    label.addgesturerecognizer(pressrecognizer)
 
// 手势响应方法
func pressclick(recognizer:uilongpressgesturerecognizer)
{
    let label:uilabel = recognizer.view as! uilabel
 
    if recognizer.state == .began
    {
      let alertview = uialertview(title: nil, message: "长按响应", delegate: nil, cancelbuttontitle: "知道了")
      alertview.show()
 
      label.backgroundcolor = uicolor.orangecolor()
      self.view.bringsubviewtofront(label)
    }
    else if recognizer.state == .ended
    {
      label.backgroundcolor = uicolor.lightgraycolor()
    }
}

 如有疑问请留言或者到本站社区交流讨论,感谢阅读希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/potato512/article/details/75107107

延伸 · 阅读

精彩推荐
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03