手势识别器无法识别通过UIImageView创建的视图

时间:2013-04-04 10:28:42

标签: iphone objective-c uigesturerecognizer redaction

我有一个带有敲击手势的UIImageview,这样任何点击它都会创建一个带有白色背景的矩形视图。我可以在imageview上移动任何地方。像这样我可以在该imageview上创建无限的视图。

现在我的问题是我想为新创建的视图添加双击手势,但是当我点击其中任何一个视图时,就会在该视图上创建一个新视图。

但我想要的是,如果我点击一个视图,那么就不能在该视图上创建新视图。 目前,即使我在UIImageView上创建了一个新视图,gestureRecognizer也能识别UIImageView。

2 个答案:

答案 0 :(得分:1)

ImageView的,

UIImageView  *imageView = [[UIImageView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(createNewView)];
[tapRecognizer setNumberOfTapsRequired:1]; // allow to create new view

UITapGestureRecognizer * tapRecognizerToFail = [[UITapGestureRecognizer alloc]init];
[tapRecognizer setNumberOfTapsRequired:2]; // to disallow more than 1 tap.

[tapRecognizer requireGestureRecognizerToFail:tapRecognizerToFail];
 [imageView setGestureRecognizers:[NSArray arrayWithObjects:tapRecognizer,tapRecognizerToFail,nil]];

//通过ImageView创建新视图

-(void)createNewView
{
    UIView  * view = [[UIView alloc]init];
    UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(newViewAction)];
    [tapRecognizer setNumberOfTapsRequired:2];
    [view addGestureRecognizer:tapRecognizer];

    [imageView addSubview:view];
}

//在新创建的视图上双击操作

-(void)newViewAction
{
 // goes here
}

答案 1 :(得分:0)

您可以使用requireGestureRecognizerToFail:声明两个识别器之间的依赖关系。

[secondaryGesture requireGestureRecognizerToFail:primaryGesture];