简体   繁体   中英

Two finger tap on UIButton

I've got a UIButton in my application called myButton . I'd like to react to a single finger tap and a two finger tap on the button differently. However, from what I understand UIButton objects are only able to detect single finger touches touchDown , touchUpInside , etc. After doing some research, it looks like I'll have to use the touchesBegan method and just check to see if both of the fingers are within myButton frame. Is there any easier way to do this? Thanks!

Yes it is! By using UITapGestureRecognizer you can do something like this

- (void)viewDidLoad {   
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]
    tap.numberOfTapsRequired = 2;
    [myButton addGestureRecognizer:tap];
    [tap release];
}
- (void)tap:(UITapGestureRecognizer *)gesture {
    NSLog(@"Tap!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM