UIView 实现单击、双击和长按

1、添加 UITapGestureRecognizer 

// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.delaysTouchesBegan = YES;
[self.view addGestureRecognizer:doubleTapRecognizer];

// single tap
UITapGestureRecognizer *tapRecognizer =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 双击手势确定监测失败才会触发单击手势的相应操作
[tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
[self.view addGestureRecognizer:tapRecognizer];

// single tap
UILongPressGestureRecognizer *gesture =  [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
gesture.minimumPressDuration = 1.5;
gesture.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:gesture];


2、实现相关触发事件

- (void)doubleTap:(UIGestureRecognizer *) gr {
    NSLog(@"doubleTap");
}
- (void)tap:(UIGestureRecognizer *) gr {
NSLog(@"tap");
 }
- (void)longPress:(UIGestureRecognizer *) gr {
NSLog(@"longPress"); 
}

你可能感兴趣的:(UIView 实现单击、双击和长按)