iPhone - 嵌套在 UIView 中的 UIbutton 不响应该视图动画后的触摸

发布于 2024-12-12 07:22:04 字数 226 浏览 0 评论 0原文

我有一个 UIView(视图 B),上面有一个 UIButton。

我将此视图 B 添加到主视图边界之外的区域中的主视图(视图 A),然后使用 UIView 动画对其进行动画处理。

UIView 动画完成后,视图 B 现在位于视图 A 之上,因此按钮可见,按钮不会响应触摸...我以前从未见过这种情况,但这是我使用新版本制作的第一个应用程序iOS(iOS 5)。有什么想法吗?

提前致谢。

I have a UIView (view B) with a UIButton on it.

I add this view B to my main view (view A) in an area outside of the bounds of the main view and I than animate it in with a UIView animation.

After the UIView animation is complete and View B is now on top of View A so the button is visible, the button does not respond to touches... I have never seen this before but this is the first app I am doing with the new iOS (iOS 5). Any ideas?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

海风掠过北极光 2024-12-19 07:22:05

这就是你所描述的情况吗?因为它看起来工作得很好。您是否检查了 UIView 上的 userInteractionEnabled 是否设置为 YES?

- (void)buttonPressed:(UIButton*)button
{
    NSLog(@"button pressed");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 100, 100)];
    view.backgroundColor = [UIColor blackColor];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 10, 100, 20);
    [view addSubview:button];

    [self.view addSubview:view];

    [UIView animateWithDuration:0.5 animations:^{
        view.transform = CGAffineTransformMakeTranslation(0, 100);
    }];

    [view release];
}

Is this the situation you are describing? Because it seems to work fine. Did you check if userInteractionEnabled is set to YES on the UIView?

- (void)buttonPressed:(UIButton*)button
{
    NSLog(@"button pressed");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 100, 100)];
    view.backgroundColor = [UIColor blackColor];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 10, 100, 20);
    [view addSubview:button];

    [self.view addSubview:view];

    [UIView animateWithDuration:0.5 animations:^{
        view.transform = CGAffineTransformMakeTranslation(0, 100);
    }];

    [view release];
}
∞觅青森が 2024-12-19 07:22:05

我不确定这个问题是否得到了回答,所以我尝试一下,只是为了记录:

检查按钮是否不在任何超级视图的框架之外。

我发现像这样放在外面的按钮可能不起作用。仔细想想,这很奇怪。当我使用从下面开始动画的按钮制作视图时,我遇到了这个问题。在此之下,我有一个灰色的可触摸视图以允许取消。唯一的问题是 1)对于灰色区域,我使用了父视图本身,2)当子视图动画就位时,我让这个灰色区域缩小......因此,按钮变成了外部,并且不起作用。

解决方案是保持视图完整大小,然后添加另一个作为灰色,或者使第一个变为灰色而不缩小(我想避免这种情况的唯一原因是它会生成一堆半透明层,这不是最佳的) 。然后是上面有按钮的视图。 :)

希望这有帮助。

I'm not sure that this was answered so I give it a shot, just for the record:

Check that the buttons are not outside the frame of any superview.

I found that a button placed outside like this may not work. Come to think about it, this is weird. I ran into this when I was making a view with buttons that animates in from below. Underneath this I have a gray touchable view to allow cancel. Only problem was that 1) for the gray area I used the parent view itself and 2) I let this gray area shrink as the subview animated in place ... so as a result, the buttons became outside, and it didn't work.

Solution was to leave the view full size and either add another as the gray, or make the first one gray and not shrunk (the only reason I wanted to avoid this was that it would make a pile of half transparent layers which is not optimal). Then the view with buttons on top of that. :)

Hope this helps.

缱倦旧时光 2024-12-19 07:22:05

如果您通过编程创建了按钮,那么您必须这样做:-

myButton.userInteractionEnabled =YES;

希望它有帮助...:)

If you have created the Button through programming then you have to do :-

myButton.userInteractionEnabled =YES;

Hope it Helps... :)

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