UIButton在iOS 5.x中不起作用,在iOS 6.x中一切都很好



通过在主UIView上点击简单的UIButton,屏幕中央会出现额外的视图(子视图)(以编程方式创建的子视图)。在该子视图上,我有启动MPMoviePlayer的UIButton(此代码位于创建子视图的方法中):

 // Create play button
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self
               action:@selector(wtf)
     forControlEvents:UIControlEventTouchUpInside];
[playButton setTitle:@"" forState:UIControlEventTouchUpInside];
[playButton setImage:[UIImage imageNamed:[self.playButtons objectAtIndex:[sender tag] - 1]] forState:UIControlStateNormal];
playButton.frame = playButtonRect;
playButton.userInteractionEnabled = YES;

按钮在同一方法中作为子视图添加到此视图中:

[self.infoView addSubview:playButton];
在 iOS 模拟器 6.0 和带有 iOS

6.0 的真实设备中一切正常,在模拟器 iOS 5.0 和带有 5.0 的设备中,我有一个非常奇怪的行为:只有当我拖动此按钮的区域时,按钮才开始工作,当我刚刚单击按钮时 - 它调用了当用户点击屏幕中任意位置时调用的方法, 就像我的按钮没有出现在屏幕上一样(但在视觉上它确实如此)。我的目标是为5.x制作这个应用程序,所以我试图在这个奇怪的问题上找到答案。

欢迎任何建议!

您是否正在向 infoView 或任何子视图添加UITapGestureRecognizer

如果是这种情况,则手势识别器正在禁止 UIButton 操作。您可以在这篇文章中使用Kevin Ballard或cdasher提供的解决方案

您只需要将具有UITapGestureRecognizer的视图设置为该手势识别器的委托 (UIGestureRecognizerDelegate )。然后,您可以添加以下代码:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return ! ([touch.view isKindOfClass:[UIControl class]]);
}

您的点击手势识别器应如下所示:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];

希望这有帮助!

相关内容

最新更新