How to call gesture tap on UIView programmatically in swift

后端 未结 23 1774
情歌与酒
情歌与酒 2020-11-28 18:50

I have a UIView and and I have added tap gesture to it:

let tap = UITapGestureRecognizer(target: self, action: Selector(\"handleTap:\"))
tap.delegate = self         


        
23条回答
  •  清歌不尽
    2020-11-28 19:13

    Here is the simplest way to add Gestures on View in Swift 5

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            addGestures()
        }
    
        // MARK: Add Gestures to target view
        func addGestures()
        {
            // 1. Single Tap or Touch
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapGetstureDetected))
            tapGesture.numberOfTapsRequired = 1
            view.addGestureRecognizer(tapGesture)
    
            //2. Double Tap
            let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(self.doubleTapGestureDetected))
            doubleTapGesture.numberOfTapsRequired = 2
            view.addGestureRecognizer(doubleTapGesture)
    
            //3. Swipe
            let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeGetstureDetected))
            view.addGestureRecognizer(swipeGesture)
    
            //4. Pinch
            let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchGetstureDetected))
            view.addGestureRecognizer(pinchGesture)
    
            //5. Long Press
            let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGetstureDetected))
            view.addGestureRecognizer(longPressGesture)
    
            //6. Pan
            let panGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.panGestureDetected))
            view.addGestureRecognizer(panGesture)
    
        }
    
        // MARK: Handle Gesture detection
        @objc func swipeGetstureDetected() {
            print("Swipe Gesture detected!!")
        }
    
        @objc func tapGetstureDetected() {
            print("Touch/Tap Gesture detected!!")
        }
    
        @objc func pinchGetstureDetected() {
            print("Pinch Gesture detected!!")
        }
    
        @objc func longPressGetstureDetected() {
            print("Long Press Gesture detected!!")
        }
    
        @objc func doubleTapGestureDetected() {
            print("Double Tap Gesture detected!!")
        }
    
        @objc func panGestureDetected()
        {
            print("Pan Gesture detected!!")
        }
    
    
        //MARK: Shake Gesture
        override func becomeFirstResponder() -> Bool {
            return true
        }
        override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?){
            if motion == .motionShake
            {
                print("Shake Gesture Detected")
            }
        }
    }
    

提交回复
热议问题