Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton touch up inside event not working in ios5 while it's ok in ios6

I had a project which contains lots of UIButton, using xcode 4.5 and storyboard and ARC. after testing in ios6, everything goes fine. But in ios5, UIButton touch up inside event not working, the action not called. I tried to use touch down and it works. However, I have a lot of UIButton, I cannot change that one by one. what's more, the touch down event does give a good experience.

I used code below in many of my view controllers: in viewDidLoad:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                      action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

-(void)dismissKeyboard {
       [aTextField resignFirstResponder];
}

this might be the reason. I will check it out later. But it works in ios6. Do you know what's wrong ? Thanks very much!

like image 949
zhijie Avatar asked Dec 13 '12 06:12

zhijie


2 Answers

I had the same problem to you. That's because the touch event in iOS5 is prevented when the gestureRecognizer you registered captures the event.

There are two solutions.

One:

1) Add a new view inside your view. It should have the same level to the buttons. The priority of the buttons should be higher than the new view.

2) change the call of 'addGestureRecognizer' to the new view.

[self.newView addGestureRecognizer:tap];

Two:

1) Implement the code below. You should return NO when the point is in the buttons.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.view];

    NSLog(@"x = %.0f, y = %.0f", point.x, point.y);
    if (CGRectContainsPoint(self.testBtn.layer.frame, point))
        return NO;

    return YES;
}

ps. you should import QuartzCore.h to access layer's attributes.

#import <QuartzCore/QuartzCore.h>
like image 103
LEAD2M Avatar answered Oct 22 '22 02:10

LEAD2M


Just use the property "cancelsTouchesInView" (NO) on UITapGestureRecognizer tap

Example:

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap)];
gesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:gesture];
like image 5
JarnoV Avatar answered Oct 22 '22 00:10

JarnoV