iOS中UIButton实现双击操作

我们在日常的开发过程中,一个UIButton可能需要承载许多的操作,用得最多的当然是单击的操作,但是有的时候,我们需要使用双击操作,而apple并没有提供封装的方法,所以,我们就自己来构建一个双击的操作示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
UIButton * tabButton = [UIButton buttonWithType:UIButtonTypeCustom];

// 点击操作
[tabButton addTarget:self action:@selector(tabButtonTapped:forEvent:) forControlEvents:UIControlEventTouchDown];
// 双击操作
[tabButton addTarget:self action:@selector(repeatBtnTapped:forEvent:) forControlEvents:UIControlEventTouchDownRepeat];

- (void)tabButtonTapped:(UIButton *)sender forEvent:(UIEvent *)event {
[self performSelector:@selector(tabButtonTap:) withObject:sender afterDelay:0.2];
}

- (void)tabButtonTap:(UIButton *)sender {
NSLog(@"单击操作");
}

- (void)repeatBtnTapped:(UIButton *)sender forEvent:(UIEvent *)event {

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tabButtonTap:) object:sender];

NSLog(@"双击操作");
}
文章目录
,