Sprite Kit - 如何延迟 touchesBegan
Sprite Kit - How to delay touchesBegan
我的应用程序的控件很简单。
- 触摸屏幕左侧调用一个函数。
- 触摸右侧调用第二个函数。 T
- 同时触摸两侧调用第三个功能。
问题是,除非您同时触摸屏幕的右侧和左侧,否则将调用先触摸哪一侧的函数。我希望能够等待几分之一秒,看看用户是否会在允许调用左侧或右侧功能之前触摸屏幕的两侧。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInView(nil)
if location.x < self.size.width / 2 {
touchingLeft = true
}
if location.x > self.size.width / 2 {
touchingRight = true
}
if touchingRight && touchingLeft {
ship.fireMain()
//Wait for 1/10 of a second...
} else if touchingLeft && !touchingRight {
ship.fireLeft()
} else if !touchingLeft && touchingRight {
ship.fireRight()
}
}
}
要检测左、右和左+右触摸,只需在短暂延迟后执行触摸测试即可。这是一个如何做到这一点的例子。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInView(nil)
if !touchingLeft && !touchingRight {
// Adjust the delay as needed
let delay = SKAction.waitForDuration(0.01)
let test = SKAction.runBlock {
// Avoid retain cycle
[weak self] in
self?.testTouches()
}
let delayAndTest = SKAction.sequence([delay, test])
runAction(delayAndTest)
}
if location.x < self.size.width / 2 {
touchingLeft = true
}
else if location.x > self.size.width / 2 {
touchingRight = true
}
}
}
func testTouches() {
if touchingLeft && touchingRight {
print("left+right")
}
else if touchingRight {
print("right")
}
else {
print("left")
}
touchingRight = false
touchingLeft = false
}
我的应用程序的控件很简单。
- 触摸屏幕左侧调用一个函数。
- 触摸右侧调用第二个函数。 T
- 同时触摸两侧调用第三个功能。
问题是,除非您同时触摸屏幕的右侧和左侧,否则将调用先触摸哪一侧的函数。我希望能够等待几分之一秒,看看用户是否会在允许调用左侧或右侧功能之前触摸屏幕的两侧。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInView(nil)
if location.x < self.size.width / 2 {
touchingLeft = true
}
if location.x > self.size.width / 2 {
touchingRight = true
}
if touchingRight && touchingLeft {
ship.fireMain()
//Wait for 1/10 of a second...
} else if touchingLeft && !touchingRight {
ship.fireLeft()
} else if !touchingLeft && touchingRight {
ship.fireRight()
}
}
}
要检测左、右和左+右触摸,只需在短暂延迟后执行触摸测试即可。这是一个如何做到这一点的例子。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInView(nil)
if !touchingLeft && !touchingRight {
// Adjust the delay as needed
let delay = SKAction.waitForDuration(0.01)
let test = SKAction.runBlock {
// Avoid retain cycle
[weak self] in
self?.testTouches()
}
let delayAndTest = SKAction.sequence([delay, test])
runAction(delayAndTest)
}
if location.x < self.size.width / 2 {
touchingLeft = true
}
else if location.x > self.size.width / 2 {
touchingRight = true
}
}
}
func testTouches() {
if touchingLeft && touchingRight {
print("left+right")
}
else if touchingRight {
print("right")
}
else {
print("left")
}
touchingRight = false
touchingLeft = false
}