Has the UILongPressGestureRecognizer changed in Swift?

Asked 2 years ago, Updated 2 years ago, 31 views

I tried a quick test of UILongPressGestureRecognizer with swift.
The objective-c successfully detected a long press, but
Swift did not detect a long press of firstL.

Where is the problem with this code?

class ViewController:UIViewController {

    @IBOutlet weak var firstL:UIButton!
    @IBOutlet weak var secondL:UIButton!

    override func viewDidLoad(){
        super.viewDidLoad()

       letgest=UILongPressGestureRecognizer(target:self, action:"press:")


        best.minimumPressDuration=0.5

        firstL.addGestureRecognizer(gest)

        secondL.addGestureRecognizer(gest)

    }

    funcpress(sender:UILongPressGestureRecognizer) {
        if let bb = sender.view as ? UIButton {
           println "yes"
        }
    }
}

swift

2022-09-30 17:20

1 Answers

Even if you write a program with the same content in Objective-C, the firstL button still doesn't respond to the long press.
The reason may be that an instance of one UIGestureRecognizer (subclass) can register only one UIView (subclass) instance.

firstL.addGestureRecognizer(gest)

secondL.addGestureRecognizer(gest)

If you register continuously like this, it will be canceled if you register first.

firstL.addGestureRecognizer(gest)

// secondL.addGestureRecognizer(gest)

If you delete the secondL registration, firstL responds to the long press.
This point is exactly the same when written in Objective-C.


2022-09-30 17:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.