2016-09-25 32 views
0

我目前正在使用街機應用程序,用戶點擊精靈跳過障礙物並向下滑動以便在障礙物下滑動。我的問題是,當我開始滑動時,touchesBegan函數被調用,所以精靈跳轉而不是滑動。有沒有辦法區分這兩個?在Spritekit中區分滑動觸摸 - Swift 3

+0

使用手勢來區分 – Knight0fDragon

+0

但我想挖掘不執行一些動作 –

+0

...水龍頭是一種姿態,所以是一個刷卡 – Knight0fDragon

回答

1

您可以使用手勢狀態來微調用戶交互。手勢協調一致,所以不應互相干擾。

func handlePanFrom(recognizer: UIPanGestureRecognizer) { 
    if recognizer.state != .changed { 
     return 
    } 

    // Handle pan here 
} 

func handleTapFrom(recognizer: UITapGestureRecognizer) { 
    if recognizer.state != .ended { 
     return 
    } 

    // Handle tap here 
} 
1

如何使用觸摸控制略有延遲?我有一個遊戲,在那裏我使用SKAction做類似的事情。您還可以選擇設置的位置屬性,讓您的自我一點回旋的餘地與touchesMoved方法的櫃面有人有顛簸的手指(感謝KnightOfDragon)

let jumpDelayKey = "JumpDelayKey" 
var startingTouchLocation: CGPoint? 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     let location = touch.location(in: self) 

     // starting touch location 
     startingTouchLocation = location 

     // start jump delay 
     let action1 = SKAction.wait(forDuration: 0.05) 
     let action2 = SKAction.run(jump) 
     let sequence = SKAction.sequence([action1, action2]) 
     run(sequence, withKey: jumpDelayKey) 
    } 
} 

func jump() { 
    // your jumping code 
} 

只要確保延遲不會太長,使你的控件不要覺得沒有反應。玩你想要的結果的價值。

比移動方法,你刪除SKAction你的觸摸,如果你的舉動已經達到閾值

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches { 
     let location = touch.location(in: self) 

     guard let startingTouchLocation = startingTouchLocation else { return } 

     // adjust this value of how much you want to move before continuing 
     let adjuster: CGFloat = 30 

     guard location.y < (startingTouchLocation.y - adjuster) || 
       location.y > (startingTouchLocation.y + adjuster) || 
       location.x < (startingTouchLocation.x - adjuster) || 
       location.x > (startingTouchLocation.x + adjuster) else {  

回報}

 // Remove jump action 
     removeAction(forKey: jumpDelayKey) 

     // your sliding code 
    } 
} 

雖然我不知道,你可以玩的手勢識別器將工作以及它如何影響響​​應者鏈。

希望這有助於

+0

這不是一個可行的答案,你需要添加閾值並追蹤觸摸動作。如果有人有一個痙攣的手指怎麼辦?對於那個用戶,他們很感動,但是對於代碼,他們正在移動他們的手指 – Knight0fDragon

+0

這是真的,我只是添加了額外的位置檢查。 – crashoverride777

+0

嗯,當你移動一個啓用了觸摸的精靈時會發生什麼?你失去了touchmoved電話? – Knight0fDragon