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 itssuper
views. (even tho the button'stouchesMoved
is called and thatnext?
is notnil
touchesEnded(...)
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 git@bitbucket.org:5t4rrk/problemtouchmovedonlyonce.git
If someone has any insights about why is this behavioring like this, please let me know \o/