[IOS]实现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]; }


==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/10450627

欢迎关注我的微博:http://weibo.com/u/2590571922



















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366412,如需转载请自行联系原作者

相关文章
|
移动开发 JavaScript weex
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
221 0
|
存储 数据处理 iOS开发
iOS开发-本地推送实现方法和数据处理方案(二)
iOS开发-本地推送实现方法和数据处理方案(二)
172 0
|
存储 数据处理 iOS开发
iOS开发-本地推送实现方法和数据处理方案(一)
iOS开发-本地推送实现方法和数据处理方案(一)
212 0
|
iOS开发
iOS开发 - 不通过import引入类名实现push或present
iOS开发 - 不通过import引入类名实现push或present
77 0
|
Android开发 iOS开发
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
361 0
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
|
存储 安全 iOS开发
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
410 0
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
|
Swift 数据安全/隐私保护 iOS开发
iOS开发 - swift通过Alamofire实现https通信
iOS开发 - swift通过Alamofire实现https通信
359 0
iOS开发 - swift通过Alamofire实现https通信
|
开发者 iOS开发
iOS开发 - 用AFNetworking实现https单向验证,双向验证
iOS开发 - 用AFNetworking实现https单向验证,双向验证
350 0
iOS开发 - 用AFNetworking实现https单向验证,双向验证
|
iOS开发
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
243 0
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
|
iOS开发
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
118 0