1

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.

Monolo
  • 18,205
  • 17
  • 69
  • 103
Pankaj Wadhwa
  • 3,073
  • 3
  • 28
  • 37

2 Answers2

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

// 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
}
Vedchi
  • 1,200
  • 6
  • 14
0

You can use requireGestureRecognizerToFail: to declare a dependency between your two recognizers.

[secondaryGesture requireGestureRecognizerToFail:primaryGesture];
Eli Ganem
  • 1,479
  • 1
  • 13
  • 26