我有一個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(如閃爍)。
有什麼建議嗎?如何診斷問題在哪裏?謝謝!
看來你是在每一個水龍頭添加手勢識別。在didMove(toView :)中將一個識別器定義爲一個場景的屬性,並在willMove(fromView :)中將其刪除。 – Whirlwind
我很快就很新。你能告訴我更多嗎?或者發佈一些例子?謝謝! –
結帳我的答案...讓我知道如果你仍然卡... – Whirlwind