- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-)/, (self.view.frame.size.height-)/, , )];
_imageView.userInteractionEnabled = YES;//交互使能,允许界面交互
_imageView.image = [UIImage imageNamed:@"cat.png"];
[self.view addSubview:_imageView]; // 单击的 TapRecognizer
UITapGestureRecognizer *singleTap;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
singleTap.numberOfTapsRequired = ; //点击的次数 =1 单击 [_imageView addGestureRecognizer:singleTap];//给对象添加一个手势监测; // 双击的 TapRecognizer
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];
doubleTap.numberOfTapsRequired = ; //点击的次数 =2 双击
[_imageView addGestureRecognizer:doubleTap];//给对象添加一个手势监测; /*
1.双击手势确定监测失败才会触发单击手势的相应操作,否则双击时第一击时会响应单击事件
2.会造成单击时要判断是否是双击,调用单击会有所延时。属正常现象。
*/
[singleTap requireGestureRecognizerToFail:doubleTap]; //捏合缩放手势 Pinch
UIPinchGestureRecognizer *pinch;
pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
// [_imageView addGestureRecognizer:pinch];//添加到_imageView的时候,是要把手指放到_imageView操作
[self.view addGestureRecognizer:pinch];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作)
pinch.delegate = self; //旋转手势 Rotation
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotate:)];
[self.view addGestureRecognizer:rotateRecognizer];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作)
rotateRecognizer.delegate = self; //滑动手势 SwipeRecognizer
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSwipe:)];
[self.view addGestureRecognizer:swipeRecognizer];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作)
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;//操作为左滑
swipeRecognizer.delegate = self; //拖动手势 PanRecognizer
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[_imageView addGestureRecognizer:panRecognizer];//关键语句,添加一个手势监测;
panRecognizer.maximumNumberOfTouches = ;
panRecognizer.delegate = self; //长按手势 LongPressRecognizer
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlelongPress:)];
[_imageView addGestureRecognizer:longPressRecognizer];
longPressRecognizer.minimumPressDuration = 1.0f;//触发长按事件时间为:1.0秒
longPressRecognizer.delegate = self; } -(void)SingleTap:(UITapGestureRecognizer*)recognizer
{
//处理单击操作
NSLog(@"单击操作");
} -(void)DoubleTap:(UITapGestureRecognizer*)recognizer
{
//处理双击操作
NSLog(@"双击操作");
} - (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
{
NSLog(@"缩放操作");//处理缩放操作
//对imageview缩放
_imageView.transform = CGAffineTransformScale(_imageView.transform, recognizer.scale, recognizer.scale);
//对self.view缩放,因为recognizer是添加在self.view上的
//recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = ;
} - (void)handleRotate:(UIRotationGestureRecognizer*) recognizer
{
NSLog(@"旋转操作");//处理旋转操作
//对imageview旋转
_imageView.transform = CGAffineTransformRotate(_imageView.transform, recognizer.rotation);
//对self.view旋转,因为recognizer是添加在self.view上的
// recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = ;
} - (void)handleSwipe:(UISwipeGestureRecognizer*) recognizer
{
//处理滑动操作
if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"左滑滑动操作");
}else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight){
NSLog(@"右滑滑动操作");
}
} -(void)handlePan:(UIPanGestureRecognizer*)recognizer
{
NSLog(@"拖动操作");
//处理拖动操作,拖动是基于imageview,如果经过旋转,拖动方向也是相对imageview上下左右移动,而不是屏幕对上下左右
CGPoint translation = [recognizer translationInView:_imageView];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:_imageView];
} -(void)handlelongPress:(UILongPressGestureRecognizer*)recognizer
{
//处理长按操作,开始结束都会调用,所以长按1次会执行2次
if(recognizer.state == UIGestureRecognizerStateBegan){
NSLog(@"开始长按操作");
}else if(recognizer.state == UIGestureRecognizerStateEnded){
NSLog(@"结束长按操作");
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

UIGestureRecognizer 手势的更多相关文章

  1. 【学习总结】UIGestureRecognizer(手势识别器)

    基本知识点 : -> IOS 3.2之后 , 苹果推出了手势识别功能 ( Gesture Recognizer ) 在触摸事件处理方面 , 简化开发难度. -> UIGesture Rec ...

  2. UIGestureRecognizer 手势浅析

    目录[-] iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 一.引言 二.手势的抽象类——UIGestureRecognizer 1.统一的初始化方法 2.手势状态 ...

  3. iOS边练边学--UIGestureRecognizer手势识别器简单介绍

    iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度. 一.UIGestureRecognizer UIGestureRe ...

  4. IOS 响应者链条 and UIGestureRecognizer 手势识别器)

    一次完整的触摸事件的传递响应的过程 UIAppliction --> UIWiondw -->递归找到最适合处理事件的控件 控件调用touches方法-->判断是否实现touches ...

  5. UIGestureRecognizer手势

    常用手势: 滑动,轻点,捏合,旋转,拖拽,长按 1.滑动(快速滑动) let swipeUp = UISwipeGestureRecognizer(target: self, action: Sele ...

  6. UI中的七种手势

    // // GestureRecognizerViewController.m #import "GestureRecognizerViewController.h" #impor ...

  7. iOS_38_手势

    Pan平移手势 终于效果图: Swipe轻扫手势 LongPress长按手势 Pinch和Rotation手势 捏合(缩放)和旋转 终于效果图: 涂鸦 终于效果图: 事件分3大类:触摸.加速计.远程遥 ...

  8. UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按

    #import "RootViewController.h" @interface RootViewController (){    UIImageView *imageView ...

  9. DesignModeler GestureRecgin…

    DesignModeler : 设计模式     GestureRecginzer:手势识别 作者:韩俊强 原创版权地址:http://blog.sina.com.cn/s/blog_814ecfa9 ...

随机推荐

  1. python笔记(2)---不定长参数

    python自定义函数中有两种不定长参数, 第一种是*name:加了星号 * 的参数会以元组(tuple)的形式导入 第二种是**name:加了星号 * *的参数会以字典(dict)的形式导入 *na ...

  2. solr的一些查询语法

    以下内容来自solr中国 1.1. 首先假设我的数据里fields有:name, tel, address 预设的搜寻是name这个字段, 如果要搜寻的数据刚好就是 name 这个字段,就不需要指定搜 ...

  3. Wannafly挑战赛27 D绿魔法师

    链接Wannafly挑战赛27 D绿魔法师 一个空的可重集合\(S\),\(n\)次操作,每次操作给出\(x,k,p\),要求支持下列操作: 1.在\(S\)中加入\(x\). 2.求\[\sum_{ ...

  4. 【leetcode】1035. Uncrossed Lines

    题目如下: We write the integers of A and B (in the order they are given) on two separate horizontal line ...

  5. 027:for标签使用详解

    for标签使用详解: for...in... 标签: for...in... 类似于 Python 中的 for...in... .可以遍历列表.元组.字符串.字典等一切可以遍历的对象.示例代码如下: ...

  6. 实现粘贴WORD图片的在线编辑器

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...

  7. eclipse 启动 tomcat 报错:Server mylocalhost was unable to start within 45 seconds

    这个专门转载一篇博文也是为了讽刺一下自己二逼的程序员职业,哈哈. eclipse启动tomcat服务器报错:Server mylocalhost was unable to start within ...

  8. cordova+vue做的app解决引入cordova-plugin-splashscreen后启动先显示黑屏在显示启动页

    先上项目目录结构cordova项目结构 android platform 结构 图中用红框框起来的为主要修改文件 这篇主要的讲cordova项目引用了cordova-plugin-splashscre ...

  9. django缓存优化(二)

    一.缓存目的: 1.减小过载 2.避免重复计算 3.提高系统性能 二.如何进行缓存 三.缓存类型 四.缓存粒度分类 五.缓存的设置与使用 示例一: CACHES = { 'default': { 'B ...

  10. 如何查看MySQL数据库的版本

    如何查看MySQL数据库的版本 一.总结 一句话总结: SQL语句:select version(); 命令行:mysql -V 或 mysql --version 二.三种方法查看MySQL数据库的 ...