UIButton not works in iOS 5.x , everything is fine

2019-03-09 13:44发布

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

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.

Any advices are welcome!

1条回答
霸刀☆藐视天下
2楼-- · 2019-03-09 13:58

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

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

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]]);
}

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

Hope this helps!

查看更多
登录 后发表回答