1>UI時間分類
swift
(1)touch:各類手勢ide
(2)motion:例如到傳感器,例如搖晃ui
(3)Remote control:利用外部設備,例如插入耳機this
2>Touch事件階段spa
touch begin --> touch move --> touch end --> touch cancelcode
多指手勢流程:orm
3>Recognize UIView ViewController的關係
事件
override func viewDidLoad() { super.viewDidLoad() let rect=CGRect(x:80,y:200,width:200,height:200); var view1 = UIView(frame: rect) view1.backgroundColor = UIColor.redColor() self.view.addSubview(view1) //1,創建手勢識別器 var gesture = UITapGestureRecognizer(target: self, action: "viewAction:") #warning 若是想要識別器可以識別多種觸控,例如1點和2點,那就要創建2個UITapGestureRecognizer gesture.numberOfTapsRequired = 2 //點擊次數 // gesture.numberOfTouchesRequired = 2 //多點觸摸 //2,關聯識別器到視圖 view1.addGestureRecognizer(gesture) } //3,手勢引用的動做 func viewAction(sender:UITapGestureRecognizer){ //得到點擊處的位置 var point:CGPoint = sender.locationInView(self.view) println("\(point)") println("clicked") }
override func viewDidLoad() { super.viewDidLoad() //1,創建手勢識別器 var gesture = UIPinchGestureRecognizer(target: self, action: "viewAction:") //2,關聯識別器到視圖 view1.addGestureRecognizer(gesture) } //3,手勢引用的動做 func viewAction(sender:UIPinchGestureRecognizer){ var _height = view1.bounds.height var _width = view1.bounds.width view1.bounds.size = CGSize(width: _width * sender.scale, height: _height * sender.scale) }
override func viewDidLoad() { super.viewDidLoad() //1,創建手勢識別器 var gesture = UIRotationGestureRecognizer(target: self, action: "viewAction:") //2,關聯識別器到視圖 view1.addGestureRecognizer(gesture) } //3,手勢引用的動做 func viewAction(sender:UIRotationGestureRecognizer){ view1.transform = CGAffineTransformMakeRotation(sender.rotation) }
var offsetX:CGFloat = 0.0 override func viewDidLoad() { super.viewDidLoad() //1,創建手勢識別器 var gesture = UISwipeGestureRecognizer(target: self, action: "viewAction:") //設置多指 gesture.numberOfTouchesRequired = 2; //2,關聯識別器到視圖 view1.UISwipeGestureRecognizer(gesture) } //3,手勢引用的動做 func viewAction(sender:UISwipeGestureRecognizer){ offsetX += 20.0 //方向屬性 if(sender.direction == UISwipeGestureRecognizerDirection.Right){ view1.transform = CGAffineTransformMakeTranslation(offsetX, 0) } }
override func viewDidLoad() { super.viewDidLoad() var gesture = UIPanGestureRecognizer(target: self, action: "view1Tap:") //支持的多指範圍 gesture.minimumNumberOfTouches = 1; gesture.maximumNumberOfTouches = 2; view1.addGestureRecognizer(gesture) } //3,手勢引用的動做 func viewAction(sender:UIPanGestureRecognizer){ //相對於view1視圖偏移的位置 var _transX = sender.translationInView(view1).x var _transY = sender.translationInView(view1).y view1.transform = CGAffineTransformMakeTranslation(_transX, _transY) }
override func viewDidLoad() { super.viewDidLoad() var gesture = UILongPressGestureRecognizer(target: self, action: "view1Tap:") //須要的點數和點擊次數 // gesture.numberOfTouchesRequired // gesture.numberOfTapsRequired //最短長按時間 gesture.minimumPressDuration = 1 //運行移動的點數,在這個範圍內不發生動做。默認是10 gesture.allowableMovement = 10 view1.addGestureRecognizer(gesture) } //3,手勢引用的動做 func viewAction(sender:UILongPressGestureRecognizer){ UIAlertView(title: "longpress", message: "你長按了", delegate: self, cancelButtonTitle: "肯定").show() } //2.獲取手勢的view的座標點 CGPoint location = [recognizer locationInView:recognizer.view]; //3.判斷點是否在rect範圍 BOOL isyes = CGRectContainsPoint(btn.frame, location);
class UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer { var edges: UIRectEdge //< The edges on which this gesture recognizes, relative to the current interface orientation } 是pan的子類,從邊緣出來
// // UICustomGestureRecognizer.swift // ttt // // Created by ling on 15/8/26. // Copyright (c) 2015年 ling. All rights reserved. // import UIKit import UIKit.UIGestureRecognizerSubclass class UICustomGestureRecognizer: UIGestureRecognizer { var leftTop = false var rightBttom = false //初始化 override init(target: AnyObject, action: Selector) { super.init(target: target, action: action) } override func touchesBegan(touches: Set<NSObject>!, withEvent event: UIEvent!) { return } override func touchesMoved(touches: Set<NSObject>!, withEvent event: UIEvent!) { var myTouch = (touches as NSSet).anyObject() as! UITouch var 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){ rightBttom = true } if(leftTop && rightBttom){ self.state = UIGestureRecognizerState.Ended } println("\(myLocation)") } override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) { self.reset() } override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { return } } // // UICustomGestureRecognizer.swift // ttt // // Created by ling on 15/8/26. // Copyright (c) 2015年 ling. All rights reserved. // import UIKit import UIKit.UIGestureRecognizerSubclass class UICustomGestureRecognizer: UIGestureRecognizer { var leftTop = false var rightBttom = false //初始化 override init(target: AnyObject, action: Selector) { super.init(target: target, action: action) } override func touchesBegan(touches: Set<NSObject>!, withEvent event: UIEvent!) { return } override func touchesMoved(touches: Set<NSObject>!, withEvent event: UIEvent!) { var myTouch = (touches as NSSet).anyObject() as! UITouch var 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){ rightBttom = true } if(leftTop && rightBttom){ self.state = UIGestureRecognizerState.Ended } println("\(myLocation)") } override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) { self.reset() } override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { return } }