当前位置:   article > 正文

触摸手势识别2_触摸手势库c语言

触摸手势库c语言

  1. // 1. 点按手势
  2. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
  3. // 点按次数,例如双击2
  4. // 注意:在iOS中最好少用双击,如果一定要用,就一定要有一个图形化的界面告知用户可以双击
  5. [tap setNumberOfTapsRequired:2];
  6. // 用几根手指点按
  7. [tap setNumberOfTouchesRequired:1];
  8. [imageView addGestureRecognizer:tap];
  9. // 2. 长按手势 Long Press
  10. UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
  11. [imageView addGestureRecognizer:longTap];
  12. // 3. 拖动手势 Pan
  13. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
  14. [imageView addGestureRecognizer:pan];
  15. // 4. 旋转手势 Rotation
  16. UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
  17. [imageView addGestureRecognizer:rotation];
  18. // 5. 缩放(捏合)手势 Pinch
  19. UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
  20. [imageView addGestureRecognizer:pinch];
  21. // 6. 轻扫手势 Swipe
  22. /*
  23. 手指在屏幕上扫动,和拖动手势的区别在于,手指离开屏幕才会触发监听方法
  24. 1> 手指可以上、下、左、右四个方向轻轻扫动,如果没有指定方向,默认都是向右扫动
  25. 2> 在设置轻扫手势时,通常需要将手势识别添加到父视图上监听
  26. 3> 在监听方法中,注意不要使用recognizaer.view,因为手势监听的是父视图,而要处理的视图通常是其他的视图
  27. 如果要使用多个方向的轻扫手势,需要指定多个轻扫手势,通常只需指定左右两个方向即可。
  28. 因为iOS用户大多不习惯上下的轻扫动作
  29. */
  30. UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
  31. [swipe1 setDirection:UISwipeGestureRecognizerDirectionUp];
  32. [self.view addGestureRecognizer:swipe1];
  33. UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
  34. [swipe2 setDirection:UISwipeGestureRecognizerDirectionDown];
  35. [self.view addGestureRecognizer:swipe2];
  36. UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
  37. [swipe3 setDirection:UISwipeGestureRecognizerDirectionLeft];
  38. [self.view addGestureRecognizer:swipe3];
  39. UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
  40. [swipe4 setDirection:UISwipeGestureRecognizerDirectionRight];
  41. [self.view addGestureRecognizer:swipe4];

  1. #pragma mark - 手势监听方法
  2. #pragma mark 轻扫手势
  3. // 尽管轻扫手势也是连续手势,但是该手势是在手指离开屏幕才会被触发的
  4. // 因此,在编写代码时,不需要处理手势的状态
  5. - (void)swipe:(UISwipeGestureRecognizer *)recognizer
  6. {
  7. // 让图片朝手指不同的方向飞出屏幕,然后再复位
  8. CGRect frame = kImageInitFrame;
  9. if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) {
  10. frame.origin.y -= 400;
  11. } else if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) {
  12. frame.origin.y += 400;
  13. } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) {
  14. frame.origin.x -= 300;
  15. } else {
  16. frame.origin.x += 300;
  17. }
  18. [UIView animateWithDuration:1.0 animations:^{
  19. [self.imageView setFrame:frame];
  20. } completion:^(BOOL finished) {
  21. [UIView animateWithDuration:1.0f animations:^{
  22. [self.imageView setFrame:kImageInitFrame];
  23. }];
  24. }];
  25. }
  26. #pragma mark 捏合手势
  27. - (void)pinch:(UIPinchGestureRecognizer *)recognizer
  28. {
  29. /**
  30. 变化过程中,放大缩小
  31. 结束后,恢复
  32. */
  33. if (UIGestureRecognizerStateChanged == recognizer.state) {
  34. [recognizer.view setTransform:CGAffineTransformMakeScale(recognizer.scale, recognizer.scale)];
  35. } else if (UIGestureRecognizerStateEnded == recognizer.state) {
  36. [UIView animateWithDuration:0.5f animations:^{
  37. // 恢复初始位置,清除所有的形变效果
  38. [recognizer.view setTransform:CGAffineTransformIdentity];
  39. }];
  40. }
  41. }
  42. #pragma mark 旋转手势,至少两根手指
  43. - (void)rotation:(UIRotationGestureRecognizer *)recognizer
  44. {
  45. // recognizer中的rotation属性是基于图片的初始旋转弧度的
  46. /**
  47. 变化过程中,旋转
  48. 结束后,恢复
  49. */
  50. if (UIGestureRecognizerStateChanged == recognizer.state) {
  51. [recognizer.view setTransform:CGAffineTransformMakeRotation(recognizer.rotation)];
  52. /**
  53. 累加的形变
  54. */
  55. // [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation)];
  56. // // 把手势识别的rotation设置为0,在下一次触发时,以当前的旋转角度为基准继续旋转
  57. // recognizer.rotation = 0;
  58. } else if (UIGestureRecognizerStateEnded == recognizer.state) {
  59. [UIView animateWithDuration:0.5f animations:^{
  60. // 恢复初始位置,清除所有的形变效果
  61. [recognizer.view setTransform:CGAffineTransformIdentity];
  62. }];
  63. }
  64. }
  65. #pragma mark 拖动手势
  66. - (void)pan:(UIPanGestureRecognizer *)recognizer
  67. {
  68. NSLog(@"拖动");
  69. /*
  70. 拖动手势结束后,以动画的方式回到初始位置
  71. */
  72. // 在手势状态处于“变化”时处理图片的移动
  73. // UIGestureRecognizerStateChanged类似于touchesMoved方法,会不断地被调用
  74. if (UIGestureRecognizerStateChanged == recognizer.state) {
  75. // translationInView,判断在父视图中平移的位置,平移的偏移量始终以视图的初始位置为基础
  76. CGPoint deltaPoint = [recognizer translationInView:self.view];
  77. // 用形变参数来改变图像的位置
  78. [recognizer.view setTransform:CGAffineTransformMakeTranslation(deltaPoint.x, deltaPoint.y)];
  79. // CGRect targetRect = kImageInitFrame;
  80. // targetRect.origin.x += deltaPoint.x;
  81. // targetRect.origin.y += deltaPoint.y;
  82. //
  83. // [recognizer.view setFrame:targetRect];
  84. } else if (UIGestureRecognizerStateEnded == recognizer.state) {
  85. [UIView animateWithDuration:0.5f animations:^{
  86. // 将图片复位
  87. // [recognizer.view setFrame:kImageInitFrame];
  88. [recognizer.view setTransform:CGAffineTransformIdentity];
  89. }];
  90. }
  91. }
  92. #pragma mark 长按手势
  93. - (void)longTap:(UILongPressGestureRecognizer *)recognizer
  94. {
  95. NSLog(@"长按");
  96. /*
  97. 旋转半圈的动画,动画完成后恢复
  98. 长按手势属于连续手势,是需要处理状态
  99. 因为长按通常只有一根手指,因此在Began状态下,长按手势就已经被识别了
  100. 针对长按的处理,最好放在UIGestureRecognizerStateBegan状态中实现
  101. */
  102. if (UIGestureRecognizerStateBegan == recognizer.state) {
  103. NSLog(@"长按");
  104. [UIView animateWithDuration:1.0f animations:^{
  105. [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];
  106. } completion:^(BOOL finished) {
  107. // CGAffineTransformIdentity将视图的形变复原(平移、缩放、旋转)
  108. [recognizer.view setTransform:CGAffineTransformIdentity];
  109. }];
  110. } else if (UIGestureRecognizerStateEnded == recognizer.state) {
  111. // [UIView animateWithDuration:1.0f animations:^{
  112. // [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];
  113. // } completion:^(BOOL finished) {
  114. // // CGAffineTransformIdentity将视图的形变复原(平移、缩放、旋转)
  115. // [recognizer.view setTransform:CGAffineTransformIdentity];
  116. // }];
  117. }
  118. }
  119. #pragma mark 点按手势
  120. - (void)tap:(UITapGestureRecognizer *)recognizer
  121. {
  122. NSLog(@"点我了");
  123. // 做一个动画效果
  124. // 向下移出屏幕,完成后再重新返回初始位置
  125. // recognizer.view 就是识别到手势的视图,也就是手势绑定到得视图
  126. CGRect initFrame = recognizer.view.frame;
  127. CGRect targetFrame = recognizer.view.frame;
  128. targetFrame.origin.y += 360;
  129. [UIView animateWithDuration:1.0f animations:^{
  130. // 自动反向重复动画
  131. [UIView setAnimationRepeatAutoreverses:YES];
  132. // 设置动画重复次数
  133. [UIView setAnimationRepeatCount:2];
  134. [recognizer.view setFrame:targetFrame];
  135. } completion:^(BOOL finished) {
  136. [recognizer.view setFrame:initFrame];
  137. }];
  138. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/317233
推荐阅读
相关标签
  

闽ICP备14008679号