我对触摸如何向上渗透到父视图感到困惑。为了测试,我构建了一个具有 4 个 UIButtons 的简单程序。Button 类向上发送触摸事件:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
for var i=0; i<4; i++ {
let button = Button(type: UIButtonType.System) as Button
button.frame = CGRectMake(0,0,200,100)
button.center = CGPoint(x: i*250+100,y: 50)
button.setTitle(String(i), forState: UIControlState.Normal)
self.view.addSubview(button)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("ViewController touchesBegan",touches.count)
for touch in touches {
print(touch.locationInView(self.view).x,touch.locationInView(self.view).y)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("ViewController touchesMoved",touches.count)
for touch in touches {
print(touch.locationInView(self.view).x,touch.locationInView(self.view).y)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("ViewController touchesEnded",touches.count)
for touch in touches {
print(touch.locationInView(self.view).x,touch.locationInView(self.view).y)
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
print("ViewController touchesCancelled")
}
}
class Button : UIButton {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("button",self.center,"touchesBegan")
self.nextResponder()?.touchesBegan(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("button",self.center,"touchesEnded")
self.nextResponder()?.touchesEnded(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("button",self.center,"touchesMoved")
self.nextResponder()?.touchesMoved(touches, withEvent: event)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
print("button",self.center,"touchesCancelled")
self.nextResponder()?.touchesCancelled(touches, withEvent: event)
}
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if (self.focused) {
print("button",self.center,"YES didUpdateFocus")
} else {
print("button",self.center,"NO didUpdateFocus")
}
}
}
我所期望的是所有与触摸相关的项目都会出现在 ViewControllers 触摸处理程序中,但它们没有。使用 tvOS 遥控器进行简单的右移显示以下内容:
button (100.0, 50.0) YES didUpdateFocus
button (100.0, 50.0) touchesBegan
ViewController touchesBegan 1
100.0 50.0
button (100.0, 50.0) touchesMoved
ViewController touchesMoved 1
116.728515625 50.0
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) NO didUpdateFocus
button (350.0, 50.0) YES didUpdateFocus
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesMoved
button (100.0, 50.0) touchesEnded
为什么在所有“按钮 touchesMoved”通知之后我看不到“ViewController touchesMoved”?
我认为这是因为发起触摸的按钮不再“聚焦”所以父视图忽略了触摸响应器..但是(i)为什么在按钮丢失之前某些动作没有出现在视图控制器中焦点,以及(ii)为什么焦点所在的按钮不显示 touchesMoved 并将该消息发送到 ViewController?
非常感谢有人可以对此有所了解。
吉姆