使用手势对UIImageView进行缩放旋转和移动

Posted Sinner_Yun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用手势对UIImageView进行缩放旋转和移动相关的知识,希望对你有一定的参考价值。

 

[cpp] view plain copy
  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. }  


这样只需要简单调用

 

 

[cpp] view plain copy
  1. [self addGestureRecognizerToView:view];    
  2.    
  3. //如果处理的是图片,别忘了  
  4. [imageView setUserInteractionEnabled:YES];    
  5. [imageView setMultipleTouchEnabled:YES];   


大功告成。

 

 

具体使用一下:

在.h文件里边定义变量

 

[cpp] view plain copy
  1. @interface YourViewController : UIViewController<UIGestureRecognizerDelegate>  
  2. {  
  3.     CGFloat lastScale;  
  4.     CGRect oldFrame;    //保存图片原来的大小  
  5.     CGRect largeFrame;  //确定图片放大最大的程度  
  6. }  

 

 

然后在viewDidLoad里面加上

 

[cpp] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     showImgView = [[UIImageView alloc] initWithFrame:<span class="s1">CGRectMake</span>(<span class="s2">0</span>, <span class="s2">0</span>, 320, 480)];  
  6.     [showImgView setMultipleTouchEnabled:YES];  
  7.     [showImgView setUserInteractionEnabled:YES];  
  8.     [showImgView setImage:[UIImage imageNamed:@"1.jpg"]];  
  9.       
  10.     oldFrame = showImgView.frame;  
  11.     largeFrame = CGRectMake(0 - screenSize.width, 0 - screenSize.height, 3 * oldFrame.size.width, 3 * oldFrame.size.height);  
  12.       
  13.     [self addGestureRecognizerToView:showImgView];  
  14.     [self.view addSubview:showImgView];  


这样就实现了

 

 

但是,这样是不够的。

因为里边的缩放和移动等没有做相应的判断。

因为代码很简洁,所以扩展也非常方便。

 

修改了缩放的代码,增加了限制,其他的类似

 

[cpp] view plain copy
  1. // 处理缩放手势  
  2. - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer  
  3. {  
  4.     UIView *view = pinchGestureRecognizer.view;  
  5.     if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  6.         view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);  
  7.         if (showImgView.frame.size.width < oldFrame.size.width) {  
  8.             showImgView.frame = oldFrame;  
  9.             //让图片无法缩得比原图小  
  10.         }  
  11.         if (showImgView.frame.size.width > 3 * oldFrame.size.width) {  
  12.             showImgView.frame = largeFrame;  
  13.         }  
  14.         pinchGestureRecognizer.scale = 1;  
  15.     }  
  16. }  


这样就好了。保证了图片的最大和最小比例。

 

 

参考文章: http://apluck.iteye.com/blog/1781607

 

以上是关于使用手势对UIImageView进行缩放旋转和移动的主要内容,如果未能解决你的问题,请参考以下文章

如何以全分辨率从 UIImageView 获取旋转、缩放和平移的图像?

iOS 如何为旋转、缩放和将 UIView 添加到 UIIMageView 进行撤消操作

拖动、缩放和旋转多个 UIImageview

ScrollView 的子视图上的旋转手势和它的捏缩放可以一起工作吗?

给图片添加缩放旋转放大手势

ios手势识别之旋转+缩放