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

这样只需要简单调用

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

大功告成。

具体使用一下:

在.h文件里边定义变量

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

然后在viewDidLoad里面加上

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

这样就实现了

但是,这样是不够的。

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

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

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

  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进行缩放、旋转和移动的更多相关文章

  1. 使用手势对UIImageView进行缩放、旋转和移动(转)

    原文地址:http://blog.csdn.net/crazy_frog/article/details/8664108 // 添加所有的手势 - (void) addGestureRecognize ...

  2. 【转】iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) -- 不错不错

    原文网址:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手 ...

  3. iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)       1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...

  4. ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...

  5. UI基础:target...action设计模式,手势识别器.UIimageview

    使用target..action和delegate设计模式可以实现解耦.使代码更加优化. 手势识别器: 手势识别器:是对触摸事件做了封装,无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用, ...

  6. WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示

    原文:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略. 运行时效果图:XAML代码:// Transform.XAML< ...

  7. vue-pdf结合alloyfinger手势缩放旋转上下翻页pdf文件

    1. demo线上链接 vuepdf在线demo 2. demo图: 3. 话不多说,上代码: 安装vue-pdf插件: npm i vue-pdf 安装vue-pdf报错catch的可以看我这篇文章 ...

  8. javascript 手势缩放 旋转 拖动支持:hammer.js

    原文: https://cdn.rawgit.com/hammerjs/hammer.js/master/tests/manual/visual.html /*! Hammer.JS - v2.0.4 ...

  9. 手机端 图片的移动缩放旋转兼容touch

    //缩放var initialScale = 1;var currentScale = 1;touch.on('#target', 'pinch', function (ev) { currentSc ...

随机推荐

  1. 第六届蓝桥杯省赛 java三羊献瑞

    将文字看作一个个变量.根据一开始确定的文字的值进行暴力循环. 三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞------------------- 三 羊 生 瑞 气 (如果有对齐 ...

  2. Delphi LiveBinds组件

    Component Logo Component Name Description TBindSourceDB Is used for creating bindings to databases. ...

  3. mySQL 判断表是否存

    select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='数据库名' and `TABLE_NAME`= ...

  4. 算法实践--最长递增子序列(Longest Increasing Subsquence)

    什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...

  5. ubuntu下的Nessus插件更新

    00x1: 记录下nessus插件离线更新,免得每次度娘我Nessus是放在虚拟机里面. 00x2: nessus 插件更新地址: https://plugins.nessus.org/v2/offl ...

  6. C# 栈 、队列的概念

    栈: 也是System.Collections下的数据结构 存储依然是Object类型的对象 Stack 名字 = new Stack(); Count:实际拥有的元素个数 栈的释放顺序是先进后出(后 ...

  7. k8s学习笔记之一:kubernetes简介

    一.虚拟化技术 1.什么是虚拟化技术 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立 ...

  8. ubuntu server cloud img username password

    新安装了OpenStack Queens发现无镜像,蹦蹦跳跳的下载了ubuntu的镜像 网址https://cloud-images.ubuntu.com/ 最好你自己找你想要的,vmdk.ova.i ...

  9. Python 内置os模块的简单实用

    获取路径&目录添加文件 在自动化测试的过程,考虑到工程文件的移动或者在其他人的工作环境中运行,所以我们的路径要灵活,不能把路径写死. 推荐使用Python的内置模块OS 参照图 import ...

  10. Tomcat、TongWeb5.0、TongWeb6.0部署solr

    将solr,solr-4.7.2复制到某一路径下,比如F盘根目录. 1.tomcat中进行配置,配置如下: <Context docBase="F:/solr" reload ...