使用UIGestureRecognizerStateEnded时出现意外延迟

时间:2012-10-15 08:29:12

标签: delay uipangesturerecognizer

当用户开始触摸屏幕,移动手指然后结束触摸时,我需要执行一些操作。触摸开始工作正常,也移动,但触摸结束运行延迟0.5-1秒。下面是代码:

-(id) init {    
    if (self = [super init]) {
        //Adding a listener for catching touch events and get call back to selector method 
        [self addGestureListener:@selector(gestureCallback:)];

        CCScene *scene = [CCScene node];

        [scene addChild: self];

        [[CCDirector sharedDirector] runWithScene:scene];
    }
    return self;
} 

- (UIPanGestureRecognizer *)addGestureListener:(SEL)selector {
    UIPanGestureRecognizer *recognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:selector] autorelease];
    [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:recognizer];
    return recognizer;
}

-(void) gestureCallback:(UIPanGestureRecognizer *) recognizer {

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"start");
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
        NSLog(@"moved");
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        NSLog(@"ended");
    }
}

在日志中(最后两行)我看到了:

2012-10-15 11:29:03.609 App[6169:c07] moved
2012-10-15 11:29:04.267 App[6169:c07] ended

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

看一下GestureRecognizer的delaysTouchesEnded属性。来自Apple的文档:

当此属性的值为YES(默认值)且接收器正在分析触摸事件时,窗口会暂停UITouchPhaseEnded阶段中触摸对象到附加视图的传递。如果手势识别器随后识别其手势,则取消这些触摸对象(通过touchesCancelled:withEvent:消息)。如果手势识别器无法识别其手势,则窗口在调用视图的touchesEnded:withEvent:方法时传递这些对象。将此属性设置为NO可在手势识别器分析相同触摸时将UITouchPhaseEnded中的触摸对象传递到视图。

答案 1 :(得分:0)

我只是在模拟器上遇到了这种延迟。使用物理设备时看不到相同的延迟。