iOS:手势的详解UIGestureReconizer
手势类:UIGestureReconizer
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, //评估可能发生的手势状态
UIGestureRecognizerStateBegan, //手势开始状态
UIGestureRecognizerStateChanged, //手势持续状态
UIGestureRecognizerStateEnded, //手势结束状态
UIGestureRecognizerStateCancelled, //手势取消状态
UIGestureRecognizerStateFailed, //手势失败状态
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded ; //确认的手势结束状态
};
@interface UIGestureRecognizer : NSObject
属性:
//手势状态
@property(nonatomic,readonly) UIGestureRecognizerState state;
//手势代理
@property(nonatomic,assign) id <UIGestureRecognizerDelegate> delegate;
//能否手势
@property(nonatomic, getter=isEnabled) BOOL enabled;
//手势触摸的视图
@property(nonatomic,readonly) UIView *view;
//是否取消触摸视图
@property(nonatomic) BOOL cancelsTouchesInView;
// 是否延迟触摸开始
@property(nonatomic) BOOL delaysTouchesBegan;
// 是否延迟触摸结束
@property(nonatomic) BOOL delaysTouchesEnded;
方法:
//初始化,创建手势,并响应事件
- (instancetype)initWithTarget:(id)target action:(SEL)action;
// 添加手势事件
- (void)addTarget:(id)target action:(SEL)action;
//移除手势事件
- (void)removeTarget:(id)target action:(SEL)action;
//指定一个手势失败,另一个手势才执行
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
//返回当前触摸点坐标系
- (CGPoint)locationInView:(UIView*)view;
//返回触摸点个数
- (NSUInteger)numberOfTouches;
//返回指定视图中第几个触摸点的坐标系
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;
@end
手势的代理协议
@protocol UIGestureRecognizerDelegate <NSObject>
@optional
//询问一个手势接收者是否应该开始解释执行一个触摸接收事件
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//询问delegate,两个手势是否同时接收消息,返回YES同事接收。返回NO,不同是接收(如果另外一个手势返回YES,则并不能保证不同时接收消息)the default implementation returns NO。这个函数一般在一个手势接收者要阻止另外一个手势接收自己的消息的时候调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//询问是否另外一个手势失败,这个手势才开始执行
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer ;
//询问是否这个手势是通过一个指定手势的触发才失败
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer ;
// 询问delegate是否允许手势接收者接收一个touch对, 返回YES,则允许对这个touch对象审核,NO,则不允许。这个方法在touchesBegan:withEvent:之前调用,为一个新的touch对象进行调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
@end
@interface UITapGestureRecognizer : UIGestureRecognizer {
属性:
//点击次数,默认为1次
@property (nonatomic) NSUInteger numberOfTapsRequired;
//需要触摸的手指个数,默认为1个
@property (nonatomic) NSUInteger numberOfTouchesRequired;
@end
=====================================================
2、pinch捏合手势:放缩
@interface UIPinchGestureRecognizer : UIGestureRecognizer {
属性:
//放缩系数
@property (nonatomic) CGFloat scale;
//每秒放缩的速率
@property (nonatomic,readonly) CGFloat velocity;
@end
=====================================================
3、rotate旋转手势:旋转
@interface UIRotationGestureRecognizer : UIGestureRecognizer {
属性:
//旋转弧度
@property (nonatomic) CGFloat rotation;
//每秒旋转的速率
@property (nonatomic,readonly) CGFloat velocity;
@end
=====================================================
4、pan拖动手势:拖动
@interface UIPanGestureRecognizer : UIGestureRecognizer {
属性:
//需要的最少触摸手指数,默认为1
@property (nonatomic) NSUInteger minimumNumberOfTouches;
//需要的最多触摸手指数,默认为unit_max
@property (nonatomic) NSUInteger maximumNumberOfTouches;
//返回在指定视图中的坐标系
- (CGPoint)translationInView:(UIView *)view;
//设置在指定视图中的坐标系(可以用于复位操作)
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view;
//返回拖动手势的速度,值是每秒移动过的坐标系point
- (CGPoint)velocityInView:(UIView *)view;
@end
=====================================================
5、longPress长按手势:长时间按下
@interface UILongPressGestureRecognizer : UIGestureRecognizer {
属性:
//需要按的次数,默认为0
@property (nonatomic) NSUInteger numberOfTapsRequired;
//需要触摸的手指数,默认为1
@property (nonatomic) NSUInteger numberOfTouchesRequired;
//长按的最小时间,默认为0.5秒
@property (nonatomic) CFTimeInterval minimumPressDuration;
//允许移动的唯一,默认为10
@property (nonatomic) CGFloat allowableMovement;
@end
=====================================================
6、swipe轻扫手势:轻轻扫过
轻扫方向枚举(位移运算,可以与或非)
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0, //往右扫
UISwipeGestureRecognizerDirectionLeft = 1 << 1, //往左扫
UISwipeGestureRecognizerDirectionUp = 1 << 2, //向上扫
UISwipeGestureRecognizerDirectionDown = 1 << 3 //向下扫
};
@interface UISwipeGestureRecognizer : UIGestureRecognizer {
属性:
//需要触摸的手指数,默认为1
@property(nonatomic) NSUInteger numberOfTouchesRequired;
//手势轻扫的方向
@property(nonatomic) UISwipeGestureRecognizerDirection direction;
@end
===================================================
7、摇动手势
//一、tap手势(点击)
//1.创建手势识别对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; //2.设置属性
tap.numberOfTapsRequired = ; //需要按几次
tap.numberOfTouchesRequired = ; //需要几个点(手指)按 //3.把手势添加到视图中
[self.myView addGestureRecognizer:tap];
执行:
//tap手势识别的处理方法
-(void)tap:(UITapGestureRecognizer *)sender
{ UIView *view = sender.view; if ([view.backgroundColor isEqual:[UIColor redColor]])
{
view.backgroundColor = [UIColor greenColor];
}
else
{
view.backgroundColor = [UIColor redColor];
}
}
//二、pinch手势(放大、缩小)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch];
//pinch手势识别的处理方法
-(void)pinch:(UIPinchGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateChanged)
{
//使用放射变换来放大和缩小
CGAffineTransform transform = self.myView.transform;
transform = CGAffineTransformScale(transform, sender.scale, sender.scale); self.myView.transform = transform;
} //复位
sender.scale = 1.0;
}
演示结果:(大图缩小成小图)
//三、rotate旋转的手势
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotate:)];
[self.view addGestureRecognizer:rotate];
执行:
//rotate手势识别的处理方法
-(void)rotate:(UIRotationGestureRecognizer *)sender
{
//持续性操作
if (sender.state == UIGestureRecognizerStateChanged)
{
//使用放射变换进行旋转
//self.myView.transform = CGAffineTransformMakeRotation(sender.rotation);(不用复位) CGAffineTransform transform = self.myView.transform;
transform = CGAffineTransformRotate(transform, sender.rotation);
self.myView.transform = transform;
} //复位
sender.rotation = ;
}
演示结果:(旋转了)
//四、pan拖动的手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; //需要手指数
pan.minimumNumberOfTouches = ;
pan.maximumNumberOfTouches = ; [self.view addGestureRecognizer:pan];
//pan手势识别的处理方法
-(void)pan:(UIPanGestureRecognizer *)sender
{
//持续性操作
if (sender.state == UIGestureRecognizerStateChanged)
{
CGPoint offsetPoint = [sender translationInView:self.view];
CGPoint center = self.myView.center;
self.myView.center = CGPointMake(center.x+offsetPoint.x, center.y+offsetPoint.y);
} //复位
[sender setTranslation:CGPointZero inView:self.view];
}
演示结果:(从中间拖到了右下角)
//五、longPress长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; //需要按的次数
longPress.numberOfTapsRequired = ;
//需要几根手指
longPress.numberOfTouchesRequired = ;
//按住的最小时间
longPress.minimumPressDuration = 0.2f; [self.myView addGestureRecognizer:longPress];
//longPress手势识别的处理方法
-(void)longPress:(UILongPressGestureRecognizer *)sender
{
//持续性操作
if (sender.state == UIGestureRecognizerStateChanged)
{
self.myView.backgroundColor = [UIColor yellowColor];
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
self.myView.backgroundColor = [UIColor redColor];
} }
演示结果:(长按时间到,红色变为黄色)
//六、swipe轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; //设置手势轻扫方向
swipe.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipe];
//swipe手势识别的处理方法
-(void)swipe:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"swipe Right");
}
else if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(@"swipe Left");
}
else if (sender.direction == UISwipeGestureRecognizerDirectionDown)
{
NSLog(@"swipe Down");
}
else if (sender.direction == UISwipeGestureRecognizerDirectionUp)
{
NSLog(@"swipe Up");
}
}
@end
演示结果:(只有向上扫,才输出结果)
-- ::27.818 -手势识别gestureReconizer[:] swipe Up
<7>摇动手势
重写方法:
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
NSLog(@"摇一摇,摇到外婆桥");
}
}
运行结果:
-- ::35.757 -shake[:] 摇一摇,摇到外婆桥
摇动手势操作方法,点击硬件设备上的如下Shake Gesture按钮即可:
iOS:手势的详解UIGestureReconizer的更多相关文章
- iOS应用开发详解
<iOS应用开发详解> 基本信息 作者: 郭宏志 出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...
- 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )
原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理 ...
- iOS中-Qutarz2D详解及使用
在iOS中Qutarz2D 详解及使用 (一)初识 介绍 Quartz 2D是二维绘图引擎. 能完成的工作有: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...
- iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...
- iOS开发——Block详解
iOS开发--Block详解 1. Block是什么 代码块 匿名函数 闭包--能够读取其他函数内部变量的函数 函数变量 实现基于指针和函数指针 实现回调的机制 Block是一个非常有特色的语法,它可 ...
- iOS开发:详解Objective-C runTime
Objective-C总Runtime的那点事儿(一)消息机制 最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎 ...
- 了解iOS消息推送一文就够:史上最全iOS Push技术详解
本文作者:陈裕发, 腾讯系统测试工程师,由腾讯WeTest整理发表. 1.引言 开发iOS系统中的Push推送,通常有以下3种情况: 1)在线Push:比如QQ.微信等IM界面处于前台时,聊天消息和指 ...
- iOS开发者证书-详解
iOS开发者证书-详解/生成/使用 本文假设你已经有一些基本的Xcode开发经验, 并注册了iOS开发者账号. 相关基础 加密算法 现代密码学中, 主要有两种加密算法: 对称密钥加密 和 公开密钥加密 ...
- iOS开发-Runtime详解
iOS开发-Runtime详解 简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [recei ...
随机推荐
- java.util.ConcurrentModification并发修改异常
在运行下面这段代码时出现了并发修改异常java.util.ConcurrentModification: public static void main(String[] args) { List l ...
- django “如何”系列10:如何管理静态文件
django开发者最关心的是web应用中的动态部分-视图函数和模板.但是明显,web应用还有其他需要注意的部分:静态文件(图片,css,javascript等等),那些都是渲染一个完整的页面需要的东西 ...
- HDU-1934
Car Plates Competition Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- ZOJ-3318
Strange Country Time Limit: 1 Second Memory Limit: 32768 KB There are n cities in the dream cou ...
- 前端读者 | 从一行代码里面学点JavaScript
本文来自 @张小俊128:链接:http://www.html-js.com/article/A-day-to-learn-from-a-line-of-code-inside-the-JavaScr ...
- SQL Error: 1064, SQLState: 42000
这个错误是因为mysql有些关键字被我们用了,需要更改关键字成其他名字 ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIGIN ...
- localStorage和sessionStorage的总结
localStorage:没有时间限制的数据存储 API: 1.localStorage.setItem('name','wangwei')/localStorage.name='wangwei'存储 ...
- HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划
有一个只含0和1的长度为n的串,问不含有101的所有串的个数. ——不存在连续的101.010.111的字符串数量 HDU:https://cn.vjudge.net/problem/HDU-3485 ...
- WAR/EAR 概念
In J2EE application, modules are packaged as EAR, JAR and WAR based on their functionality JAR: EJB ...
- BZOJ 2789 letters(树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2789 [题目大意] 给出两个字符串,通过A字符串相邻之间字符的交换得到B字符串, 求最 ...