tvOS touchesBegan,touchesMoved,touchesEnded

时间:2015-12-13 12:44:28

标签: tvos touchesbegan touchesmoved touches touchesended

我对如何触摸向上流向父视图感到困惑。为了测试,我构建了一个包含4个UIButton的简单程序。 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)为什么之前没有一些动作出现在viewcontroller中按钮失去焦点,以及(ii)为什么IS对焦的按钮不显示touchesMoved并将该消息发送到ViewController?

非常感谢有人可以解决这个问题。

吉姆

0 个答案:

没有答案