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. iOS 11开发教程(四)iOS11模拟器介绍一

    iOS11模拟器介绍 在图1.6或者1.7中所看到的类似于手机的模型就是iOS模拟器.iOS模拟器是在没有iPhone或iPad设备时,对程序进行检测的设备.iOS模拟器可以模仿真实的iPhone或i ...

  2. C# 序列化简单格式XML

    问师傅反序列化和序列化到底是什么, 然后师傅鄙视一下我的智商,让我做个反序列化解析XML. 一边听着师傅在旁边跟女朋友打电话收到暴击伤害,一边写,搞了一个半小时. XML文件: <?xml ve ...

  3. 冒泡排序(高级版)之C++实现

    冒泡排序(高级版)之C++实现 一.源代码:BubbleSortHigh.cpp #include<iostream> using namespace std; /*定义输出一维数组的函数 ...

  4. 课堂练习—hash

    课堂练习-hash 要求: 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,6 ...

  5. java的注解

    本文转载自:http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html 一.概念 Annontation是Java5开始引入的新特征 ...

  6. 【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)

    Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills, every ski ...

  7. Atlas 安装运行随笔

    Atlas 是一个用于数据库负载均衡 ,读写分离的中间件,他实现了mysql 的语法,对于普通调用DAL 层来说的话,和mysql 是一样的. 一,安装1,从源码安装 , 可以参考 http://bl ...

  8. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划

    A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...

  9. SCC缩点

    int V; //顶点数量 vector<int> G[max_v]; //图的邻接表表示方法 vector<int> rG[max_v]; //把边反向建的图 vector& ...

  10. offsetLeft && left

    /* function getCss(obj,attr){ return window.getComputedStyle ? window.getComputedStyle(obj,null)[att ...