1

嘿,伙计们,我在尝试removeGestureRecognizer:从一个视图时遇到了一个问题,

我想要做的是doubleTap其中一个imageView,并删除点击imageView的singleTap Gesture,而不删除其他imageViews singleTap Gesture。

以下是我生成视图、手势和机制的方式:

。H

UITapGestureRecognizer *singleTap;

.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSInteger i;
    for (i = 1; i <= 3; i++) 
    {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(110, 70+80*(i-1), 100, 60);
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];

        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleMethod:)];
        doubleTap.numberOfTapsRequired = 2;

        singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleMethod:)];
        [singleTap requireGestureRecognizerToFail:doubleTap];

        [imageView addGestureRecognizer:doubleTap];
        [imageView addGestureRecognizer:singleTap];
    }
}
- (void)singleMethod: (id)sender
{
    NSLog(@"SingleTap");
}
- (void)doubleMethod: (id)sender
{
    NSLog(@"%d",[((UITapGestureRecognizer *)sender).view.gestureRecognizers count]);

    UIImageView *imageView = nil;
    NSArray *tryToFindYou = [self.view subviews];
    for (imageView in tryToFindYou) 
    {
        if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)sender).view.tag) 
        {
            [imageView removeGestureRecognizer:singleTap];
        }
    }

    NSLog(@"%d",[((UITapGestureRecognizer *)sender).view.gestureRecognizers count]);

}

但是我写的这些行无法准确找到附加到双击 imageView 的 singleTap Gesture。

NSLog的时候.gestureRecognizers count,还是2,去掉的是最后一个imageViewsingleTap Gesture,变成了1,没错。

我找不到第一个和第二个,有什么想法可以找到它们吗?谢谢你的阅读:)

4

1 回答 1

10

您应该循环浏览 UIView 类的 gestureRecognizers 属性,其中添加了手势,如下所示:

for (imageView in tryToFindYou) 
    {
        if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)sender).view.tag) 
        {
            for(UIGestureRecognizer *gesture in [imageView gestureRecognizers])
            {
               if([gesture isKindOfClass:[UITapGestureRecognizer class]])
               {
                 if (gesture.numberOfTapsRequired == 1) 
                    [imageView removeGestureRecognizer:gesture];
               }
            }
        }
    }
于 2011-01-18T07:27:43.260 回答