UIButton不适用于iOS 5.x,在iOS 6.x中一切都很好 [英] UIButton not works in iOS 5.x , everything is fine in iOS 6.x

查看:79
本文介绍了UIButton不适用于iOS 5.x,在iOS 6.x中一切都很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在主UIView上点击简单的UIButton,屏幕中心会出现附加的View(子视图)(以编程方式创建子视图)。在该子视图中,我有UIButton启动MPMoviePlayer(此代码在创建子视图的内部方法中):

By tapping simple UIButton on main UIView, additional View (subview) appears in the center of screen (subview created programmatically). On that subview I have UIButton that launches MPMoviePlayer (this code is inside method that creates subview):

 // 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;

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

This button added as subview to this view inside the same method:

[self.infoView addSubview:playButton];

在iOS模拟器6.0和带有iOS 6.0的真实设备中,一切正常,在模拟器iOS 5.0和设备中5.0我有一个非常奇怪的行为:按钮开始工作只有当我按下这个按钮的区域,当我只是点击按钮 - 它调用方法当用户点击屏幕中的任何地方时调用,就像我的按钮没有出现在屏幕上(但在视觉上)。我的目标是为5.x创建这个应用程序,所以我试着在这个奇怪的问题上找到答案。

In iOS Simulator 6.0 and real device with iOS 6.0 everything works fine, in Simulator iOS 5.0 and device with 5.0 I have a very strange behavior: button start to work only when I made drag by the area of this button, when I just clicked on button - it called method what called when user taps anywhere in the screen, like my button doesn't appear on screen (but visually it does). My goal is to make this app for 5.x, so I try to find answer on this strange issue.

欢迎任何建议!

推荐答案

您是否在infoView或其中任何子视图中添加 UITapGestureRecognizer

Are you adding a UITapGestureRecognizer to infoView or any of it subviews?

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

If that is the case, your gesture recognizer is inhibiting the UIButton action. You could use the solution provided by Kevin Ballard or cdasher on this post

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

You just need to set the view that has the UITapGestureRecognizer as the delegate of that Gesture Recognizer (UIGestureRecognizerDelegate). Then you can add the following code:

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

您的Tap Gesture识别器应如下所示:

And your Tap Gesture Recognizer should look like this:

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

希望这有帮助!

这篇关于UIButton不适用于iOS 5.x,在iOS 6.x中一切都很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆