目标是只要在按钮上保持触摸就每0.5秒执行一次动作,并在触摸结束时停止该动作。根据SO中其他帖子的建议,我采用了一种利用布尔值的策略,该布尔值在touchDown上设置为true,在touchUp上设置为false。但是,似乎touchUp func要么没有设置布尔值,要么touchDown忽略它,因为当我将取景器从按钮上抬起时,动作继续。
这是我的触摸代码:
// MARK: - Touches
func touchDown(atPoint pos : CGPoint) {
print("Touched Down in \(purpose) down arrow")
arrowIsPressed = true
self.texture = SKTexture(imageNamed: "bottomArrowPressed.png")
adjustBlue() // Because you want one immediate action, in case there is no continuous touch
while arrowIsPressed == true {
perform(#selector(callback), with: nil, afterDelay: 1.0)
}
}
func touchUp(atPoint pos : CGPoint) {
arrowIsPressed = false
self.texture = SKTexture(imageNamed: "bottomArrow.png")
adjustCounter = 0
}
// MARK: - Touch Helpers
@objc func callback() {
self.adjustBlue()
}
func adjustBlue() {
adjustCounter += 1
print("Adjust Action \(adjustCounter)")
// eventually useful code will go here
}
请注意,它是一个小按钮,我不关心所有回合跟踪多个触摸。
我的逻辑在哪里失败了?我觉得这是一个相当直接的问题和解决方案,但我做错了什么,我无法理解什么。
更新:通过在while循环中放置一个print语句,我看到while循环被反复调用非常快,即使我在其中有一个执行语句,延迟为.5秒。不知道为什么......
我的直觉尖叫你的while循环阻塞主线程。
while不会在迭代之间延迟,而是回调的调用会延迟。循环之间没有延迟,因此主线程被阻塞,并且触摸功能无法运行。
你可以改成这个
func touchDown(atPoint pos : CGPoint) {
print("Touched Down in \(purpose) down arrow")
arrowIsPressed = true
self.texture = SKTexture(imageNamed: "bottomArrowPressed.png")
adjustBlue() // Because you want one immediate action, in case there is no continuous touch
callback()
}
func touchUp(atPoint pos : CGPoint) {
arrowIsPressed = false
self.texture = SKTexture(imageNamed: "bottomArrow.png")
adjustCounter = 0
}
// MARK: - Touch Helpers
@objc func callback() {
adjustBlue()
// loop in a non blocking way after 0.5 seconds
DispatchQueue.main.asynAfter(deadline: .now() + 0.5) {
if self.arrowIsPressed {
self.callback()
}
}
}
func adjustBlue() {
adjustCounter += 1
print("Adjust Action \(adjustCounter)")
// eventually useful code will go here
}