iphoneobjective-cuigesturerecognizerredaction

Gesture recognizer does not recognise a view which is created over an UIImageView


I have an UIImageview with tap gesture on it, such that any tap over it creates a rectangular view with white background. Which I can move any where over imageview. Like this I can create infinite views over that imageview.

Now my problem is that I want to add double tap gestures to newly created views but when I click on any of those views then a new view is created over that view.

But what I want is that if I tap on a view then no new view must be created over that view. Currently gestureRecognizer recognises UIImageView even if I have created a new view over UIImageView.


Solution

  • 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]];
    

    // New View Creation over 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];
    }
    

    // Double tap action over newly created view

    -(void)newViewAction
    {
     // goes here
    }