2017-03-12 82 views
1

我有一個Swiperecognizer奇怪的問題。UISwipeGestureRecognizer的奇怪行爲 - 微型冷凍應用程序

我在屏幕上產卵物體。

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]) 

我有一些物理對象(Object2,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和內存和CPU有35%和內存大約50MB。屏幕上的節點最多20. FPS爲60,但當應用程序凍結時,它會下降到58(如閃爍)。

有什麼建議嗎?如何診斷問題在哪裏?謝謝!

+0

看來你是在每一個水龍頭添加手勢識別。在didMove(toView :)中將一個識別器定義爲一個場景的屬性,並在willMove(fromView :)中將其刪除。 – Whirlwind

+0

我很快就很新。你能告訴我更多嗎?或者發佈一些例子?謝謝! –

+0

結帳我的答案...讓我知道如果你仍然卡... – Whirlwind

回答

1

而是在的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() 
} 
} 
+0

是的!很棒!可能removeGestureRecognizer是神奇的:)我可以有最後一個小問題嗎?當我啓動應用程序 - >產生第一個對象 - >第一次刷卡對象不工作 - >第二次刷卡工作。之後,每次刷新新對象都可以正常工作。你知道爲什麼在第一個對象上第一次滑動不起作用嗎? :(謝謝。 –

+0

檢查你的觸摸處理邏輯是否正確。如果第一次調用刷卡處理程序,請在其中放入一個斷點或打印語句,但如果你的觸摸處理邏輯錯誤正在體驗你所描述的內容 – Whirlwind

+0

我在第一次滑動應用程序停止後,設置了兩個滑動點並正常工作的斷點 –