我正在尝试将UITapGestureRecognizer附加到我正在创建的webview,然后在用户点击webview时删除该视图。以下是我的示例代码。我究竟做错了什么?谢谢!
- (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
答案 0 :(得分:3)
我认为你根本不是在谈论UIWebView
。如果你这样做,你的代码没有反映出来。因此,您正在处理UIImageView
,原因非常简单,因为默认情况下任何UIImageView
对象都将userInteractionEnabled
设置为NO
。您应该将其更改为YES
。所以添加这一行,
circleView.userInteractionEnabled = YES;
你也没有发布它。所以添加
[circleView release];
将其添加为子视图后。