[英]swipe gesture in touches methods in swift
因此,我已經在我的游戲中實現了手勢校正器,以檢測玩家的動作,但是發現它們沒有給我想要的結果,因此我正在嘗試通過touches方法進行輕掃手勢,同時在接觸方法。 我設法使touch功能在touches方法中起作用,但是我無法實現在touches方法中滑動的功能,而且我似乎找不到有關如何執行此操作的教程。 下面的代碼顯示了我正在嘗試實現的touches方法:
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?) {
}
}
如何使touches方法識別特定方向上的滑動手勢? 如果他們朝某個方向滑動而不是將手指從屏幕上移開,然后一次動作向后滑動回原點,我該怎么做,以便將其識別為輕拍?
這是一個如何檢測滑動手勢的示例。
首先,定義實例變量以存儲起始位置和時間
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")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.