UISwipeGestureRecognizer 的奇怪行为 - 微冻结应用程序

Strange behavior of UISwipeGestureRecognizer - micro freezing app

我对 Swiperecognizer 有一个奇怪的问题。

我在屏幕上生成了对象。

let SpawnObject = SKAction.run({
        () in

        showobject()

    })

    let delay1 = SKAction.wait(forDuration: 0.9)
    let SpawnDelay1 = SKAction.sequence([SpawnObject,delay1])
    let SpawnDelayForever1 = SKAction.repeatForever(SpawnDelay1)
    self.run(SpawnDelayForever1)

    let distance = CGFloat(self.frame.height + 200)
    let moveObject = SKAction.moveBy(x: -distance, y: 0, duration: TimeInterval(0.004 * distance))
    let removeObject = SKAction.removeFromParent()
    moveAndRemove = SKAction.sequence([moveObject,removeObject])

我有一些物理对象(对象 2,B.png 类似)。

func showObject(){
    let texture =  SKTexture(imageNamed: "A.png")
    object = SKSpriteNode(texture: texture)
    object.name = "A"
    object.position = CGPoint(x: 0, y: self.frame.width)
    object.setScale(0.7)
    object.zPosition = 2
    object.run(moveAndRemove)

    object.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
    object.physicsBody?.categoryBitMask = PhysicsCategory.Object
    object.physicsBody?.collisionBitMask = PhysicsCategory.Object2
    object.physicsBody?.contactTestBitMask = PhysicsCategory.Object2
    object.physicsBody?.affectedByGravity = false
    object.physicsBody?.isDynamic = true


    addChild(object)
}

我在 touchesBegan 中设置了滑动识别器

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        location = touch.location(in: self)
        node = self.atPoint(location)
        if node.name == "A" {
            //node.run(SKAction.moveBy(x: 1000, y: 0, duration: 1)).  //Here I try set action only to touch
            let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rightSlide(_:)))
            rightSwipe.direction = .right
            view!.addGestureRecognizer(rightSwipe)
        }
        else if node.name == "B" {
            //node.run(SKAction.moveBy(x: -1000, y: 0, duration: 1)).   //Here I try set action only to touch
            let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(leftSlide(_:)))
            leftSwipe.direction = .left
            view!.addGestureRecognizer(leftSwipe)
        }else {print ("Blank space")}
    }

}

func leftSlide(_ sender: UISwipeGestureRecognizer){
    if node.name == "B" {
    node.run(SKAction.moveBy(x: -1000, y: 0, duration: 1))
    }else {print ("Bad swipe")}
    }
func rightSlide(_ sender: UISwipeGestureRecognizer){
    if node.name == "A" {
    node.run(SKAction.moveBy(x: 1000, y: 0, duration: 1))
    }else {print ("Bad swipe")}
}

现在是问题。对象显示在屏幕上,当我在它们上滑动时,它们消失在一边。一切正常,直到我将大约 50 个对象刷掉,之后每次下一次刷卡冻结应用程序微秒,对象奇怪地向下跳几个像素(更多刷卡,更多像素)并返回到它们的原始位置并且在这个奇怪的行为对象刷掉之后.更多滑动 = 更大的冻结和更大的跳跃。

我尝试关闭物理但什么也没有。但是,当我直接将操作更改为触摸(无需滑动,只需触摸)时,应用程序将永远运行良好。所以一定有错误的刷卡识别器。或不?我检查了 CPU 和 MEMORY,CPU 有 35%,内存大约为 50MB。屏幕上的节点最多 20。FPS 为 60,但当应用程序冻结时,它会下降到 58(如闪烁)。

有什么建议吗?如何诊断问题出在哪里?谢谢!

不要每次都在 touchesBegan 方法中添加识别器,您应该执行以下操作:

1) 创建一个数组来存储所有的识别器,这样你以后就可以轻松地删除它们

2) 只添加一次识别器。请注意,识别器是添加到视图中,而不是场景中

3) 当场景即将从视图中移除时移除它们

import SpriteKit

class GameScene: SKScene,SKPhysicsContactDelegate {


     var recognizers:[UIGestureRecognizer] = []

    override func didMove(to view: SKView) {


        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.leftSlide(recognizer:)))
        leftSwipe.direction = .left
        leftSwipe.numberOfTouchesRequired = 1
        self.view?.addGestureRecognizer(leftSwipe)
        recognizers.append(leftSwipe)

         //do the same for other recognizers

    }

    func leftSlide(recognizer:UISwipeGestureRecognizer){
        print("Swipe")
    }

//define other methods here to handle other recognizers...

  override func willMove(from view: SKView) {
    super.willMove(from: view)

    for recognizer in recognizers {
        self.view?.removeGestureRecognizer(recognizer)
    }
    recognizers.removeAll()
}
}