2017-01-04 41 views
0

因此,我已經在我的遊戲中實現了手勢recongizers來檢測我的玩家的移動,但發現他們沒有給我我想要的結果,所以我正在考慮製作滑動手勢在觸摸方法和觸摸方法中也是觸摸方法。我已經設法使觸摸功能在觸摸方法中工作,但我無法實現在觸摸方法中滑動的功能,我似乎無法找到關於如何執行此操作的教程。我下面的代碼顯示了我使用的嘗試和實現這一目標的方法接觸:在swift中觸摸方法中的滑動手勢

class GameScene: SKScene { 

var touchOrigin = CGPoint() 
var player = SKSpriteNode() 


override func didMove(to view: SKView) { 

    backgroundColor = .black 

    player = SKSpriteNode(texture: nil, color: .orange, size: CGSize(width: 50, height: 50)) 
    player.position = CGPoint(x: 0, y: 0) 
    addChild(player) 

} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches { 

     var currentTouchPosition = touch.location(in: self) 
     touchOrigin = touch.location(in: self) 

     if (Int(touchOrigin.x) > Int(currentTouchPosition.x)) { 

      player.position.x -= 50 

     } else if (Int(touchOrigin.x) < Int(currentTouchPosition.x)) { 

      player.position.x += 50 

     } 

     if touch.tapCount == 1 { 

      player.position.y += 50 //replace this with function :) 

     } else if touch.tapCount >= 2 { 

      player.position.y += 150 // change to run shield effect :) 

     } 
    } 

} 

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
} 

} 

我怎樣才能讓觸摸法確認某個方向滑動手勢?如果他們在某個方向上滑動,而不是將他們的手指從屏幕上移開,然後在一個動作中滑回原點,我該如何才能將其識別爲龍頭?

回答

2

下面是如何檢測滑動手勢的示例。

首先,定義變量來保存的起始位置和時間

var start:(location:CGPoint, time:TimeInterval)? 

並限定滑動手勢的參數。相應地調整這些:

let minDistance:CGFloat = 25 
let minSpeed:CGFloat = 1000 
let maxSpeed:CGFloat = 6000 

touchesBegan,保存每個新的觸摸事件

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

touchesEnded的位置/時間,確定通過比較的距離和速度,如果用戶的手勢是一個滑動手勢。對角滑動的測試是可選的。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    var swiped = false 
    if let touch = touches.first, let startTime = self.start?.time, 
      let startLocation = self.start?.location { 
     let location = touch.location(in:self) 
     let dx = location.x - startLocation.x 
     let dy = location.y - startLocation.y 
     let distance = sqrt(dx*dx+dy*dy) 

     // Check if the user's finger moved a minimum distance 
     if distance > minDistance { 
      let deltaTime = CGFloat(touch.timestamp - startTime) 
      let speed = distance/deltaTime 

      // Check if the speed was consistent with a swipe 
      if speed >= minSpeed && speed <= maxSpeed { 

       // Determine the direction of the swipe 
       let x = abs(dx/distance) > 0.4 ? Int(sign(Float(dx))) : 0 
       let y = abs(dy/distance) > 0.4 ? Int(sign(Float(dy))) : 0 

       swiped = true 
       switch (x,y) { 
       case (0,1): 
        print("swiped up") 
       case (0,-1): 
        print("swiped down") 
       case (-1,0): 
        print("swiped left") 
       case (1,0): 
        print("swiped right") 
       case (1,1): 
        print("swiped diag up-right") 
       case (-1,-1): 
        print("swiped diag down-left") 
       case (-1,1): 
        print("swiped diag up-left") 
       case (1,-1): 
        print("swiped diag down-right") 
       default: 
        swiped = false 
        break 
       } 
      } 
     } 
    } 
    start = nil 
    if !swiped { 
     // Process non-swipes (taps, etc.) 
     print("not a swipe") 
    } 
}