Моя цель, имея одновременно и то, и другое:
- UIButton, обрабатывающий событие (
.touchDown
) - другое представление выше в иерархии (т. е. супер), которое получает
touchBegan/Moved/Ended/Cancelled
.
Я хочу это событие, потому что мне нужна сила касания и другие вещи для некоторых вычислений.
В верхнем/супер вид я переопределяю тач Начал и друзей, чтобы получить силы и прочее.
НО, по сути, UIButton не пересылает событие касания, поэтому (в этом примере) я расширяю UIButton (в моем коде я расширяю подкласс ~, но это не меняет проблему) и переопределяю touchesBegan и друзей и добавляю next?.touchesBegan(...)
к нему.
Что работает:
touchesBegan(...)
правильно перенаправляет в суперпредставление
Что не работает:
touchesMoved(...)
только пересылать ОДИН РАЗ на своиsuper
просмотров. (даже если кнопкаtouchesMoved
называется, аnext?
неnil
touchesEnded(...)
НЕ ВЫЗЫВАЕТСЯ, еслиtouchesMoved(...)
уже был вызван (только один вызов touchesMoved(...) если вы следуете). и сноваnext?
неnil
// 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")
}
}
Вот пример проекта, демонстрирующий проблему:
git clone [email protected]:5t4rrk/problemtouchmovedonlyonce.git
Если у кого-то есть какие-либо идеи о том, почему это ведет себя так, пожалуйста, дайте мне знать\o/