溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[IOS]實現IOS單擊或者雙擊事件

發布時間:2020-07-17 08:55:31 來源:網絡 閱讀:626 作者:蓬萊仙羽 欄目:移動開發

提供一下三種方法參考:

方法一:

//單擊事件 -(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
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女