添加到 UIImageView 的按钮没有响应
我正在使用 UIImageView,其中有两个按钮作为子视图添加到我的应用程序中。但是,如果我尝试单击按钮,它们将没有响应。这有原因吗,或者有解决方法吗?
- (void)showPopover {
if (isClicked == NO) {
if (popover == nil) {
popover = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Popover.png"]];
[popover setFrame:CGRectMake(190, -10, 132, 75)];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(30, 30, 24, 24)];
[btn1 setBackgroundColor:[UIColor clearColor]];
[btn1 setImage:[UIImage imageNamed:@"Bookmark.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(addFav) forControlEvents:UIControlStateNormal];
[popover addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setFrame:CGRectMake(80, 30, 24, 24)];
[btn2 setBackgroundColor:[UIColor clearColor]];
[btn2 setImage:[UIImage imageNamed:@"ButtonBarReload.png"] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(doHud) forControlEvents:UIControlStateNormal];
[popover addSubview:btn2];
isClicked = YES;
}
popover.alpha = 0.0;
isClicked = YES;
[self.view.superview addSubview:popover];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
popover.alpha = 1.0;
[UIView commitAnimations];
}
else if (isClicked == YES) {
[popover setAlpha:1.0];
isClicked = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[popover setAlpha:0.0];
[popover removeFromSuperview];
[UIView commitAnimations];
popover = nil;
[popover release];
}
}
I am using a UIImageView with two buttons added as a subview in my app. However, if I try to click the buttons, they are unresponsive. Is there a reason for this, or a fix?
- (void)showPopover {
if (isClicked == NO) {
if (popover == nil) {
popover = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Popover.png"]];
[popover setFrame:CGRectMake(190, -10, 132, 75)];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(30, 30, 24, 24)];
[btn1 setBackgroundColor:[UIColor clearColor]];
[btn1 setImage:[UIImage imageNamed:@"Bookmark.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(addFav) forControlEvents:UIControlStateNormal];
[popover addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setFrame:CGRectMake(80, 30, 24, 24)];
[btn2 setBackgroundColor:[UIColor clearColor]];
[btn2 setImage:[UIImage imageNamed:@"ButtonBarReload.png"] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(doHud) forControlEvents:UIControlStateNormal];
[popover addSubview:btn2];
isClicked = YES;
}
popover.alpha = 0.0;
isClicked = YES;
[self.view.superview addSubview:popover];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
popover.alpha = 1.0;
[UIView commitAnimations];
}
else if (isClicked == YES) {
[popover setAlpha:1.0];
isClicked = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[popover setAlpha:0.0];
[popover removeFromSuperview];
[UIView commitAnimations];
popover = nil;
[popover release];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,图像视图设置为禁用用户交互。尝试
imageView.userInteractionEnabled = YES;
。By default, image views are set to disable user interaction. Try
imageView.userInteractionEnabled = YES;
.对于 UIImageView将 userInteractionEnabled 设置为 YES
....
set userInteractionEnabled to YES
For UIImageView....