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. In 和Exists

    1.exist,not exist一般都是与子查询一起使用. In可以与子查询一起使用,也可以直接in (a,b.....) 2.exist会针对子查询的表使用索引. not exist会对主子查询都 ...

  2. python opencv3 显示一张图片

    git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 显示一张图像 ...

  3. python opencv3 视频文件的读写

    git: https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 读取视频文件 ...

  4. 【10.26校内测试】【状压?DP】【最小生成树?搜索?】

    Solution 据说正解DP30行??? 然后写了100行的状压DP?? 疯狂特判,一算极限时间复杂度过不了aaa!! 然而还是过了....QAQ 所以我定的状态是待转移的位置的前三位,用6位二进制 ...

  5. BZOJ 2754 SCOI 2012 喵星球上的点名 后缀数组 树状数组

    2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2068  Solved: 907[Submit][St ...

  6. 【转载】CMarkup函数说明

    1.初始化Load    导入一个XML文件到CMarkup的对象中,并对它进行解析.类似C#的Load.SetDoc  从字符串中导入XML数据,并对它解析.类似C#的LoadXml. 2.输出Sa ...

  7. 【JavaScript代码实现四】获取和设置 cookie

    // 创建cookie function setCookie(name, value, expires, path, domain, secure) { var cookieText = encode ...

  8. Python的静态方法和类成员方法

    http://www.cnblogs.com/2gua/archive/2012/09/03/2668125.html Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还 ...

  9. 11gR2 RAC重新启动后仅仅能起单节点

    11gR2 RAC重新启动后仅仅能起单节点 问题背景: 将11gR2 RAC正常部署完毕之后运行两节点重新启动操作发现当中有一个节点的集群资源无法启动,遂再次重新启动该无法启动集群资源的节点,还是不可 ...

  10. C# 去掉webapi返回json所带的转义字符

    反序列换报错: {"Error converting value \"{\"Result\":true,\"Code\":\"\& ...