iOS手势识别

  1 /**
  2  一. 手势说明
  3  
  4  0> UIGestureRecognizer     所有手势识别的父类,不允许直接使用
  5  
  6     最常用的属性:
  7     view:   发生手势的视图
  8     state:  手势当前的状态,主要用于连续手势,对于离散手势一般不使用
  9  
 10  1> UITapGestureRecognizer          点按手势(离散手势,其他手势都是连续手势)
 11  
 12     属性:
 13     numberOfTapsRequired       点击次数,单击双击
 14     numberOfTouchesRequired    手指根数
 15  
 16  2> UILongPressGestureRecognizer    长按手势
 17  
 18  3> UIPanGestureRecognizer          拖动手势
 19 
 20     方法:
 21     // 在视图中拖动的距
 22     - (CGPoint)translationInView:(UIView *)view;
 23     // 在视图中拖动的速度
 24      - (CGPoint)velocityInView:(UIView *)view;
 25  
 26  4> UIPinchGestureRecognizer        捏合手势
 27  
 28     属性:
 29     scale           比例
 30  
 31  5> UIRotationGestureRecognizer     旋转手势
 32  
 33     属性
 34     rotation        旋转角度
 35  
 36  6> UISwipeGestureRecognizer        轻扫手势,通常添加到根视图上
 37     
 38     属性
 39     numberOfTouchesRequired     参与轻扫手势的手指根数
 40     direction                   轻扫的方向
 41  
 42     提示:
 43     1) 如果要检测几个方向的轻扫,需要分别实例化几个轻扫手势
 44     2) 轻扫手势虽然是连续手势,但是不需要去处理UIGestureRecognizerStateChanged状态(因为是在手指离开屏幕后,该手势才被识别 45  46  */
 47 
 48 - (void)viewDidLoad
 49 {
 50     [super viewDidLoad];
 51     
 52     // 0. 实例化UIImageView
 53     UIImage *image = [UIImage imageNamed:@"001"];
 54     UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
 55     [imageView setFrame:kImageFrame];
 56     [self.view addSubview:imageView];
 57     [imageView setUserInteractionEnabled:YES];
 58     self.imageView = imageView;
 59     
 60     // 1. 添加点按手势
 61     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
 62     // 定义双击
 63     tap.numberOfTapsRequired = 2;
 64     [imageView addGestureRecognizer:tap];
 65     
 66     // 2. 添加长按手势
 67     UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
 68     [imageView addGestureRecognizer:longTap];
 69     
 70     // 3. 拖动手势
 71     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
 72     
 73     [imageView addGestureRecognizer:pan];
 74     
 75     // 4. 捏合手势
 76     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
 77     [imageView addGestureRecognizer:pinch];
 78     
 79     // 5. 旋转手势
 80     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
 81     [imageView addGestureRecognizer:rotation];
 82     
 83     // 6. 轻扫手势
 84     // 如果要做清扫手势,需要分别实例化4个方向的手势
 85     UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
 86     
 87     swipeUp.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
 88     
 89     [self.view addGestureRecognizer:swipeUp];
 90 }
 91 
 92 #pragma mark - 点按手势
 93 - (void)tap:(UITapGestureRecognizer *)recognizer
 94 {
 95     // 双击放大图片
 96     UIImageView *imageView = (UIImageView *)recognizer.view;
 97 
 98     [UIView animateWithDuration:0.8f animations:^{
 99         [imageView setTransform:CGAffineTransformScale(imageView.transform, 2.0, 2.0)];
100     } completion:^(BOOL finished) {
101         [UIView animateWithDuration:0.5f animations:^{
102             [imageView setTransform:CGAffineTransformScale(imageView.transform, 0.5, 0.5)];
103         }];
104     }];
105 }
106 
107 #pragma mark - 长按手势
108 - (void)longTap:(UILongPressGestureRecognizer *)recognizer
109 {
110     NSLog(@"长按");
111     // 长按旋转图片
112     // 开始长按
113     // 在形变时,iOS采取就近原则旋转,如果不能按照希望的方向旋转,可以增加一些修正值,例如0.01
114     // 直接通过形变属性,要实现一次性转一圈,比较困难,可以分两次进行
115     if (UIGestureRecognizerStateBegan == recognizer.state) {
116         [UIView animateWithDuration:0.8f animations:^{
117             [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)];
118         }];
119     } else if (UIGestureRecognizerStateEnded == recognizer.state) {
120         [UIView animateWithDuration:0.8f animations:^{
121             [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)];
122         } completion:^(BOOL finished) {
123             // 清除所有的形变属性
124             [recognizer.view setTransform:CGAffineTransformIdentity];
125         }];
126     }
127 }
128 
129 #pragma mark - 拖动手势
130 - (void)pan:(UIPanGestureRecognizer *)recognizer
131 {
132     CGPoint translation = [recognizer translationInView:self.view];
133     
134     // 让图像根据手指移动
135     if (UIGestureRecognizerStateChanged == recognizer.state) {
136         // 形变参数,有Make的时候,是相对初始位置计算的
137         // 没有Make是递增修改形变的
138         [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
139         
140         // 拖动手指中的平移距离是相对于初始位置,如果使用CGAffineTransformTranslate累加形变方法
141         // 需要在每次位移之后,重置recognizer的位移量,就是将位移量清零
142         [recognizer setTranslation:CGPointZero inView:recognizer.view];
143     }
144 }
145 
146 #pragma mark - 捏合手势
147 - (void)pinch:(UIPinchGestureRecognizer *)recognizer
148 {
149     // 调整图像的显示比例
150     if (UIGestureRecognizerStateChanged == recognizer.state) {
151         
152         // 等比例缩放
153         [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale)];
154         
155         // 在设置累加形变参数时,需要充值手势的缩放比例
156         recognizer.scale = 1.0f;
157     }
158 }
159 
160 #pragma mark - 旋转手势
161 - (void)rotate:(UIRotationGestureRecognizer *)recognizer
162 {
163     if (UIGestureRecognizerStateChanged == recognizer.state) {
164         recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
165         
166         // 重置旋转量
167         recognizer.rotation = 0.0f;
168     }
169 }
170 
171 #pragma mark - 轻扫手势
172 - (void)swipe:(UISwipeGestureRecognizer *)recognizer
173 {
174     NSLog(@"%d", recognizer.direction);
175     if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) {
176         NSLog(@"向下");
177     } else if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) {
178         NSLog(@"向上");
179     } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) {
180         NSLog(@"向左");
181     } else if (UISwipeGestureRecognizerDirectionRight == recognizer.direction) {
182         NSLog(@"向右");
183     }
184 }

 

posted @ 2014-01-02 14:08  yuan555  阅读(387)  评论(0编辑  收藏  举报