[IOS]实现IOS单击或者双击事件

提供一下三种方法参考:

方法一:

[plain]  view plain copy print ?
  1. //单击事件  
  2. -(void)fun1  
  3. {  
  4.       
  5.     NSLog(@"click1");  
  6. }  
  7. //双击事件  
  8. -(void)fun2  
  9. {  
  10.     NSLog(@"click2");  
  11. }  
  12.   
  13. //单击和双击方法之一  
  14. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  15. {  
  16.       
  17.     if ([[touches anyObject] tapCount] == 1) {  
  18.         [self performSelector:@selector(fun1) withObject:nil afterDelay:1];  
  19.     }  
  20.     else if ([[touches anyObject] tapCount] ==2)  
  21.     {  
  22.         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fun1) object:nil];  
  23.         [self performSelector:@selector(fun2) withObject:nil afterDelay:1];  
  24.     }  
  25. }  

方法二:[线程]

[plain]  view plain copy print ?
  1. int num = 0;  
  2. -(void)fun1  
  3. {  
  4.     [NSThread sleepForTimeInterval:1];  
  5.     if(num == 1)  
  6.     {  
  7.         NSLog(@"click 1");  
  8.     }  
  9. }  
  10. -(void)fun2  
  11. {  
  12.     [NSThread sleepForTimeInterval:1];  
  13.     if(num == 2)  
  14.     {  
  15.         NSLog(@"click 2");  
  16.     }  
  17. }  
  18. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  19. {  
  20.     if([[touches anyObject] tapCount] == 1)  
  21.     {  
  22.         num = 1;  
  23.         NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(fun1) object:nil];  
  24.         [thread start];  
  25.     }  
  26.     else if([[touches anyObject] tapCount] == 2)  
  27.     {  
  28.         num = 2;  
  29.         NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(fun2) object:nil];  
  30.         [thread start];  
  31.     }  
  32. }  

方法三:[利用手势控件本身自带的方法]

原理:执行第二个方法的时候,取消第一次的方法操作

[plain]  view plain copy print ?
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];     
  4.  //点击事件  
  5.     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun1)];  
  6.     //单点触摸  
  7.       tap.numberOfTouchesRequired = 1;  
  8.     //点击几次,如果是1就是单击  
  9.     tap.numberOfTapsRequired = 1;  
  10.     [self.view addGestureRecognizer:tap];  
  11.       
  12.     UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun2)];  
  13.     tap2.numberOfTapsRequired = 2;  
  14.     [self.view addGestureRecognizer:tap2];  
  15.       
  16.     //如果满足第二次 第一次的就取消  
  17.     [tap requireGestureRecognizerToFail:tap2];  
  18. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值