首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用touches滚动SpriteNode

使用touches滚动SpriteNode
EN

Stack Overflow用户
提问于 2015-09-07 12:59:06
回答 1查看 252关注 0票数 0

我想在没有uiscrollview的sprite工具箱中滚动一个节点。

// In touchesMoved

代码语言:javascript
复制
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操作,结果是非常可怕的,但是如果没有结果,结果也是可怕的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-07 13:41:18

在SpriteKit中,一种很好的方法是使用UIPanGestureRecognizer来检测触摸,其中您将创建一个SKActions序列,该序列将加速或减速移动。您可能必须使用update方法和/或touches来处理pan停止或另一个立即启动。不幸的是,如果您只想使用SpriteKit,就必须使用SKActions来实现这一点。

我想我也应该提一种更简单(也可能更好看)的方法来做这件事。看一看"ScrollKit",它运行得很好(尽管它确实使用了UIScrollView):https://github.com/bobmoff/ScrollKit

如果您发现UIScrollView没有传递给touchesMoved,您可以尝试一些不同的东西。

  • 您可以添加一个UITapGestureRecognizer并在UIScrollView CanCancelContentTouches上将其设置为YES
  • 您可以子类UIScrollView并覆盖touchesMethods,这样如果用户没有滚动,触摸信息就会传递到响应链中:
代码语言:javascript
复制
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)
    }    
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32439385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档