iOS Development

ios – Occasion response priorities for gesture recognizers, UIControl, and UIButton

I’ve a guardian view with a gesture recognizer added. The guardian view accommodates a UIButton and a UIControl, with corresponding actions added respectively.

The code is as follows

class MyViewController: UIViewController {
    
     personal lazy var parentView = UIView()
     personal lazy var button = UIButton(body: .zero)
     personal lazy var management = UIControl(body: .zero)
    
     override func viewDidLoad() {
         tremendous.viewDidLoad()
        
         view.backgroundColor = .white
         view.addSubview(parentView)
         parentView.addSubview(button)
         parentView.addSubview(management)
        
         parentView.backgroundColor = .crimson
         button.backgroundColor = .yellow
         management.backgroundColor = .blue
        
         let tapGesture = UITapGestureRecognizer(goal: self, motion: #selector(handleParentViewTapped))
         parentView.addGestureRecognizer(tapGesture)
         button.addTarget(self, motion: #selector(handleButtonClicked), for: .touchUpInside)
         management.addTarget(self, motion: #selector(handleControlClicked), for: .touchUpInside)
        
         parentView.snp.makeConstraints { make in
             make.edges.equalToSuperview()
         }
         button.snp.makeConstraints { make in
             make.main.prime.equalToSuperview()
             make.dimension.equalToSuperview().multipliedBy(0.5)
         }
         management.snp.makeConstraints { make in
             make.trailing.backside.equalToSuperview()
             make.dimension.equalToSuperview().multipliedBy(0.5)
         }
     }
    
     @objc func handleParentViewTapped() {
         print("view tapped")
     }
    
     @objc func handleButtonClicked() {
         print("button clicked")
     }
    
     @objc func handleControlClicked() {
         print("UIControl clicked")
     }

}

I clicked UIButton and UIControl successively, and the console printing sequence is as follows

button clicked
view tapped

As you possibly can see, clicking a UIButton can set off the motion equivalent to the UIButton, however clicking a UIControl will set off the callback of the gesture recognizer.

Subsequent, I set the cancelsTouchesInView of the guardian view’s gesture recognizer to false. This time, the callbacks of the gesture recognizer, UIButton, and UIControl will all be known as.

view tapped
button clicked
view tapped
UIControl clicked

my query is

  1. Since UIButton additionally inherits from UIControl, why is the occasion response precedence of UIButton larger than the gesture recognizer of the guardian view, however UIControl is decrease than the gesture recognizer of the guardian view?
  2. If I’ve a customized UIControl and wish to obtain the identical precedence impact as a UIButton, how ought to I obtain it?
  3. I do know that if the cancelsTouchesInView of the gesture recognizer is about to false, the press occasion will proceed to be handed to the UIControl, so the UIControl can reply to the occasion. However why can each UIButton and gesture recognizers reply to occasions after cancelsTouchesInView is about to false?

Credit: www.ismmailgsm.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button