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 UIWebView
at all. If you did, your code didn't reflect that. As such you are dealing with UIImageView
and the reason is pretty straightforward as any UIImageView
object by default has its userInteractionEnabled
set to NO
. You should change it to YES
. So add this line,
circleView.userInteractionEnabled = YES;
and you aren't releasing it either. So add
[circleView release];
after you add it as a subview.
精彩评论