gpt4 book ai didi

ios - 使用触摸滚动 SpriteNode

转载 作者:行者123 更新时间:2023-11-29 01:40:34 24 4
gpt4 key购买 nike

我想在没有 uiscrollview 的情况下滚动 Sprite 套件中的节点

//触摸移动

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 操作,结果会非常糟糕,但如果不使用,结果也会很糟糕

最佳答案

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

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


如果您发现 UIScrollView 没有将触摸传递给 touchesMoved,您可以尝试一些不同的方法。
- 您可以添加 UITapGestureRecognizer 并将 UIScrollView CanCancelContentTouches 设置为 YES
- 您可以子类化 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)
}
}

关于ios - 使用触摸滚动 SpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com