9

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!

Olex
  • 1,656
  • 3
  • 20
  • 38

1 Answers1

33

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!

Community
  • 1
  • 1
lmt
  • 811
  • 1
  • 11
  • 27
  • Thanks for your advice! But how it can be possible to have different behaviour in different iOS versions? Does something changed in way of user interacting with gestures in iOS 6.x? – Olex Dec 02 '12 at 16:05
  • 1
    Hey Alexey, my pleasure! It seems that in iOS 6 they have overcome this by default, but I'm not sure. Anyway did you solve the problem? Or is still happening? – lmt Dec 02 '12 at 16:42
  • Will try to implement your answer in my project and then report about results here. – Olex Dec 03 '12 at 11:04
  • 1
    Great! it seems you've solved it. If you have any other difficulties just keep me posted. Good luck! – lmt Dec 03 '12 at 16:42
  • HI Imt, I have one issue same for Gesture. i add Tap Gesture in Self.view and 1 swipe gestures on UItableview. UIButton in Header Section of Table. and it is not work for me. Please help me to solved..thank Suresh – Suresh Jagnani Apr 29 '13 at 18:13
  • Hi @Suresh Jagnani, could you provide more information of your issue? have you followed the steps of this post? – lmt May 02 '13 at 17:23