gpt4 book ai didi

ios - 在touchesBegan 和touchesEnded 之间是否有调用的方法?

转载 作者:行者123 更新时间:2023-11-28 22:35:11 25 4
gpt4 key购买 nike

我问了this question在 Stack Exchange Game Development Site 上关于如何组合点击和长按手势识别器,并收到以下答案:

The way to deal with this is to set a timer once the person taps the phone. The most user friendly scenario that you'd implement would look something like this:

  1. When you detect a tap, set a timer (t = timeToRepeat)
  2. On each frame, decrease the timer by dt
  3. If the timer reaches zero, move the sprite a tile and reset the timer
  4. If the user releases their finger before the first cycle of the timer, move the sprite one tile

Obviously the amount of time that you set your timer to will determine how fast your sprite moves. There are a few variations on this theme depending on the kind of behavior you want as well. For example, you can move the sprite once immediately upon detecting a tap and ignore step #4.

我同意这是实现它的方法,所以我正在尝试实现它并提出了以下代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//This records the time when the user touches the screen
self.startTime = [NSDate date];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

NSTimeInterval temp = [self.startTime timeIntervalSinceNow];
NSTimeInterval holdTime = temp * -1;

if(holdTime < self.threshold) {
//Tap
}
else {
//Hold
}
}

这段代码有效,但我意识到我应该在用户按住屏幕时调用计时器代码,而不是在他们完成后调用。那么有没有办法在用户按下时调用 touchesEnded 中的代码?

最佳答案

从技术上讲,如果用户保持他/她的手指完全静止,则不会在它们之间调用任何方法。但在实践中,touchesMoved 被称为一堆。你应该只使用 NSTimer 而不是自己跟踪时间

关于ios - 在touchesBegan 和touchesEnded 之间是否有调用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16284837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com