iOS手势识别器
UIGestureRecognizer
UIGestureRecognizer类,用于检测、识别用户使用设备时所用的手势.它是一个抽象类,定义了所有手势的基本行为.以下是UIGestureRecognizer子类,用于处理具体的用户手势行为:
UITapGestureRecognizer // 1.单击
UILongPressGestureRecognizer // 3.长按
UISwipeGestureRecognizer // 4.轻扫
UIPanGestureRecognizer // 5.移动
UIRotationGestureRecognizer // 6.旋转
UIPinchGestureRecognizer // 7.捏合
创建手势:
// 1.单击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imgView addGestureRecognizer:tap];
// 2.双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imgView addGestureRecognizer:doubleTap];
// 双击失败才单击
[tap requireGestureRecognizerToFail:doubleTap];
// 3.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//设置最短时间
longPress.minimumPressDuration = 1;
[imgView addGestureRecognizer:longPress];
// 4.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 设置轻扫方向
[swipe setDirection:UISwipeGestureRecognizerDirectionRight];
[imgView addGestureRecognizer:swipe];
// 5.移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imgView addGestureRecognizer:pan];
// 轻扫失败才移动
[pan requireGestureRecognizerToFail:swipe];
// 6.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation];
// 7.捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinch];
手势触发事件:
GestureAction:
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
}
}
- (void)panAction:(UIPanGestureRecognizer *)pan {
//手指所在的坐标
CGPoint point = [pan locationInView:self.view];
_view.center = point;
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
if (rotation.state == UIGestureRecognizerStateChanged) {
//取到弧度
CGFloat angle = rotation.rotation;
//正在旋转
rotation.view.transform = CGAffineTransformMakeRotation(angle);
} else if (rotation.state == UIGestureRecognizerStateEnded) {
//还原
[UIView animateWithDuration:.5 animations:^{
rotation.view.transform = CGAffineTransformIdentity;
}];
}
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
if (pinch.state == UIGestureRecognizerStateChanged) {
// 取到缩放比率
CGFloat scale = pinch.scale;
// 缩放
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
} else if (pinch.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:.5 animations:^{
pinch.view.transform = CGAffineTransformIdentity;
}];
}
}
Motion 摇晃手势
//让当前对象成为第一响应者
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇一摇开始");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇一摇结束");
}
iOS手势识别器的更多相关文章
- iOS 手势识别器概述
手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...
- iOS 手势识别器(UIGestureRecognizer)
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势. UIGestureRecognizer的子类有: UITapGestureRecogni ...
- iOS 七大手势之轻拍,长按,旋转手势识别器方法
一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...
- 我的IOS学习之路(三):手势识别器
在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...
- iOS 触摸事件与手势识别器(Gesture Recognizers)
Gesture Recognizers与触摸事件分发 通过一个问题引出今天的知识: 1.大家应该都遇见过 当需要给tableView 添加一个tap 手势识别 但是tableView 的上的事件(滑动 ...
- IOS 响应者链条 and UIGestureRecognizer 手势识别器)
一次完整的触摸事件的传递响应的过程 UIAppliction --> UIWiondw -->递归找到最适合处理事件的控件 控件调用touches方法-->判断是否实现touches ...
- iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波
一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听vi ...
- iOS开发UI高级手势识别器
####手势识别器 UIGestureRecognizer类 ·UITapGestureRecognizer(轻击) ·UIPinchGestureRecognizer(捏合) ·UIPanGestu ...
- iOS的触摸事件的用法以及和手势识别器的区别
1.首先来介绍下触摸事件和手势识别器的利与弊 触摸事件和手势识别器二者之间有直接的关系 手势识别器是在触摸事件的基础上演变过来的 当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义v ...
随机推荐
- Redux学习笔记--异步Action和Middleware
异步Action 之前介绍的都是同步操作,Redux通过分发action处理state,所有的数据流都是同步的,如果需要一步的话怎么办? 最简单的方式就是使用同步的方式来异步,将原来同步时一个acti ...
- less @import and extend及mixin详解
在less中,通过 @import (keyword) "filename"的方式引入其他的文件,这个keyword可以是以下6种: referrence referrence这个 ...
- Day03——Python函数
函数定义和使用 1.语法 def 函数名(参数): ... 函数体 ... 返回值 函数的定义主要有如下要点: def:表示函数的关键字 函数名:函数的名称,日后根据函数名调用函数 函数体:函数中进行 ...
- TIA Portal 和 scout 之间的驱动器地址分配
TIA Portal集成了scout.在使用simotion控制器时,分配驱动装置的地址可能会碰到问题. 解决方法: 1)在配置驱动时,TIA Portal软件的语言需要选择为应为中文 2)unico ...
- ZT————pull push mode
谁能讲讲push和pull模式是什么意思?(参与有分) [问题点数:100分,结帖人mickeyfirst] 收藏 mickeyfirst mickeyfirst 等级: 结帖率:94.12% 楼主 ...
- SVN安装操作流程
SVN 安装操作流程 1.服务端安装流程 1.1 双击打开svn-server安装包 1.2 点击Next 1.3 勾选上“I accert the terms in the License Agre ...
- 475. Heaters (start binary search, appplication for binary search)
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- Ubuntu16.04使用所遇问题记录
记录笔者在使用Ubuntu系统过程中所遇到过的错误/问题和解决方案.本机系统为Ubuntu 16.04 LTS,64-bit. 目前已有的解决方案: (1)Ubuntu安装搜狗输入法 (2)Windo ...
- Intellij IDEA Organize Imports
使用Eclipse进行开发时,我喜欢用Ctrl + Shift + O快捷键管理Java类的导入,它可以导入所需的Java类,去除不需要的Java类. Eclipse的Organize Imports ...
- UVa 1218 - Perfect Service(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...