IOS开发—7种常用手势UIGestureRecognizer介绍

来源:互联网 发布:php未来10年发展 编辑:程序博客网 时间:2024/05/22 15:30

7种常用手势UIGestureRecognizer介绍

[objc] view plaincopy
  1. #import "ViewController.h"   
  2. @interface ViewController ()  
  3. {  
  4.     UITapGestureRecognizer *_tap;  
  5.     UIPanGestureRecognizer *_pan;  
  6.     UIPinchGestureRecognizer *_pinch;  
  7.     UIRotationGestureRecognizer *_rotation;  
  8.     UISwipeGestureRecognizer *_swipe;  
  9.     UILongPressGestureRecognizer *_longpress;  
  10.     UIScreenEdgePanGestureRecognizer *_edgePan;  
  11. }  
  12. @property (nonatomic, weak) IBOutlet UIView *testView;  
  13. @end  
  14.    
  15. @implementation ViewController  
  16. - (void)viewDidLoad {  
  17.     [super viewDidLoad];  
  18.     [self addTapGesture];  
  19.     [self addPanGesture];  
  20.     [self addPinchGesture];  
  21.     [self addRotationGesture];  
  22.     [self addSwipeGesture];  
  23.     [self addLongpressGesture];  
  24.     [self addEdgePanGesture];  
  25. //    手势谦让  
  26.     [self gestureHumility];  
  27. }  
  28.    
  29. #pragma mark - 手势  
  30. //单击  
  31. - (void)addTapGesture{  
  32.     _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];  
  33.     _tap.numberOfTapsRequired = 1;  
  34.     [_testView addGestureRecognizer:_tap];  
  35. }   
  36.   
  37. //拖拽  
  38. - (void)addPanGesture{  
  39.     _pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];  
  40.     _pan.minimumNumberOfTouches = 1;  
  41.     [_testView addGestureRecognizer:_pan];  
  42. }  
  43.   
  44. //捏合  
  45. - (void)addPinchGesture{  
  46.     _pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];  
  47.     [_testView addGestureRecognizer:_pinch];  
  48. }  
  49.    
  50. //旋转  
  51. - (void)addRotationGesture{  
  52.     _rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];  
  53.     [_testView addGestureRecognizer:_rotation];  
  54. }  
  55.    
  56. //轻扫  
  57. - (void)addSwipeGesture{  
  58.     _swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];  
  59.     //指定扫动方向  
  60.     _swipe.direction = UISwipeGestureRecognizerDirectionDown;  
  61.     [_testView addGestureRecognizer:_swipe];  
  62. }  
  63.    
  64. //长按  
  65. - (void)addLongpressGesture{  
  66.     _longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpressAction:)];  
  67.     _longpress.minimumPressDuration = 1.0;  
  68.     [_testView addGestureRecognizer:_longpress];  
  69. }  
  70.    
  71. //边缘滑动手势  
  72. - (void)addEdgePanGesture{  
  73.     _edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePanAction:)];  
  74.     _edgePan.edges = UIRectEdgeLeft;  
  75.     [self.view addGestureRecognizer:_edgePan];  
  76. }  
  77.    
  78. #pragma mark - 动作  
  79. //单击  
  80. - (void)tapAction:(UITapGestureRecognizer *)tap{  
  81.     NSLog(@"单击");  
  82. }  
  83.    
  84. //拖拽  
  85. - (void)panAction:(UIPanGestureRecognizer *)pan{  
  86.     NSLog(@"拖拽");  
  87.     CGPoint point = [pan translationInView:pan.view];  
  88. //    pan.view.transform =CGAffineTransformMakeTranslation(point.x, point.y);  
  89.     pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);  
  90.     [pan setTranslation:CGPointZero inView:pan.view];  
  91. }  
  92.    
  93. //捏合  
  94. - (void)pinchAction:(UIPinchGestureRecognizer *)pinch{  
  95.     NSLog(@"捏合");  
  96.     pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);  
  97.      
  98. }  
  99.    
  100. //旋转  
  101. - (void)rotationAction:(UIRotationGestureRecognizer *)rotation{  
  102.     NSLog(@"旋转");  
  103.     rotation.view.transform = CGAffineTransformMakeRotation(rotation.rotation);  
  104. }  
  105.   
  106. //轻扫  
  107. - (void)swipeAction:(UISwipeGestureRecognizer *)swipe{  
  108.     NSLog(@"向下轻扫");  
  109. }  
  110.    
  111. //长按  
  112. - (void)longpressAction:(UILongPressGestureRecognizer *)longpress{  
  113.     NSLog(@"长按");  
  114. }  
  115.    
  116. //边缘滑动手势  
  117. - (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)edgePan{  
  118.     NSLog(@"左边缘滑动");  
  119.     UIColor *random = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];  
  120.     self.view.backgroundColor = random;  
  121. }  
  122.   
  123. #pragma mark - privatemethods  
  124. - (void)gestureHumility{  
  125.     [_pan requireGestureRecognizerToFail:_swipe];  
  126. }   
  127. @end  
0 0
原创粉丝点击