IOS手势操作:点击+滑动

举报
清雨小竹 发表于 2022/09/25 04:01:59 2022/09/25
【摘要】 1.手指点击事件 //单指单击UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self ...

1.手指点击事件


  
  1. //单指单击
  2. UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
  3. action:@selector(handleSingleFingerEvent:)];
  4. singleFingerOne.numberOfTouchesRequired = 1; //手指数
  5. singleFingerOne.numberOfTapsRequired = 1; //tap次数
  6. singleFingerOne.delegate = self;
  7. //单指双击
  8. UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
  9. action:@selector(handleSingleFingerEvent:)];
  10. singleFingerTwo.numberOfTouchesRequired = 1;
  11. singleFingerTwo.numberOfTapsRequired = 2;
  12. singleFingerTwo.delegate = self;
  13. //双指单击
  14. UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
  15. action:@selector(handleDoubleFingerEvent:)];
  16. doubleFingerOne.numberOfTouchesRequired = 2;
  17. doubleFingerOne.numberOfTapsRequired = 1;
  18. doubleFingerOne.delegate = self;
  19. UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
  20. action:@selector(handleDoubleFingerEvent:)];
  21. doubleFingerTwo.numberOfTouchesRequired = 2;
  22. doubleFingerTwo.numberOfTapsRequired = 2;
  23. doubleFingerTwo.delegate = self;
  24. //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
  25. [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
  26. //同理双指亦是如此
  27. [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
  28. [self.view addGestureRecognizer:singleFingerOne];
  29. [self.view addGestureRecognizer:singleFingerTwo];
  30. [self.view addGestureRecognizer:doubleFingerOne];
  31. [self.view addGestureRecognizer:doubleFingerTwo];
  32. [singleFingerOne release];
  33. [singleFingerTwo release];
  34. [doubleFingerOne release];
  35. [doubleFingerTwo release];
  36. //处理单指事件
  37. - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
  38. {
  39. if (sender.numberOfTapsRequired == 1) {
  40. //单指单击
  41. NSLog(@"单指单击");
  42. }else if(sender.numberOfTapsRequired == 2){
  43. //单指双击
  44. NSLog(@"单指双击");
  45. }
  46. }
  47. //处理双指事件
  48. - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
  49. {
  50. if (sender.numberOfTapsRequired == 1) {
  51. //双指单击
  52. NSLog(@"双指单击");
  53. }else if(sender.numberOfTapsRequired == 2){
  54. //双指双击
  55. NSLog(@"双指双击");
  56. }
  57. }


2.手指滑动事件


  
  1. UISwipeGestureRecognizer *recognizer;
  2. recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
  3. [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
  4. [[self view] addGestureRecognizer:recognizer];
  5. [recognizer release];
  6. recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
  7. [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
  8. [[self view] addGestureRecognizer:recognizer];
  9. [recognizer release];
  10. recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
  11. [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
  12. [[self view] addGestureRecognizer:recognizer];
  13. [recognizer release];
  14. recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
  15. [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
  16. [[self view] addGestureRecognizer:recognizer];
  17. [recognizer release];


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer

{

    

    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft||recognizer.direction==UISwipeGestureRecognizerDirectionDown) {

        

        NSLog(@"swipe left");

        //执行程序

        [self ReadNextPage];

    }

    

    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight||recognizer.direction == UISwipeGestureRecognizerDirectionUp) {

        

        NSLog(@"swipe right");

        //执行程序

        [self ReadLastPage];

    }

    

}


文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/8630960

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。