ios – 在Objective-C中实现singleTap和doubleTap

前端之家收集整理的这篇文章主要介绍了ios – 在Objective-C中实现singleTap和doubleTap前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将singTap和doubleTap添加到如下代码的视图中:
-(void)awakeFromNib{
    [self setUserInteractionEnabled:YES];
    //
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];
    doubleTapGesture.numberOfTapsrequired = 2;
    [self addGestureRecognizer:doubleTapGesture];

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    singleTapGesture.numberOfTapsrequired = 1;
    [self addGestureRecognizer:singleTapGesture];
}

-(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{
    [[self delegate] singleTapOnView];
}

-(void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTapGesture{
    [[self delegate] doubleTapOnView];
}

当doubleTap时,singleTap也会开火.如何在doubleTap时禁用singleTap?谢谢!

解决方法

告诉单击识别器它需要双击识别器在触发之前失败:
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];

(这实际上是UIGestureRecognizer docs for this method中的典型示例.)

原文链接:/iOS/331872.html

猜你在找的iOS相关文章