实现IOS单击或者双击事件
作者:网络转载 发布时间:[ 2014/8/25 11:17:27 ] 推荐标签:软件测试 iOS
提供一下三种方法参考:
方法一:
//单击事件 -(void)fun1 { NSLog(@"click1"); } //双击事件 -(void)fun2 { NSLog(@"click2"); } //单击和双击方法之一 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([[touches anyObject] tapCount] == 1) { [self performSelector:@selector(fun1) withObject:nil afterDelay:1]; } else if ([[touches anyObject] tapCount] ==2) { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fun1) object:nil]; [self performSelector:@selector(fun2) withObject:nil afterDelay:1]; } }
方法二:[线程]
int num = 0; -(void)fun1 { [NSThread sleepForTimeInterval:1]; if(num == 1) { NSLog(@"click 1"); } } -(void)fun2 { [NSThread sleepForTimeInterval:1]; if(num == 2) { NSLog(@"click 2"); } } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if([[touches anyObject] tapCount] == 1) { num = 1; NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(fun1) object:nil]; [thread start]; } else if([[touches anyObject] tapCount] == 2) { num = 2; NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(fun2) object:nil]; [thread start]; } }
方法三:[利用手势控件本身自带的方法]
原理:执行第二个方法的时候,取消第一次的方法操作
- (void)viewDidLoad { [super viewDidLoad]; //点击事件 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun1)]; //单点触摸 tap.numberOfTouchesRequired = 1; //点击几次,如果是1是单击 tap.numberOfTapsRequired = 1; [self.view addGestureRecognizer:tap]; UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun2)]; tap2.numberOfTapsRequired = 2; [self.view addGestureRecognizer:tap2]; //如果满足第二次 第一次的取消 [tap requireGestureRecognizerToFail:tap2]; }
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11