我的应用程序的控件很简单。
问题是,除非您在同一时间触摸屏幕的右侧和左侧,否则将调用首先触摸的任何一侧的功能。我希望能够等待几分之一秒才能看到用户是否要在调用左侧或右侧功能之前触摸屏幕的两侧。
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()
}
}
}
答案 0 :(得分:0)
要检测左,右和左+右触摸,只需在短暂延迟后执行触摸测试。这是一个如何做到这一点的例子。
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
}