【问题标题】:Scroll SpriteNode using touches使用触摸滚动 SpriteNode
【发布时间】:2015-09-07 22:29:26
【问题描述】:

我想在没有 uiscrollview 的情况下滚动 sprite kit 中的节点

// 在 touchesMoved 中

let touch: UITouch = touches.first as! UITouch;
let location:CGPoint = touch.locationInNode(self);
let lastPosition = touch.previousLocationInNode(self)

var newLoc = selectNode.position.y + (location.y - lastPosition.y)
// Check if the top part is reached
if(newLoc < selectNodeStart){
    newLoc = selectNodeStart
}

if(selectNode.position.y >= selectNodeStart){
    // let sk = SKAction.moveToY(newLoc, duration: 0.1)
    // selectNode.runAction(sk)
    selectNode.position.y = newLoc
}

如果我使用 sk 动作,结果很糟糕,但没有结果也很糟糕

【问题讨论】:

    标签: ios swift uiscrollview sprite-kit


    【解决方案1】:

    在 SpriteKit 中执行此操作的一种优雅方式是使用 UIPanGestureRecognizer 来检测触摸,在其中您将创建一系列 SKAction 来加速或减速运动。您可能必须使用update 方法和/或touches 来处理平移停止或另一个立即启动。不幸的是,如果你只想使用 SpriteKit,你将不得不使用 SKActions 来实现它。

    我想我还会提到一种更简单(并且可能更好看)的方法来做到这一点。看看“ScrollKit”,它工作得很好(虽然它确实使用了 UIScrollView):https://github.com/bobmoff/ScrollKit


    如果您发现 UIScrollView 没有通过触摸到 touchesMoved,您可以尝试一些不同的方法。
    - 您可以添加一个UITapGestureRecognizer 并在UIScrollView 上设置CanCancelContentTouchesYES
    - 您可以继承 UIScrollView 并覆盖 touchesMethods,这样如果用户没有滚动,触摸信息将向上传递到响应者链:

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent){
    
         if (!self.dragging){
             self.nextResponder.touchesBegan(touches , withEvent: event) 
         }
         else{
             super.touchesBegan(touches , withEvent: event)
         }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent){
    
        if (!self.dragging){
            self.nextResponder.touchesMoved(touches , withEvent:event)  
        }
        else{
            super.touchesMoved(touches , withEvent: event)
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent: event UIEvent){
    
        if (!self.dragging){ 
             self.nextResponder.touchesEnded(touches , withEvent: event)
        }
        else{
            super.touchesEnded(touches , withEvent: event)
        }    
    }
    

    【讨论】:

    • 我必须能够点击滚动视图内的节点,使用 ScrollKit swift 版本,touchesBegan 中的代码永远不会运行。你有 UIPanGestureRecognizer 的例子吗?
    • @grape1 看看我的编辑,它应该可以帮助您使用 touchesMethods 和 ScrollKit。如果要看起来不错,使用 UIPanGestureRecognizer 的自定义实现将相当复杂(因为一切都将使用 SKActions 完成,您已经看到了它是如何进行的)。就个人而言,我从来没有对它的外观有任何疑问,但我也从来没有创建过长滚动动作。为此,为了简单起见,我会使用类似 ScrollKit 的东西。
    • ScrollKit 无法满足我的需要。一个简单的垂直滚动节点,其中包含要选择的字符网格。
    • 我最终继承了 UIScrollView,感谢您的帮助
    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多