質問

I asked this question on the Stack Exchange Game Development Site about how to combine the tap and long hold gesture recognizers, and received the following answer:

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.

I agree that this is the way to do it, so I am trying to implement this and have come up with the following code to do so:

-(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
    }
}

This code works but I realized that I should call the timer code while the user is holding down on the screen, not after they finish. So is there a way to call the code in touchesEnded while the user is pressing down?

役に立ちましたか?

解決

Technically, if the user keeps his/her finger perfectly still there is no method that is called between them. In practice though, touchesMoved gets called a bunch. You should just use an NSTimer though instead of keeping track of the time yourself

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top