手势事件
UITapGestureRecognizer(点击手势)
UIPanGestureRecognizer(拖动手势)
UIPinchGestureRecognizer(缩放手势)
UISwipeGestureRecognizer(擦碰手势)
UIRotationGestureRecognizer(旋转手势)
UILongPressGestureRecognizer(长按手势)
添加和移除手势:
uiview.addGestureRecognizer(gesture)uiview.removeGestureRecognizer(gesture)
下面代码简单演示各个手势用法:
@IBOutlet weak var imgView: UIImageView!var lastScaleFactor: CGFloat = 1var netRotation: CGFloat = 0var netTranslation: CGPoint = CGPointMake(0,0)override func viewDidLoad() { super.viewDidLoad() // 点击手势 let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:") tapGesture.numberOfTapsRequired = 2 // 点击次数 self.view.addGestureRecognizer(tapGesture) // 捏手势 let pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:") self.view.addGestureRecognizer(pinchGesture) // 旋转手势 let rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:") self.view.addGestureRecognizer(rotateGesture) // 滑动手势: 默认为右滑 let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:") swipeGesture.direction = .Down //.Left .Down .Up .Right self.view.addGestureRecognizer(swipeGesture) // // 拖动手势: 与滑动手势冲突,二者选其一// let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")// self.view.addGestureRecognizer(panGesture) // 长按手势 let longpressGestrue = UILongPressGestureRecognizer(target: self, action: "handleLongPressGesture:") longpressGestrue.minimumPressDuration = 1 longpressGestrue.numberOfTouchesRequired = 1 longpressGestrue.allowableMovement = 15 self.view.addGestureRecognizer(longpressGestrue)}override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()}// MARK: gesture methodsfunc handleTapGesture(sender: UITapGestureRecognizer) { print("handleTapGesture") if imgView.contentMode == UIViewContentMode.ScaleAspectFit { imgView.contentMode = UIViewContentMode.Center } else { imgView.contentMode = UIViewContentMode.ScaleAspectFit }}func handlePinchGesture(sender: UIPinchGestureRecognizer) { print("handlePinchGesture") let factor = sender.scale print("factor", factor) if factor > 1 { // 放大 imgView.transform = CGAffineTransformMakeScale(lastScaleFactor + factor - 1, lastScaleFactor + factor - 1) } else { // 缩小 imgView.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor) } if sender.state == UIGestureRecognizerState.Ended { if factor > 1 { lastScaleFactor = lastScaleFactor + factor - 1 } else { lastScaleFactor = lastScaleFactor * factor } }}func handleRotateGesture(sender: UIRotationGestureRecognizer) { print("handleRotateGesture") let rotation = sender.rotation print("rotation", rotation) imgView.transform = CGAffineTransformMakeRotation(rotation + netRotation) if sender.state == UIGestureRecognizerState.Ended { netRotation += rotation }}func handleSwipeGesture(sender: UISwipeGestureRecognizer) { print("handleSwipeGesture")}func handlePanGesture(sender: UIPanGestureRecognizer) { print("UIPanGestureRecognizer") let translation = sender.translationInView(imgView) imgView.transform = CGAffineTransformMakeTranslation(netTranslation.x + translation.x, translation.y + netTranslation.y) if sender.state == UIGestureRecognizerState.Ended { netTranslation.x += translation.x netTranslation.y += translation.y }}func handleLongPressGesture(sender: UILongPressGestureRecognizer) { print("handleLongPressGesture") if sender.state == UIGestureRecognizerState.Began { print("----------") }}
状态变化:
触控事件
func touchesBegan(touches: NSSet, withEvent event: UIEvent)
通知调用者当有一个或者多个手指触摸到了视图或者窗口时触发此方法func touchesMoved(touches: NSSet, withEvent event: UIEvent)
告诉接收者一个或者多个手指在视图或者窗口上触发移动事件func touchesEnded(touches: NSSet, withEvent event: UIEvent)
当一个触摸事件结束时发出的UITouch实例对象func touchesCancelled(touches: NSSet, withEvent event: UIEvent)
通知接收者当系统发出取消事件的时候(如低内存消耗的告警框)默认是不开启多点触控的,如开启:view.multipleTouchEnabled = true
override func touchesBegan(touches: Set, withEvent event: UIEvent?) { for touch: AnyObject in touches { let tap: UITouch = touch as! UITouch print("tapCount", tap.tapCount) print("event begin!") }}override func touchesMoved(touches: Set , withEvent event: UIEvent?) { for touch: AnyObject in touches { let tap: UITouch = touch as! UITouch print(tap.locationInView(self.view)) }}override func touchesEnded(touches: Set , withEvent event: UIEvent?) { print("event end!")}override func touchesCancelled(touches: Set ?, withEvent event: UIEvent?) { print("event canceled!")}
参考: