ios - 如何在didMoveToView(Swift,SpriteKit)中注册触摸?

标签 ios swift sprite-kit

我在第一个场景中有一个重复 Action ,我想在用户第一次触摸屏幕时立即停止。是否可以在 didMoveToView 中检测触摸?我无法使用 touchesBegan,因为这是仅第一次触摸的特殊情况,我不希望每次触摸都重复它。

override func didMoveToView(view: SKView) {
    triangle.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
    self.addChild(triangle)
    triangle.runAction(SKAction.repeatActionForever(rotateAction))
    //->This is where I need to detect a touch
}

最佳答案

didMoveToView 期间——尤其是当您的应用程序启动时第一次调用它时——目前还不可能有当前触摸事件,因为您没有运行来响应主事件循环。如果您想处理触摸,touchesBegan 就是执行该操作的地方。

如果您只想在收到的第一个触摸事件上执行某些操作,则只需跟踪触摸事件是否是第一个即可。例如:

var touchedBefore = false
override func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?) {
    if !touchedBefore {
        touchedBefore = true
        // do your first-touch business
    } else {
        // do your touch handling for other times
    }
}

关于ios - 如何在didMoveToView(Swift,SpriteKit)中注册触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33698725/

相关文章:

ios - Xcode 9 没有可编译的体系结构(ARCHS=arm64、VALID_ARCHS=armv6 armv7)

ios - 如何更改 View 比例

ios - 隐藏键盘 IOS Swift

Swift setsockopt SOL_SOCKET SO_RCVTIMEO 获取无效参数

ios - 我怎样才能得到真正的 'self' ,哪个节点运行 SKaction 序列

ios - 使用 SKReferenceNode 引用的 SpriteKit 场景未加载自定义类

ios - 限制对 Firebase 存储的访问,以便只有我的应用可以访问它

ios - 无法从 iOS 连接到 signalR 服务器

ios - 检查设备日期设置在iOS中是否是自动的

ios - Swift Infinite Scroller FPS 代码效率低下