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

时间:2021-05-02

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

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

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

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

* began
* ended
* cancelled
* failed
* possible

手势效果图

代码示例

? 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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章