我试图在点击关闭按钮时关闭图像视图。但按钮不响应触摸事件。 这是我的代码:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:@"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}
答案 0 :(得分:2)
要使UIImageView处理触摸,您需要将userInteractionEnabled设置为YES,它默认为NO。 尝试:
[imageView setUserInteractionEnabled:YES];
答案 1 :(得分:2)
UIImageView userInteractionEnabled
有1个属性,默认值为NO。所以在你的代码中添加
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;
希望这有帮助。
答案 2 :(得分:0)
也许你的self.view中有另一个tag = 1的视图。请尝试编辑按钮,如下所示:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}