【问题标题】:Why does my node stop rotating if I move my finger off my node and into the GameView?如果我将手指从节点上移到 GameView 中,为什么我的节点会停止旋转?
【发布时间】:2016-05-02 02:55:16
【问题描述】:

我有这个节点,如果我的手指放在它上面我可以旋转。我遇到的问题是,当我旋转节点时,我的手指从节点滑落到视图上,它停止旋转。如果我的手指不再在节点上,我怎么还能触摸到?

例如,我的应用程序中有一个 UISlider,如果我的手指放在滑块上并向下移动,然后尝试将滑块向左和向右移动,它仍然可以工作。我如何能够对触摸事件仍然存在的节点执行相同的操作?如果你不明白我在说什么,请告诉我。这是我用来旋转节点的代码:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)

    if node.name == "circle"                 
       //lets user rotate when there is one finger on node.
       let dy = circle.position.y - location.y
       let dx = circle.position.x - location.x
       let angle2 = atan2(dy, dx)
       circle.zRotation =  angle2
    }

【问题讨论】:

    标签: ios swift touchesmoved


    【解决方案1】:

    您应该将最后一个节点存储在实例变量中,然后使用它尝试保持旋转,即使它没有碰到它。

    class MyClass  {
    var lastNodeSelected:SKNode?
    
    override func touchesStart(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
    let location = touch.locationInNode(self)
    let node = self.nodeAtPoint(location)
    
        if node.name == "circle"                 
           lastNodeSelected = node
        }
    }
    
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
    let location = touch.locationInNode(self)
    
        if lastNodeSelected != nil {
           let touchedNode = lastNodeSelected!                 
           let dy = touchedNode.position.y - location.y
           let dx = touchedNode.position.x - location.x
           let angle2 = atan2(dy, dx)
           touchedNode.zRotation =  angle2
        }
    
    override func touchesEnd(touches: Set<UITouch>, withEvent event: UIEvent?) {
       lastNodeSelected = nil
    

    【讨论】:

    • 谢谢它的工作!你在 touchesStart 上犯了一个错误,它应该是 touchesBegan。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2019-03-16
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多