1、点击

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];

    //设置当前需要点击的次数
[tap setNumberOfTapsRequired:];
//设置当前需要触发事件的手指数量
[tap setNumberOfTouchesRequired:];
//设置当前代理
tap.delegate=self;
[_view addGestureRecognizer:tap];
//触发方法
- (void) click{
NSLog(@"当前视图被点击了! ");
}

2、长按

UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
//设置当前长按最小的时长
longPress.minimumPressDuration=; //设置允许的移动范围
[longPress setAllowableMovement:];
[_view addGestureRecognizer:longPress];
//触发方法
- (void) longPress{
NSLog(@"长按事件触发! ");
}

3、轻扫

UISwipeGestureRecognizer * swip=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMethod)];
//往左边方向
swip.direction=UISwipeGestureRecognizerDirectionLeft ;
//往右边方向
swip.direction=UISwipeGestureRecognizerDirectionRight ;
//往上面方向
swip.direction=UISwipeGestureRecognizerDirectionUp ;
//往下面方向
swip.direction=UISwipeGestureRecognizerDirectionDown ;
[_view addGestureRecognizer:swip]; //触发方法
- (void) swipMethod{
NSLog(@"轻扫事件触发! ");
}

  如果涉及到2个以上方向的手势最好添加多个UISwipeGestureRecognizer 对象并设置不同的方向,不要通过下面方式用符号|来连接:

swip.direction=UISwipeGestureRecognizerDirectionLeft  | UISwipeGestureRecognizerDirectionRight  

4、拖动

  第一步:添加视图

_view=[[UIView alloc] initWithFrame:CGRectMake(, , , )];
[_view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_view];

  第二步:添加手势

UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(paned:)];
[_view addGestureRecognizer:pan];

  第三步:实现方法

- (void) paned:(UIPanGestureRecognizer *) pan{

    //获取移动的大小
CGPoint point= [pan translationInView:pan.view];
//更改视图的中心点坐标
CGPoint points=_view.center;
points.x+=point.x;
points.y+=point.y;
_view.center=points;
//每次都清空一下消除坐标叠加
[pan setTranslation:CGPointZero inView:pan.view];
}

5、旋转

  第一步:添加视图

_view=[[UIView alloc] initWithFrame:CGRectMake(, , , )];
[_view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_view];

  第二步:添加手势

UIRotationGestureRecognizer * roate=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[_view addGestureRecognizer:roate];
roate.delegate=self;

  第三步:实现方法

- (void) rotate:(UIRotationGestureRecognizer *) rote{
//获取当前旋转的度数
CGFloat rotation= rote.rotation;
//通过仿射变换实现旋转
_view.transform=CGAffineTransformRotate(_view.transform, rotation);
//防止旋转叠加需要清零
rote.rotation=;
}

6、捏合

  第一步:添加视图

_view=[[UIView alloc] initWithFrame:CGRectMake(, , , )];
[_view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_view];

  第二步:添加手势

UIPinchGestureRecognizer * pich=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(piches:)];
[_view addGestureRecognizer:pich];
pich.delegate=self;

  第三步:实现方法

- (void) piches:(UIPinchGestureRecognizer *) pich{
//获取比例
CGFloat scale=pich.scale;
//通过仿射变换实现缩放
_view.transform=CGAffineTransformScale(_view.transform, scale, scale);
//防止比例叠加需要置为1
pich.scale=;
}

【补充】如果需要同时响应多个手势需要重写代理方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

IOS中的手势详解的更多相关文章

  1. iOS中—触摸事件详解及使用

    iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...

  2. IOS中UITableViewCell使用详解

    IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...

  3. iOS中RSA加密详解

    先贴出代码的地址,做个说明,因为RSA加密在iOS的代码比较少,网上开源的也很少,最多的才8个星星.使用过程中发现有错误.然后我做了修正,和另一个库进行了整合,然后将其支持CocoaPod. http ...

  4. 在IOS中 NSRange类详解

    NSRange的定义 typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; NSRange是一个结构 ...

  5. iOS中 支付宝钱包详解/第三方支付 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址 ...

  6. iOS中 断点下载详解 韩俊强的博客

    布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface Vie ...

  7. iOS中 百度地图详解 韩俊强的博文

    需要准备工作按照下图引进类库 需要添加 添加的两个字符串为:NSLocationWhenInUseUsageDescription  /  NSLocationAlwaysUsageDescripti ...

  8. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

  9. iOS 单元测试之XCTest详解(一)

    iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...

随机推荐

  1. read file into shell vars

    test.ksh value=$(<rosstest.txt)echo $value

  2. 说一说Servlet的生命周期

    servlet有良好的生存期的定义,包括加载和实例化.初始化.处理请求以及服务结束.这个生存期由javax.servlet.Servlet接口的init,service和destroy方法表达. Se ...

  3. python opencv3 静态图片检测人脸

    git:https://github.com/linyi0604/Computer-Vision # coding:utf-8 import cv2 filename = "../data/ ...

  4. centOS7.4服务器 yum安装 搭建lamp环境

    // 红色加粗是linux命令 安装gcc和gcc-c++ yum -y install gcc gcc-c++ yum list httpd* 安装apche yum -y install http ...

  5. LR监控linux系统资源

    一.检查系统是否安装rpc服务 使用LR监控Linux,首先查看系统是否开启了rpc服务,其次查看Linux系统守护进程rpc.restat是否启动,该进程是必须的.可以通过命令rpcinfo -p来 ...

  6. .vs目录有什么用?

    写这篇博文的目的就是方便后来者能够在百度里轻松搜到. 反正我找了半天没找到关于.vs目录的介绍,最后还是在同事的帮助下才找到的. 参考地址:https://developercommunity.vis ...

  7. Git_分支管理

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  8. Microsoft Composition (MEF 2)

    This packages provides a version of the Managed Extensibility Framework (MEF) that is lightweight an ...

  9. mysql 连接出错 'mysqladmin flush-hosts'

    本文章 转载于: http://blog.itechol.com/space-33-do-blog-id-5670.html    求助QQ:499628121   环境说明:   内网测试服务器19 ...

  10. Microsoft office2016打开很慢解决

    (1)打开Excel(word.ppt也可以),进入空白纸张,或者随便打开或新建一个文件也行,然后点击左上角“文件”按钮,进入点击“选项”. (2)然后在“常规”选项里,拉到最下面,把“”这个选项去除 ...