I am trying to attach a UITapGestureRecognizer to a webview that i am creating and then remove that view when a user taps the webview. Below is my sample code. What am i doing wrong? Thanks!
- (void) setupPuzzle1
{
puzzleDuration--;
//Create object circle
UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red-circle.png"] highlightedImage:[UIImage imageNamed:@"red-circle.png"]];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeImage:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[circleView addGestureRecognizer:recognizer];
[recognizer release];
int x = rand()%280;
int y = rand()%420;
circleView.frame = CGRectMake(x,y,40,40);
[self.view addSubview:circleView];
if (puzzleDuration > 0)
{
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setupPuzzle1) userInfo:nil repeats:NO];
}
}
#pragma mark - UITapGestureRecognizer methods
- (void)removeImage:(UITapGestureRecognizer *)recognizer
{
NSLog(@"Remove Image");
[[recognizer view] removeFromSuperview];
}
@end
I think you weren’t talking about
UIWebViewat all. If you did, your code didn’t reflect that. As such you are dealing withUIImageViewand the reason is pretty straightforward as anyUIImageViewobject by default has itsuserInteractionEnabledset toNO. You should change it toYES. So add this line,and you aren’t releasing it either. So add
after you add it as a subview.