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"
}
}
}
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.
© 2024 OneMinuteCode. All rights reserved.