使用手势对UIImageView进行缩放、旋转和移动
- // 添加所有的手势
- - (void) addGestureRecognizerToView:(UIView *)view
- {
- // 旋转手势
- UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
- [view addGestureRecognizer:rotationGestureRecognizer];
- // 缩放手势
- UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
- [view addGestureRecognizer:pinchGestureRecognizer];
- // 移动手势
- UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
- [view addGestureRecognizer:panGestureRecognizer];
- }
- // 处理旋转手势
- - (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
- {
- UIView *view = rotationGestureRecognizer.view;
- if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
- view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
- [rotationGestureRecognizer setRotation:0];
- }
- }
- // 处理缩放手势
- - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
- {
- UIView *view = pinchGestureRecognizer.view;
- if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
- view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
- pinchGestureRecognizer.scale = 1;
- }
- }
- // 处理拖拉手势
- - (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
- {
- UIView *view = panGestureRecognizer.view;
- if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
- CGPoint translation = [panGestureRecognizer translationInView:view.superview];
- [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
- [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
- }
- }
这样只需要简单调用
- [self addGestureRecognizerToView:view];
- //如果处理的是图片,别忘了
- [imageView setUserInteractionEnabled:YES];
- [imageView setMultipleTouchEnabled:YES];
大功告成。
具体使用一下:
在.h文件里边定义变量
- @interface YourViewController : UIViewController<UIGestureRecognizerDelegate>
- {
- CGFloat lastScale;
- CGRect oldFrame; //保存图片原来的大小
- CGRect largeFrame; //确定图片放大最大的程度
- }
然后在viewDidLoad里面加上
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- showImgView = [[UIImageView alloc] initWithFrame:<span class="s1">CGRectMake</span>(<span class="s2">0</span>, <span class="s2">0</span>, 320, 480)];
- [showImgView setMultipleTouchEnabled:YES];
- [showImgView setUserInteractionEnabled:YES];
- [showImgView setImage:[UIImage imageNamed:@"1.jpg"]];
- oldFrame = showImgView.frame;
- largeFrame = CGRectMake(0 - screenSize.width, 0 - screenSize.height, 3 * oldFrame.size.width, 3 * oldFrame.size.height);
- [self addGestureRecognizerToView:showImgView];
- [self.view addSubview:showImgView];
这样就实现了
但是,这样是不够的。
因为里边的缩放和移动等没有做相应的判断。
因为代码很简洁,所以扩展也非常方便。
修改了缩放的代码,增加了限制,其他的类似
- // 处理缩放手势
- - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
- {
- UIView *view = pinchGestureRecognizer.view;
- if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
- view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
- if (showImgView.frame.size.width < oldFrame.size.width) {
- showImgView.frame = oldFrame;
- //让图片无法缩得比原图小
- }
- if (showImgView.frame.size.width > 3 * oldFrame.size.width) {
- showImgView.frame = largeFrame;
- }
- pinchGestureRecognizer.scale = 1;
- }
- }
这样就好了。保证了图片的最大和最小比例。
参考文章: http://apluck.iteye.com/blog/1781607
使用手势对UIImageView进行缩放、旋转和移动的更多相关文章
- 使用手势对UIImageView进行缩放、旋转和移动(转)
原文地址:http://blog.csdn.net/crazy_frog/article/details/8664108 // 添加所有的手势 - (void) addGestureRecognize ...
- 【转】iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) -- 不错不错
原文网址:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手 ...
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...
- UI基础:target...action设计模式,手势识别器.UIimageview
使用target..action和delegate设计模式可以实现解耦.使代码更加优化. 手势识别器: 手势识别器:是对触摸事件做了封装,无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用, ...
- WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示
原文:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略. 运行时效果图:XAML代码:// Transform.XAML< ...
- vue-pdf结合alloyfinger手势缩放旋转上下翻页pdf文件
1. demo线上链接 vuepdf在线demo 2. demo图: 3. 话不多说,上代码: 安装vue-pdf插件: npm i vue-pdf 安装vue-pdf报错catch的可以看我这篇文章 ...
- javascript 手势缩放 旋转 拖动支持:hammer.js
原文: https://cdn.rawgit.com/hammerjs/hammer.js/master/tests/manual/visual.html /*! Hammer.JS - v2.0.4 ...
- 手机端 图片的移动缩放旋转兼容touch
//缩放var initialScale = 1;var currentScale = 1;touch.on('#target', 'pinch', function (ev) { currentSc ...
随机推荐
- 第六届蓝桥杯省赛 java三羊献瑞
将文字看作一个个变量.根据一开始确定的文字的值进行暴力循环. 三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞------------------- 三 羊 生 瑞 气 (如果有对齐 ...
- Delphi LiveBinds组件
Component Logo Component Name Description TBindSourceDB Is used for creating bindings to databases. ...
- mySQL 判断表是否存
select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='数据库名' and `TABLE_NAME`= ...
- 算法实践--最长递增子序列(Longest Increasing Subsquence)
什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...
- ubuntu下的Nessus插件更新
00x1: 记录下nessus插件离线更新,免得每次度娘我Nessus是放在虚拟机里面. 00x2: nessus 插件更新地址: https://plugins.nessus.org/v2/offl ...
- C# 栈 、队列的概念
栈: 也是System.Collections下的数据结构 存储依然是Object类型的对象 Stack 名字 = new Stack(); Count:实际拥有的元素个数 栈的释放顺序是先进后出(后 ...
- k8s学习笔记之一:kubernetes简介
一.虚拟化技术 1.什么是虚拟化技术 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立 ...
- ubuntu server cloud img username password
新安装了OpenStack Queens发现无镜像,蹦蹦跳跳的下载了ubuntu的镜像 网址https://cloud-images.ubuntu.com/ 最好你自己找你想要的,vmdk.ova.i ...
- Python 内置os模块的简单实用
获取路径&目录添加文件 在自动化测试的过程,考虑到工程文件的移动或者在其他人的工作环境中运行,所以我们的路径要灵活,不能把路径写死. 推荐使用Python的内置模块OS 参照图 import ...
- Tomcat、TongWeb5.0、TongWeb6.0部署solr
将solr,solr-4.7.2复制到某一路径下,比如F盘根目录. 1.tomcat中进行配置,配置如下: <Context docBase="F:/solr" reload ...