UIButton touch up inside event not working in ios5

2019-03-27 19:40发布

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!

3条回答
甜甜的少女心
2楼-- · 2019-03-27 20:16

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];
查看更多
狗以群分
3楼-- · 2019-03-27 20:40

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>
查看更多
放我归山
4楼-- · 2019-03-27 20:40

Override your button parent view pointinside method, setting a new custom frame size that contains your button.

查看更多
登录 后发表回答