Button tap and long press gesture

后端 未结 1 998
暖寄归人
暖寄归人 2020-12-05 07:24

I\'m having a little trouble with the gestures.

I\'m trying to use both tap and long press on the same button, so I\'ve used

@IBAction func xxx (sen         


        
相关标签:
1条回答
  • 2020-12-05 08:06

    Hard to say what´s not working with your code, with the only two rows that you have provided, but I would recommend you to do it in this way instead:

    Create an outlet to your button instead

    @IBOutlet weak var myBtn: UIButton!
    

    And in your viewDidLoad() add the gestures to the buttons

    let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap")  
    let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
    tapGesture.numberOfTapsRequired = 1
    myBtn.addGestureRecognizer(tapGesture)
    myBtn.addGestureRecognizer(longGesture)
    

    And then create the actions to handle the taps

    func normalTap(){
    
        print("Normal tap")
    }
    
    func longTap(sender : UIGestureRecognizer){
        print("Long tap")
        if sender.state == .Ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
        }
        else if sender.state == .Began {
            print("UIGestureRecognizerStateBegan.")
            //Do Whatever You want on Began of Gesture
        }
    }
    

    Swift 3.0 version:

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.normalTap))
    let longGesture = UILongPressGestureRecognizer(target: self, action: Selector(("longTap:")))
    tapGesture.numberOfTapsRequired = 1
    myBtn.addGestureRecognizer(tapGesture)
    myBtn.addGestureRecognizer(longGesture)
    
    func normalTap(){
    
        print("Normal tap")
    }
    
    func longTap(sender : UIGestureRecognizer){
        print("Long tap")
        if sender.state == .ended {
            print("UIGestureRecognizerStateEnded")
            //Do Whatever You want on End of Gesture
        }
        else if sender.state == .began {
            print("UIGestureRecognizerStateBegan.")
            //Do Whatever You want on Began of Gesture
        }
    }
    

    Updated syntax for Swift 5.x:

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
    button.addGestureRecognizer(tapGesture)
    
    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
    button.addGestureRecognizer(longGesture)
    
    @objc func normalTap(_ sender: UIGestureRecognizer){
        print("Normal tap")
    }
    
    @objc func longTap(_ sender: UIGestureRecognizer){
        print("Long tap")
        if sender.state == .ended {
            print("UIGestureRecognizerStateEnded")
            //Do Whatever You want on End of Gesture
        }
        else if sender.state == .began {
            print("UIGestureRecognizerStateBegan.")
            //Do Whatever You want on Began of Gesture
        }
    }
    
    0 讨论(0)
提交回复
热议问题