添加到 UIImageView 的按钮没有响应

发布于 2024-11-03 03:06:29 字数 1856 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2024-11-10 03:06:29

默认情况下,图像视图设置为禁用用户交互。尝试imageView.userInteractionEnabled = YES;

By default, image views are set to disable user interaction. Try imageView.userInteractionEnabled = YES;.

隱形的亼 2024-11-10 03:06:29

对于 UIImageView将 userInteractionEnabled 设置为 YES

....

该属性继承自
UIView父类。本班
改变这个的默认值
属性为NO。

set userInteractionEnabled to YES

For UIImageView....

This property is inherited from the
UIView parent class. This class
changes the default value of this
property to NO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文