gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章IOS 开发之swift中手势的实例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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 开发之swift中手势的实例详解的文章就讲到这里了,如果你想了解更多关于IOS 开发之swift中手势的实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com