Swift之手勢總結


手勢個人筆記,代碼簡單,不過多贅述。

建立手勢只需三步

  • 建立和設置手勢識別器實例,設置target,action,指定一些屬性
  • 附加識別器到視圖
  • 實現action方法

1、首先還是創建一個view

@IBOutlet var view1: UIView!

2、實現手勢綁定

//-----------------------手勢--------------------------- /**點擊手勢*/ func tapGestureDemo() { //建立手勢識別器 let gesture = UITapGestureRecognizer(target: self, action: "viewTap:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } /**縮放手勢*/ func pinchGestureDemo() { //建立手勢識別器 let gesture = UIPinchGestureRecognizer(target: self, action: "viewPinch:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } /**旋轉手勢*/ func rotationGestureDemo() { //建立手勢識別器 let gesture = UIRotationGestureRecognizer(target: self, action: "viewRotation:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } /**滑動手勢*/ func swipeGestureDemo() { //建立手勢識別器 let gesture = UISwipeGestureRecognizer(target: self, action: "viewSwipe:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } /**平移手勢*/ func panGestureDemo() { //建立手勢識別器 let gesture = UIPanGestureRecognizer(target: self, action: "viewPan:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } /**長按手勢*/ func LongPressGestureDemo() { //建立手勢識別器 let gesture = UILongPressGestureRecognizer(target: self, action: "viewLongPress:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } /**自定義手勢*/ func custonGestureDemo() { //建立手勢識別器 let gesture = CustomGestureRecognizer(target: self, action: "viewCustom:") //附加識別器到視圖 self.view1.addGestureRecognizer(gesture) } //-----------------------手勢action事件--------------------------- /**點擊事件*/ @IBAction func viewTap(sender: UITapGestureRecognizer) { print("clicked...") } /**縮放事件*/ func viewPinch(sender: UIPinchGestureRecognizer) { print("pinch...") let _width = view1.bounds.width let _height = view1.bounds.height view1.bounds.size = CGSize(width: _width * sender.scale, height: _height * sender.scale) } /**旋轉事件*/ func viewRotation(sender: UIRotationGestureRecognizer) { view1.transform = CGAffineTransformMakeRotation(sender.rotation) } /**滑動事件*/ var offsetX: CGFloat = 0 func viewSwipe(sender: UISwipeGestureRecognizer) { if sender.direction == UISwipeGestureRecognizerDirection.Right { //每次向右平移20 offsetX += 20 view1.transform = CGAffineTransformMakeTranslation(offsetX, 0) } } /**平移事件*/ func viewPan(sender: UIPanGestureRecognizer) { let _transX = sender.translationInView(view1).x let _transY = sender.translationInView(view1).y view1.transform = CGAffineTransformMakeTranslation(_transX, _transY) } /**長按事件*/ func viewLongPress(sender: UILongPressGestureRecognizer) { print("long pressed....") } func viewCustom(sender: CustomGestureRecognizer) { print("custom gesture...") }

3、最后自定義手勢,很簡單一個例子,可自由擴展

  • 新建一個CustomGestureRecognizer自定義手勢類,繼承自UIGestureRecognizer
  • 切記,引入UIGestureRecognizerSubclass
  • 開始實現類容,下面代碼實現從左上角滑動到右下角實例
import UIKit import UIKit.UIGestureRecognizerSubclass class CustomGestureRecognizer: UIGestureRecognizer { var leftTop = false var rightDown = false override init(target: AnyObject?, action: Selector) { super.init(target: target, action: action) } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) { } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) { let myTouch = touches.first! as UITouch let myLocation = myTouch.locationInView(self.view) if myLocation.x < 10 && myLocation.y < 10 { leftTop = true } if (myLocation.x + 10) > self.view?.bounds.width && (myLocation.y + 10) > self.view?.bounds.height { rightDown = true } if leftTop && rightDown { self.state = UIGestureRecognizerState.Ended } } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) { self.reset() } }



文/瘋狂的米老鼠(簡書作者)
原文鏈接:http://www.jianshu.com/p/6ef6603d035d
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com