嘿,伙计们,我从视图中尝试removeGestureRecognizer:
时遇到了问题,
我想要做的是doubleTap
其中一个imageView
,并删除点击的imageView
的singleTap手势,而不删除其他imageView
的singleTap手势。
这是我如何生成视图,手势和机制:
·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]);
}
但是我写的这些行找不到与双击图像视图相连的singleTap手势。
当NSLog .gestureRecognizers count
时,它仍为2,它删除的是最后一个imageView
的singleTap手势,它变为1,这是正确的。
我无法找到第一个和第二个,找到它们的任何想法?谢谢你阅读:)
答案 0 :(得分: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];
}
}
}
}