Swift-いくつかの一般的なジェスチャー用のUIGestureRecognizer



Swift Uigesturerecognizer



ジェスチャ基本クラス:UIGestureRecognizer(ジェスチャレコグナイザー)

ジェスチャ認識オブジェクトは、タッチシーケンスやその他の入力の論理的な決定によく使用され、認識を操作します。



特定のサブクラスには次の7つがあります

1、 UITapGestureRecognizer タップジェスチャ認識



二、 UIPinchGestureRecognizer ピンチジェスチャ認識オブジェクト

3、 UIRotationGestureRecognizer 回転ジェスチャ認識

4、 UISwipeGestureRecognizer スワイプジェスチャ認識



5、 UIPanGestureRecognizer ドラッグジェスチャ認識

6、 UIScreenEdgePanGestureRecognizer 画面エッジドラッグジェスチャ認識

7 、 UILongPressGestureRecognizer 長押しジェスチャ認識

メソッドで一般的に使用されるプロパティ:

一般的な方法:

1, init (target: Any?, action: Selector?) initialization gesture method 2, addTarget (_ target: Any, action: Selector) gesture in the view binding method 3, removeTarget (_ target: Any?, action: Selector?) gesture in the view unbinding method 4, location (in view: UIView?) -> CGPoint gesture position in the view 5, location (ofTouch touchIndex: Int, in view: UIView?) -> CGPoint detailed gesture position in the view // proxy method in UIGestureRecognizerDelegate 1、gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool Does the gesture recognizer explain the gesture? 2、gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool Whether the gesture recognizer accepts touch objects 3、gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool Whether two gestures are recognized at the same time 4、 gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool Whether to require another gesture recognizer to fail in order to respond Common attributes: 1, state recognizer status 2, isEnabled is available 3, view bound view 4, cancelsTouchesInView cancel gesture recognition in the view 5, delaysTouchesBegan delay gesture start 6, delaysTouchesEnded delay gesture end Several common gestures: 1. Tap gesture: UITapGestureRecognizer Example: /* Common tap gestures * Commonly used for keyboard recycling, event one-time response, etc. */ func TapGestureRecognizer() -> Void { let tap:UITapGestureRecognizer = UITapGestureRecognizer.init() tap.numberOfTapsRequired = 2 // Taps tap.numberOfTouchesRequired = 1 //number of fingers tap.delegate = self tap.addTarget(self, action: #selector(tapAction(action:))) self.view.addGestureRecognizer(tap) } /*How to tap gestures*/ @objc func tapAction(action:UITapGestureRecognizer) -> Void { Print('Double click to print') } /* gesture recognizer proxy method */ // Whether the gesture occurs func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { if gestureRecognizer.state == .possible { Print('Gesage has occurred') return true } else if gestureRecognizer.state == .changed{ Print('Gesture changes') return false } return true } / * Whether the bound object accepts the touch object * / func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { /* yes view response * no view does not respond */ return true } / * Gestures accept the press operation, I do not know what to do * / func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive press: UIPress) -> Bool { return true } /*Do you recognize both gestures at the same time*/ func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return false } /*Do you want another gesture recognizer to fail to respond (question failure request)*/ func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool { return false } 2, drag gestures /* Drag gestures*/ func pan() -> Void { let pan:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(panAction(pan:))) /*Maximum and minimum gesture touch times*/ pan.minimumNumberOfTouches = 1 pan.maximumNumberOfTouches = 3 self.view.addGestureRecognizer(pan) } @objc func panAction(pan:UIPanGestureRecognizer) -> Void { let orgin:CGPoint = pan.translation(in: self.view) print('orgin:(orgin)') } 3, long press gesture /*Long press gesture*/ func long() -> Void { let long:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(longAction(long:))) self.view.addGestureRecognizer(long) } @objc func longAction(long:UILongPressGestureRecognizer) -> Void { Print('Awaken long press') } 4, cleaning gestures /*Sweeping gesture*/ func swip() -> Void { let swip:UISwipeGestureRecognizer = UISwipeGestureRecognizer.init(target: self, action: #selector(swipAction(swip:))) swip.direction = [.right , .left] self.view.addGestureRecognizer(swip) } @objc func swipAction(swip:UISwipeGestureRecognizer) -> Void { Print('left and right cleaning') }

この記事はKsillyのCSDNブログからのものです。全文アドレスをクリックしてください。