UIPanGestureRecognizer不会切换到状态“结束”状态。或“取消”如果用户在负方向上平移x和y [英] UIPanGestureRecognizer does not switch to state "End" or "Cancel" if user panned x and y in negative direction

查看:271
本文介绍了UIPanGestureRecognizer不会切换到状态“结束”状态。或“取消”如果用户在负方向上平移x和y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对UIPanGestureRecognizer有一点问题。
如果用户平移到左上角,则识别器不报告UIGestureRecognizerStateEnded
状态(表示负x和y
方向)

I've got a little problem with the UIPanGestureRecognizer. The Recognizer does not report the UIGestureRecognizerStateEnded state if the user panned to the top left (means negative x and y directions)

当用户抬起手指时,如果任何方向
为正,则状态更改为UIGestureRecognizerStateEnded,但如果两个方向均为负或零,则仅停止报告
操作。

The state changes to UIGestureRecognizerStateEnded if any direction is positive when the user lifts his finger, but it just ceases to report actions if both directions are negative or zero.

这很糟糕,因为我隐藏了一些叠加视图,只要用户
拖动一些东西并且这些视图在失败的情况下不会返回。

This is bad because i hide some overlay views as long as the user drags something around and those views do not return in failure case.

当然我可以设置一个NSTimer来自动再次显示
时间之后的叠加但我可以看到我的代码中没有明显的错误和
我希望它干净。

Of course I could setup a NSTimer to display the overlay after some time automatically again but i can see no obvious error in my code and I want it clean.

我错过了什么吗?它是Apple Bug吗?

Is there something i missed? Is it an Apple Bug?

初始化是这样的:

pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[self addGestureRecognizer:pan];
[pan release];

处理函数如下所示:

- (void)panRecognized:(UIPanGestureRecognizer *)gestureRecognizer {
    switch ([gestureRecognizer state]) {
        case UIGestureRecognizerStateBegan:
            // fade some overlaying views out
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            // fade in the overlays
            break;
        default:
            break;
    }

    // handle panning
}


推荐答案

该行


[self addGestureRecognizer:pan];

[self addGestureRecognizer:pan];

对我来说不合适。

好像你是在UIView里面创建手势识别器而不是一个 UIViewController 。因此,如果视图是dealloc,它和手势识别器都会消失。

It seems like you are creating the gesture recognizer from inside a UIView and not a UIViewController. So if the view is dealloc both it and the gesture recognizer will disappear.

最好从 UIViewController 创建手势识别器。此外, UIViewController 需要保持识别器的强大功能。

Better to create the gesture recognizer from the UIViewController. Also the UIViewController needs to keep a strong point to the recognizer.

这篇关于UIPanGestureRecognizer不会切换到状态“结束”状态。或“取消”如果用户在负方向上平移x和y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆