您好,登錄后才能下訂單哦!
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() } }
如有疑問請留言或者到本站社區交流討論,感謝閱讀希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。