滚动SpriteNode使用触摸
问题描述:
我想滚动精灵套件节点没有UIScrollView的滚动SpriteNode使用触摸
// 在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行动的结果是非常可怕的,但没有结果也是可怕的
答
在SpriteKit中这样做的一种优雅的方式是使用UIPanGestureRecognizer
来检测触摸,并在其中你无线我们将创建一系列可以加速或减速运动的SKA。您可能必须使用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)
}
}
我必须要能够利用滚动内的节点上使用ScrollKit swift版本,touchesBegan中的代码永远不会运行。你有没有UIPanGestureRecognizer的例子? – grape1
@ grape1看看我的编辑,它应该帮助你触摸方法和ScrollKit。使用UIPanGestureRecognizer进行自定义实现将会相当复杂,因为如果它看起来不错(因为一切都将通过SKActions完成,并且您已经看到了这种情况)。就我个人而言,我从来没有遇到过它的外观问题,但我从来不需要创建一个长时间的滚动操作。为此,我会为了简单起见而喜欢ScrollKit。 – MaxKargin
ScrollKit对我所需要的太多了。一个简单的垂直滚动节点,其中包含要选择的字符网格。 – grape1