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
手势效果图
代码示例
// 点击 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() }
// 滑动 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() }
// 拖动 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 } }
// 缩放 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 } } }
// 旋转 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 } }
// 长按 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() } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读希望能帮助到大家,谢谢大家对本站的支持!
赞 (0)