UIGesture 平移,滑动,长按手势

  1 //
  2 //  ViewController.m
  3 //  UIWindowsApp
  4 //
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "ViewController2.h"
 11 
 12 @interface ViewController ()
 13 
 14 @end
 15 
 16 @implementation ViewController
 17 
 18 
 19 
 20 
 21 - (void)viewDidLoad {
 22     [super viewDidLoad];
 23     
 24     UIImage* image = [UIImage imageNamed:@"1.jpg"];
 25     
 26     UIImageView* _imageView= [[UIImageView alloc]init];
 27     
 28     _imageView.image = image;
 29     
 30     _imageView.frame = CGRectMake(50, 100, 220, 300);
 31     
 32     //是否开启交互事件响应开关,默认值为NO
 33     _imageView.userInteractionEnabled = YES;
 34     
 35     [self.view addSubview:_imageView];
 36 
 37     //1.创建一个平移手势
 38     UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAct:)];
 39 
 40     //将手势添加到视图
 41     [_imageView addGestureRecognizer:pan];
 42     
 43     //将手势从视图移除
 44     [_imageView removeGestureRecognizer:pan];
 45     //2.创建一个滑动手势
 46     UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeAct:)];
 47     
 48     //设置滑动手势接收的事件的类型
 49     
 50     swipe.direction = UISwipeGestureRecognizerDirectionLeft;
 51     //UISwipeGestureRecognizerDirectionLeft;
 52 //    swipe.direction = UISwipeGestureRecognizerDirectionDown;
 53 //    swipe.direction = UISwipeGestureRecognizerDirectionRight;
 54 //    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
 55 
 56     [_imageView addGestureRecognizer:swipe];
 57 
 58     //3.创建一个长按事件
 59     
 60     UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAct:)];
 61     
 62     [_imageView addGestureRecognizer:longPress];
 63     
 64     //设置长按手势的持续时间,默认是0.5秒
 65     longPress.minimumPressDuration = 3;
 66 
 67     
 68 }
 69 -(void) longPressAct:(UILongPressGestureRecognizer*) longPress
 70 {
 71     //到达三秒,触发函数
 72     if(longPress.state == UIGestureRecognizerStateBegan)
 73     {
 74         NSLog(@"长按开始");
 75     }
 76     //手指离开屏幕,触发函数
 77     else if (longPress.state == UIGestureRecognizerStateEnded)
 78     {
 79         NSLog(@"长按结束");
 80     }
 81     
 82 }
 83 
 84 
 85 
 86 
 87 -(void) swipeAct:(UISwipeGestureRecognizer *) swipt
 88 {
 89     if(swipt.direction & UISwipeGestureRecognizerDirectionLeft)
 90     {
 91         NSLog(@"Left");
 92     }else if (swipt.direction & UISwipeGestureRecognizerDirectionRight)
 93     {
 94         NSLog(@"Right");
 95     }
 96 
 97     
 98 }
 99 
100 
101 
102 
103 -(void) panAct:(UIPanGestureRecognizer*) pan
104 {
105     NSLog(@"pan");
106     
107     //获取移动的坐标,相对于视图的坐标系
108     CGPoint pt = [pan translationInView:self.view];
109     
110     NSLog(@"x=%.2f , y=%.2f ",pt.x,pt.y);
111     
112     //获取移动时的相对速度,这个速度是指每秒钟移动的像素的值
113     CGPoint pv = [pan velocityInView:self.view];
114     
115     NSLog(@"pv.x = %.2f,pv.y = %.2f",pv.x,pv.y);
116     
117     
118 }
119 
120 
121 
122 //是否可以同时响应两个手势,yes 可以,no 不可以
123 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
124 {
125     return YES;
126 }
127 
128 
129 - (void)didReceiveMemoryWarning {
130     [super didReceiveMemoryWarning];
131     // Dispose of any resources that can be recreated.
132 }
133 
134 
135 @end
原文地址:https://www.cnblogs.com/vector11248/p/7611217.html