ios - 在 Sprite Kit 中使用 3D touch 测量压力

标签 ios swift sprite-kit 3dtouch force-touch

在我的主应用程序中,我尝试使用 3D 触摸来激活子弹发射,但结果似乎非常不一致,而且它并不总是给我写入结果。当我使用这段代码时,我的 iPhone 6S 也崩溃了,这是我用于测试的代码。

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

        var force =  touch.force * 10
        var maxForce = touch.maximumPossibleForce

        print ("The force is")
        print (maxForce*4)
        print (force*10000000)

        if force != 0{


            myLabel.text = "Force"
            myLabel.fontSize = 45
            myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
            self.addChild(myLabel)
            myLabel.zPosition = 100000


        }

        else {

           myLabel.removeFromParent()
        }

最佳答案

我猜它会崩溃,因为您多次向父级添加标签(您的 if 条件被破坏,无论您调用什么函数,(touchesBegan,touchesMoved,touchesEnded) 只有一个条件永远为真,在 touchesBegantouchesMoved 中,你的力量永远 > 0,在你的 touchEnded 中,你的力应该是 0。(我假设这个代码是你的 touchMoved)相反,在你的 viewDidLoad 代码中添加你的标签,并使用.hidden 字段显示/隐藏你的移动标签。(确保在 touchesEnded 上隐藏它)

关于ios - 在 Sprite Kit 中使用 3D touch 测量压力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36245081/

相关文章:

ios - 仅对一个单元格进行缩进

iphone - 如何读取 Plist 数据到 UITableView

ios - SceneKit:如何一起为多个 SCNNode 设置动画,然后调用完成 block 一次

ios - 如何以编程方式为文本添加大纲?

swift - 如果条件为真时语句未运行

ios - [NSKeyedArchiver archivedDataWithRootObject :self. 数据] 崩溃

ios - 即使重写的 hittest 返回正确的值,注释也不响应命中

ios - 尝试使用 NSUserDefaults() 保存数组时出错

Xcode 6.3 和 Swift : Unicode Utilities (e. g。 UCKeyTranslate)不可用?

audio - 首次使用 SpriteKit 播放声音时的小延迟