iOS开发中,除了有关触摸的这组方法来控制用户的手指触控外,还可以用UIGestureRecognize的衍生类来进行判断,方便了开发。
UIGestureRecognize的子类类别有以下几种:

  • UITapGestureRecognizer //轻拍识别器
  • UIPinchGestureRecognizer //捏合识别器
  • UIRotationGestureRecognizer //旋转识别器
  • UISwipeGestureRecognizer //扫动识别器
  • UIPanGestureRecognizer //拖动识别器
  • UILongPressGestureRecognizer //长按识别器

下面的例子我是用UIPinchGestureRecognizer和UIRotationGestureRecognizer通过手势实现图片旋转和缩小放大。

ViewController.m中的代码如下;

#import "ViewController.h"@interface ViewController ()@property(nonatomic,strong) UIImageView *imageView;
@property(nonatomic) CGFloat lastRotation;-(void)changeImageSize:(UIPinchGestureRecognizer *)recognizer;
-(void)rotateImage:(UIRotationGestureRecognizer *)rotateRecognizer;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];//在view上添加一个ImageViewUIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Totoro"]];image.frame = CGRectMake(0, 0, 128, 128);self.imageView = image;self.imageView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);[self.view addSubview:self.imageView];//添加捏合手势识别器,changeImageSize:方法实现图片的放大与缩小UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeImageSize:)];[self.view addGestureRecognizer:pinchRecognizer];//添加旋转手势识别器,rotateImage:方法实现图片的旋转UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateImage:)];[self.view addGestureRecognizer:rotateRecognizer];}-(void)changeImageSize:(UIPinchGestureRecognizer *)recognizer
{CGRect frame = self.imageView.frame;//监听两手指滑动的距离,改变imageView的frameframe.size.width = recognizer.scale*128;frame.size.height = recognizer.scale*128;self.imageView.frame = frame;//保证imageView中心不动self.imageView.center =CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
}-(void)rotateImage:(UIRotationGestureRecognizer *)rotateRecognizer
{//改变imageView的角度,使图片旋转if ([rotateRecognizer state]==UIGestureRecognizerStateEnded) {self.lastRotation = 0.0;return;}CGAffineTransform currentTransform = self.imageView.transform;CGFloat rotation = 0.0 - (self.lastRotation - rotateRecognizer.rotation);CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);self.imageView.transform = newTransform;self.lastRotation = rotateRecognizer.rotation;
}@end

运行代码,实现效果如下:

转载于:https://www.cnblogs.com/ahtchxw/p/5466603.html

iOS开发——手势识别器(用手势实现图片旋转和缩小放大)相关推荐

  1. IOS开发设置应用程序App启动图片

    IOS开发设置应用程序App启动图片 在资源Assets 的图片区域 右键建立一个Launch Image ,如第二张图,然后把相应的启动图片拖进去. 最后选中项目,找到 Assets ,设置 启动图 ...

  2. itext实现图片等比缩小放大转pdf

    普通人:计划失败 -> 沮丧 -> 骂自己一顿 -> 制订新计划 -> 继续失败 学霸:计划失败 -> 沮丧 -> 骂自己一顿 -> 深度思考计划为啥失败 - ...

  3. iOS开发概述-12.手势处理

    1.手势识别概述 如果想监听一个view上面的触摸事件,之前的做法是 自定义一个view 实现view的touches方法,在方法内部实现具体处理代码 但是通过touches方法监听view触摸事件, ...

  4. iOS开发之禁用ios7 手势滑动返回功能

    在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: [objc] view plaincopyprint? - (void)viewDidAppear:(BOOL)animated { ...

  5. iOS开发之实现毛玻璃效果及图片模糊效果

    毛玻璃效果的实现 App设计时往往会用到一些模糊效果或者毛玻璃效果,iOS目前已提供了一些模糊API可以让我们方便使用.苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如 ...

  6. iOS开发- 相机(摄像头)获取到的图片自动旋转90度解决办法

    http://www.tuicool.com/articles/IfEZre 今天写demo的时候发现, 如果把通过相机获取到的图片,直接进行操作, 比如裁剪, 缩放, 则会把原图片向又旋转90度. ...

  7. IOS开发教程--如何使用点9图片

    事先准备一张图片: UIImage *image = [UIImage imageNamed:@"red.png"]; 在iOS 5.0之前可以这么用: NSInteger lef ...

  8. iOS开发:UIColor转成纯色图片(UIImage)

    Objective-c 版本 UIKIT_EXTERN UIImage * __nullable UIColorAsImage(UIColor * __nonnull color, CGSize si ...

  9. iOS开发--AVFoundation进行视频合成, 导出结果旋转90度问题

    创建出的视频轨道AVMutableCompositionTrack 设置setPreferredTransform __block AVMutableCompositionTrack*videoCom ...

最新文章

  1. 微软新作,ImageBERT虽好,千万级数据集才是亮点
  2. 上周热点回顾(12.18-12.24)
  3. Leetcode 83 删除排序链表中的重复元素 (每日一题 20210804)
  4. NUXT快速开始,选择Vuetify
  5. Tensorflow get_variable和Varialbe的区别
  6. 未来教育计算机二级01为法律一班,2019年3月计算机二级MSOffice提分试题及答案012...
  7. 使用javascript实现浏览器全屏
  8. 有没有一些细分IT行业,没有35岁现象
  9. Python中map的使用方法
  10. pytorch中mask操作之torch.masked_select
  11. Flask项目之手机端租房网站的实战开发(十三)
  12. cudnn下载与安装
  13. 【IoT】创业:产品生产之老化测试
  14. Java学习实战教程之mysql优化
  15. Rancher Labs获2500万美元C轮融资
  16. 在react-native fetch中 then res res.json 是什么意思
  17. 装逼神器--黑客帝国的实现效果(linux环境系统)
  18. 输入框@选人功能实现
  19. DataGridView 控件中自动生成列
  20. 订单查询系统c语言作业,C语言设计订单管理系统答题.doc

热门文章

  1. php函数的默认值,php函数指定默认值方法的小例子
  2. linux vim6,VIM6:linux终端下的英文字典--SDCV
  3. mysql数据库名称sid_数据库sid 和数据库名
  4. mpvue还在维护吗_mpvue 问题汇总(持续更新)
  5. mysql copy pending_mysql 案例 ~ 主从复制延迟之并行复制
  6. 多线程处理缓慢_华为昇腾,AI推理性能超越对手一倍:软件挖掘处理器全部潜力...
  7. java数组与字符串编程及答案_04747_Java语言程序设计(一)_第4章_数组和字符串...
  8. js 多维数组长度_C++申请与释放动态数组1(学习笔记:第6章 16)
  9. linux系统怎样连手机,Linux下手机与蓝牙的连接配置
  10. DASH流媒体MPD文件存储