简体   繁体   English

如何检查平移手势未移动

[英]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. 我遇到的触摸事件是UITouchPhaseStationary坚果我不知道如何实现它。

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. 没有一个国家是什么,以及UITouchPhaseStationary在解释这个帖子是支持多点触控,并让你知道,如果有在屏幕上的第二个动的手指,而另一根手指移动。

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. 你可以实现touchesEnded:withEvent:来确定触摸何时完成。 Set a flag on your panGesture method and then check that value 在panGesture方法上设置一个标志,然后检查该值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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