UIGestureRecognizer

为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer,利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势,要实现<UIGestureRecognizerDelegate>

手势状态:

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态

     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成

    UIGestureRecognizerStateChanged,    // 手势状态发生转变

     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)

     UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态

     UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态

     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded };

常用的六种手势:

  1. 轻击手势(TapGestureRecognizer)
  2. 轻扫手势 (SwipeGestureRecognizer)
  3. 长按手势(LongPressGestureRecognizer)
  4. 拖动手势(PanGestureRecognizer)
  5. 捏合手势(PinchGestureRecognizer)
  6. 旋转手势(RotationGestureRecognizer)

1. 轻击手势(TapGestureRecognizer)

) 初始化轻击手势识别器对象

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

 ) 设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = ;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = ;
//添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap]; ) 监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];
) 代理
tap.delegate = self;
//轻击手势触发方法
-(void) tapIconView:(UITapGestureRecognizer *)sender
{ NSLog(@"我用两只手指敲击屏幕成功了!");
}
#pragma mark - 代理方法 /** * 当点击view的时候,会先调用这个方法 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint pos = [touch locationInView:touch.view]; if (pos.x <= self.iconView.frame.size.width * 0.5) { return YES; }
return NO; }

2. 轻扫手势 (SwipeGestureRecognizer)

  1. 初始化轻扫手势识别器对象
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //设置轻扫的向右方向 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右 [self.view addGestureRecognizer:swipeGesture]; UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //设置轻扫的方向 swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左 [self.view addGestureRecognizer:swipeGestureLeft]; //轻扫手势触发方法 -(void)swipeGesture:(id)sender { UISwipeGestureRecognizer *swipe = sender; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //向左轻扫 } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { //向右轻扫 } } 

3. 长按手势(LongPressGestureRecognizer

初始化轻扫手势识别器对象

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];

//设置长按时间

longPressGesture.minimumPressDuration = 0.5;

[self.view addGestureRecognizer:longPressGesture];

//长按手势触发方法

-(void)longPressGesture:(id)sender

{

 UILongPressGestureRecognizer *longPress = sender;

 if (longPress.state == UIGestureRecognizerStateBegan)

 {

 //your code

 }

}

说明:长按手势的常用状态如下

开始:UIGestureRecognizerStateBegan

改变:UIGestureRecognizerStateChanged

结束:UIGestureRecognizerStateEnded

取消:UIGestureRecognizerStateCancelled

失败:UIGestureRecognizerStateFailed

4. 拖动手势(PanGestureRecognizer)

初始化轻扫手势识别器对象

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

[self.view addGestureRecognizer:panGesture];

//拖动手势触发方法

-(void) panGesture:(id)sender

{
UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //your code }

5. 捏合手势(PinchGestureRecognizer)

初始化轻扫手势识别器对象

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

[self.view addGestureRecognizer:panGesture];
//拖动手势触发方法 -(void) panGesture:(id)sender { UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //your code
}

6. 旋转手势(RotationGestureRecognizer)

初始化轻扫手势识别器对象

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];

[self.view addGestureRecognizer:rotationGesture];

//旋转手势触发方法

-(void)rotationGesture:(id)sender

{

UIRotationGestureRecognizer *gesture = sender;

if (gesture.state==UIGestureRecognizerStateChanged)

{

_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);

}

if(gesture.state==UIGestureRecognizerStateEnded)

{

[UIView animateWithDuration: animations:^{

_imageView.transform=CGAffineTransformIdentity;//取消形变

}];

}

}

iOS学习之手势的更多相关文章

  1. iOS学习路线图

    一.iOS学习路线图   二.iOS学习路线图--视频篇       阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天       学习后目标:    ...

  2. ios学习之UISwipeGestureRecognizer手势识别

    ios学习之UISwipeGestureRecognizer手势识别   本文部分转自俺是一个瓜娃!!!的博客UISwipeGestureRecognizer ---手指动作,转载过来仅是为了自己查询 ...

  3. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  4. iOS开发摇动手势实现详解

    1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势.系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你快速摇动设备三次,那只会收到一个摇动事件. ...

  5. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  6. ios学习-delegate、传值、跳转页面

    ios学习-delegate.传值.跳转页面     1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...

  7. iOS学习——UIView的研究

    在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...

  8. 2015最新iOS学习线路图

    iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...

  9. iOS学习——iOS原生实现二维码扫描

    最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时后台会自动上传用户的当前地 ...

随机推荐

  1. Python—模块

    一.模块 模块,是用一堆代码实现了某个功能的代码集合,模块分为三种:自定义模块(自己定义).内置模块(python自带).开源模块 导入模块 (1).导入一个py文件,解释器解释该py文件 (2).导 ...

  2. Python基础第二篇

    一.三元运算 if True: name='a' else: name='b' #上面的代码用三元运算表示: name="a" if True else "b" ...

  3. mysql查询优化器为什么可能会选择错误的执行计划

    有可能导致mysql优化器选择错误的执行计划的原因如下: A:统计信息不准确,mysql依赖存储引擎为其提供的统计信息来评估成本,然而有的存储引擎提供的信息是准确的,有的引擎提供的可能就偏差很大,如: ...

  4. js中==, !==, === ,!=的区别

    在讨论比较符的时候我们先要来讨论哈js的类型,这样有助于我们从本质上了解原理. 下面是我找的资料加上个人的总结: js中有5种数据类型:Undefined.Null.Boolean.Number和St ...

  5. BFS AOJ 0558 Chess

    AOJ 0558 Chess http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0558    在H * W的地图上有N个奶酪工厂,每个 ...

  6. shell & dialog

    最近使用dialog写图形自动化shell脚本,  功能很强大,功能不是非常多但是足够用.想写一篇linux下dialog的使用方法,虽然命令不多,但是写起来也需要下很大功夫,而且不一定写得更好,在网 ...

  7. 【设计模式】常用de单例模式

    > 单例模式 单例模式,是常见的设计模式之一,一般来说,是程序员较早接触的模式之一.嘻嘻,包括我~~~ > 分类 一般来说,分两种: 饿汉模式.非常饿嘛,一上来就加载了,所以,就是非延迟加 ...

  8. Cheatsheet: 2015 12.01 ~ 12.31

    Mobile Setting Up the Development Environment iOS From Scratch With Swift: How to Test an iOS Applic ...

  9. 《BI那点儿事》数据流转换——透视

    这个和T-SQL中的PIVOT和UNPIVOT的作用是一样的.数据透视转换可以将数据规范或使它在报表中更具可读性. 通过透视列值的输入数据,透视转换将规范的数据集转变成规范程度稍低.但更为简洁的版本. ...

  10. javacsript Numnber 对象

    引子: initUppercent = (uploadedSize / file.size * 100).toFixed(2) + '%'; 一.javaScript Number对象 ------- ...