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中手势的实例详解

代码示例

// 点击 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 } } }

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。