简体   繁体   中英

How to check Pan gesture Not Moved

I have view where pan a view from one location to another. I can get the location of the points when moved, but I want to do some action when pan has not moved or is idle in certain point. I Don't see any state to show pan gesture is idle. The touch event I came across is UITouchPhaseStationary nut I don't know how to implement it.

There isn't a state for that, and UITouchPhaseStationary as explained in this post is for multi-touch, and lets you know if there is a second stationary finger on the screen while another finger is moving.

However, you can implement something like this yourself. Just make a timer with a time interval set to the timeout before the touch should be considered stationary and run it when the gesture's position changes. You'll want to invalidate the timer when the gesture ends as well as when the gesture changes to reset the timer. Here's an example.

- (void)gestureDidFire:(UIPanGestureRecognizer *)gesture
{
    static NSTimer *timer = nil;

    if (gesture.state == UIGestureRecognizerStateChanged) {
        if (timer.isValid) {
            [timer invalidate];
        }

        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDidFire:) userInfo:nil repeats:NO];

    }else if (gesture.state == UIGestureRecognizerStateEnded) {
        if (timer.isValid) {
            [timer invalidate];
        }
    }
}

- (void)timerDidFire:(NSTimer *)timer
{
    NSLog(@"Stationary");
}

You could implement touchesEnded:withEvent: to determine when touch has finished. Set a flag on your panGesture method and then check that value

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if (self.hasMoved) {
       // Do something
       self.hasMoved = NO;
    }
}

Try this

    - (void)panRecognized:(UIPanGestureRecognizer *)rec
    {
    CGPoint vel = [rec velocityInView:self.view];
     if (vel.x == 0 && vel.y == 0)
     {
       // Not moved
     }
     else
              // moved
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM