My goal, having at the same time, both:
- a UIButton that handle an event (
.touchDown) - another view upper in the hierarchy (i.e.: super) that receives a
touchBegan/Moved/Ended/Cancelled.
I want that event because, I need the touch force and other stuff for some computing
In the upper/super view, I override the touchesBegan and friends, so that I can get forces and stuff.
BUT, basically, a UIButton doesn't forward a touch event, so (in this example) I extend UIButton (in my code I extend a subclass~ but that doesn't change the problem) and override the touchesBegan and friends, and add next?.touchesBegan(...) to it.
What works:
touchesBegan(...)forwards to the super view correctly
What does not work:
touchesMoved(...)only forward ONCE to itssuperviews. (even tho the button'stouchesMovedis called and thatnext?is notniltouchesEnded(...)is NOT CALLED when atouchesMoved(...)has been called before (only one touchesMoved(...) call if you follow). and againnext?is notnil
// One of the overrided UIButton touches event
extension UIButton {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("Button: touches began - ")
super.touchesBegan(touches, with: event)
next?.touchesBegan(touches, with: event)
if next == nil { print("next was nil!") }
print("Button: touches began - end\n")
}
}
// One of the overrided ViewController touches event
// (which is only called once for touchesMoved, and then touchesEnded not called)
extension ViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("ViewController: touches began - ")
super.touchesBegan(touches, with: event)
print("ViewController: touches began - end\n")
}
}
Here is an example project to show you the problem:
git clone [email protected]:5t4rrk/problemtouchmovedonlyonce.git
If someone has any insights about why is this behavioring like this, please let me know \o/
I tried a slightly different approach using a subclass of a
UIGestureRecognizer. See the code below based on your example project. Interesting it seems the UIButton is checking for the state.beginwhich the touchesBegan is setting. It might be possible to change your code to not set the state in thetouchesBeganmethod. See if this works for you.The UIKit gesture recognizers also give you a few delegate methods and the option to use a selector which has access to the state set by the touches... methods. But they don't give you that much information for each individual touch.
Sources:
developer.apple.com/documentation/uikit/touche developer.apple.com/documentation/uikit/uigesturer
raywenderlich.com/6747815-uigesturerecognizer-tu
Code: