ios - 除了按钮内部之外的所有地方都禁用触摸? - swift

标签 ios swift sprite-kit touch skspritenode

当我在游戏中死亡时,我想忽略用户的所有触摸事件,除非用户点击重置游戏按钮的内部或按钮。这是我的代码。

  for touch in touches{
        let location = touch.locationInNode(self)

        if died == true{
            Ball.physicsBody?.velocity = CGVectorMake(0, 0)
            if resetGame.containsPoint(location){

                restartScene()
                runAction(SKAction.playSoundFileNamed("Woosh.wav", waitForCompletion: false))

            }

            else  {


                self.userInteractionEnabled = false



            }

这一切都在我的touchesBegan里面。这是我试图忽略用户的触摸,除非触摸的位置在按钮的大小范围内。我怎样才能忽略用户在屏幕上除按钮以外的所有地方的触摸? resetGame 是一个 SKSpriteNode 图像。

最佳答案

您的问题有两种解决方案。

我想给大家推荐的第一个案例是基于手势识别器的。 您可以将按钮与其他触摸事件分开,并通过 bool 变量打开/关闭触摸事件,如下所示:

手势识别器:

在你的类的全局变量声明部分:

var tapGesture :UITapGestureRecognizer!
var enableTouches:Bool = true

override func didMoveToView(view: SKView) {
        super.didMoveToView(view)
        self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(GameClass.simpleTap(_:)))
        self.tapGesture.cancelsTouchesInView = false
        view.addGestureRecognizer(tapGesture)
        myBtn.name = "myBtn"
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        return self.enableTouches
}

func simpleTap(sender: UITapGestureRecognizer) {
        print("simple tap")
        if sender.state == .Ended {
           var touchLocation: CGPoint = sender.locationInView(sender.view)
           touchLocation = self.convertPointFromView(touchLocation)
           let touchedNode = self.nodeAtPoint(touchLocation)
           // do your stuff
           if touchedNode.name == "myBtn" { 
              // button pressed, do your stuff
           }
        }
}

override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
       if died == true { 
            self.enableTouches = false // disable all touches but leave gestures enabled
            //do whatever you want when hero is died
       }
}

只有一个 bool 值

我想提出的第二个解决方案是使用一个简单的 bool 值来停止触摸流(它不是很优雅,但可以工作)。 此方法查看按钮何时被点击,更新方法检查您的英雄是否已死,因此所有触摸都将被禁用:

在你的类的全局变量声明部分:

var enableTouches:Bool = true

override func didMoveToView(view: SKView) {
            super.didMoveToView(view)
            myBtn.name = "myBtn"
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (!enableTouches) {
            return
        }

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)
        let touchedNode = self.nodeAtPoint(positionInScene)
        // do your stuff
        if touchedNode.name == "myBtn" { 
           // button pressed, do your stuff
           if died == true { 
                self.enableTouches = false // disable all touches
                //do whatever you want when hero is died
           }
        }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (!enableTouches) {
            return
        }

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)
        let touchedNode = self.nodeAtPoint(positionInScene)
        // do your stuff

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (!enableTouches) {
            return
        }
}

第一种情况允许您始终启用手势,这样当您的英雄将死时,您还可以用手势做其他事情。第二种情况会在您按下按钮并执行“模具流程”时停止您的触摸。选择最适合您的。

关于ios - 除了按钮内部之外的所有地方都禁用触摸? - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035663/

相关文章:

ios - 日期格式 Swift 3.0

swift - 核心运动: Value of optional type 'NSOperationQueue?' not unwrapped

xcode - Swift:如何为我的游戏添加地板?

swift 3 (SpriteKit) : Locking the x axis of a SKSpriteNode and its physicsBody

iOS Swift Firebase - 如何使当前用户数据在整个应用程序中可用?

javascript - Facebook 登录设置 : Where is the "Enable Client Access Token Flow" switch?

ios - 通过 didFinishLaunchingWithOptions 通过远程推送启动应用程序时延迟 openURL

ios - 如何使用 Xcode 7 beta 编译旧的 Swift 代码?

ios - 在 Swift Sprite Kit 中核心数据可用吗?

ios - 键盘完成键 Action swift iOS 不起作用