嵌套在UIView中的UIbutton不响应那个视图的触摸动画



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

我将这个视图B添加到我的主视图(视图A)在主视图边界之外的区域,然后用UIView动画将其动画化。

在UIView动画完成后,视图B现在在视图A的顶部,所以按钮是可见的,按钮不响应触摸…我以前从未见过这个,但这是我用新iOS (iOS 5)做的第一个应用程序。

这是你所描述的情况吗?因为它看起来很好用。你检查userInteractionEnabled是否在UIView上被设置为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];
}

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

检查按钮不在任何父视图的框架之外。

我发现像这样放在外面的按钮可能不起作用。仔细想想,这很奇怪。当我在制作一个从下面动画按钮的视图时,我遇到了这个问题。在这个下面,我有一个灰色的可触摸视图,允许取消。唯一的问题是,1)对于灰色区域,我使用了父视图本身,2)我让这个灰色区域随着子视图动画的到位而缩小……所以,结果,按钮在外面,它不起作用。

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

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

myButton.userInteractionEnabled =YES;

希望它能帮助…:)

最新更新