码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发--旋转、移动、缩放手势实例代码

时间:2014-07-24 12:16:18      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:style   http   java   color   os   io   width   art   

代码如下:

C代码  bubuko.com,布布扣
  1. // 添加所有的手势  
  2. - (void) addGestureRecognizerToView:(UIView *)view  
  3. {  
  4.     // 旋转手势  
  5.     UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];  
  6.     [view addGestureRecognizer:rotationGestureRecognizer];  
  7.       
  8.     // 缩放手势  
  9.     UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];  
  10.     [view addGestureRecognizer:pinchGestureRecognizer];  
  11.       
  12.     // 移动手势  
  13.     UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];  
  14.     [view addGestureRecognizer:panGestureRecognizer];  
  15. }  
  16.   
  17. // 处理旋转手势  
  18. - (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer  
  19. {  
  20.     UIView *view = rotationGestureRecognizer.view;  
  21.     if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  22.         view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);  
  23.         [rotationGestureRecognizer setRotation:0];  
  24.     }  
  25. }  
  26.   
  27. // 处理缩放手势  
  28. - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer  
  29. {  
  30.     UIView *view = pinchGestureRecognizer.view;  
  31.     if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  32.         view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);  
  33.         pinchGestureRecognizer.scale = 1;  
  34.     }  
  35. }  
  36.   
  37. // 处理拖拉手势  
  38. - (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer  
  39. {  
  40.     UIView *view = panGestureRecognizer.view;  
  41.     if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  42.         CGPoint translation = [panGestureRecognizer translationInView:view.superview];  
  43.         [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];  
  44.         [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];  
  45.     }  
  46. }  

 

别忘了

Java代码  bubuko.com,布布扣
  1. [self addGestureRecognizerToView:view];  

 

如果处理的是图片,别忘了

C代码  bubuko.com,布布扣
  1. [imageView setUserInteractionEnabled:YES];  
  2. [imageView setMultipleTouchEnabled:YES];  

ios开发--旋转、移动、缩放手势实例代码,布布扣,bubuko.com

ios开发--旋转、移动、缩放手势实例代码

标签:style   http   java   color   os   io   width   art   

原文地址:http://www.cnblogs.com/lovewx/p/3865172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!