How to call gesture tap on UIView programmatically in swift

后端 未结 23 1675
情歌与酒
情歌与酒 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:04

    If you want Objective C code is given below,

    UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; // Declare the Gesture.
    gesRecognizer.delegate = self;
    [yourView addGestureRecognizer:gesRecognizer]; // Add Gesture to your view.
    
    // Declare the Gesture Recognizer handler method.
    
    - (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{
       NSLog(@"Tapped");
    }
    

    or you want swift code is given below,

    import UIKit
    class ViewController: UIViewController {
    
        @IBOutlet weak var myView: UIView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Add tap gesture recognizer to view
            let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
            myView.addGestureRecognizer(tapGesture)
        }
    
        // this method is called when a tap is recognized
        func handleTap(sender: UITapGestureRecognizer) {
    
            print("tap")
        }
    }
    
    0 讨论(0)
  • 2020-11-28 19:06

    Instead of invoking myView's UITapGestureRecognizer, you can directly call the handleTap function,

    0 讨论(0)
  • 2020-11-28 19:09

    For anyone who is looking for Swift 3 solution

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    
    view.addGestureRecognizer(tap)
    
    view.isUserInteractionEnabled = true
    
    self.view.addSubview(view)
    
    // function which is triggered when handleTap is called
     func handleTap(_ sender: UITapGestureRecognizer) {
         print("Hello World")
      }
    
    0 讨论(0)
  • 2020-11-28 19:12

    I worked out on Xcode 6.4 on swift. See below.

    var view1: UIView!
    
    func assignTapToView1() {          
      let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap"))
      //  tap.delegate = self
      view1.addGestureRecognizer(tap)
      self.view .addSubview(view1)
    
    ...
    }
    
    func handleTap() {
     print("tap working")
     view1.removeFromSuperview()
     // view1.alpha = 0.1
    }
    
    0 讨论(0)
  • 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")
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 19:15

    I worked out on Xcode 7.3.1 on Swift 2.2. See below.

    func addTapGesture() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.handleTap))
        tap.numberOfTapsRequired = 1
        self.myView.addGestureRecognizer(tap)
    }
    
    func handleTap() {
        // Your code here...
    }
    
    0 讨论(0)
提交回复
热议问题