IOS第15天(3,事件处理,手势处理)
7> 手势识别
使用UIImageView原因:之前既能看见图片,又能监听点击的只有UIButton,学了手势,我们的UIImageView也可以。
* tap(代理:左边不能点,右边能点)
* longPress(allowableMovement:触发之前,最大的移动范围)
> 默认调用两次,开始一次,结束一次。
* swipe:(一个手势只能识别一个方向)
* 旋转:
基于上一次旋转
* 复位:(手势的取值都是相对最原始的位置,我们应该是需要相对上一次,因此每次调用,就复位一下,每次都是从零开始旋转角度)
缩放:复位
* 如何同时支持旋转和缩放?默认不支持多个手指,
Simultaneously:同时
当使用一个手势的时候会调用代理的Simultaneously方法,询问是否支持多个手势
* pan
获取平移的位置:translationInView
复位:setTranslation:inView: 需要传一个view,因为点的位置跟坐标系有关系,看他是基于哪个坐标系被清空的。
#import "HMViewController.h" @interface HMViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imagView; @end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [_imagView addGestureRecognizer:pan];
} - (void)pan:(UIPanGestureRecognizer *)pan
{
// 获取手指移动的位置
CGPoint trans = [pan translationInView:_imagView]; _imagView.transform = CGAffineTransformTranslate(_imagView.transform, trans.x, trans.y); // 复位
[pan setTranslation:CGPointZero inView:_imagView];
NSLog(@"%@",NSStringFromCGPoint(trans)); } #warning pinch
- (void)addPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 设置代理的原因:想要同时支持多个手势
pinch.delegate = self;
[_imagView addGestureRecognizer:pinch]; } - (void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imagView.transform = CGAffineTransformScale(_imagView.transform, pinch.scale, pinch.scale); // 复位
pinch.scale = ;
} // Simultaneous:同时
// 默认是不支持多个手势
// 当你使用一个手势的时候就会调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
} #warning rotation
- (void)addRotation
{
// rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_imagView addGestureRecognizer:rotation];
} - (void)rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"%f",rotation.rotation);
// _imagView.transform = CGAffineTransformMakeRotation(rotation.rotation);
_imagView.transform = CGAffineTransformRotate(_imagView.transform, rotation.rotation); // 复位
rotation.rotation = ;
} #warning Swipe
- (void)addSwipe
{
// Swipe
// 一个手势只能识别一个方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[_imagView addGestureRecognizer:swipe];
} - (void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"swipe");
} #warning longPress
- (void)addLongPress
{
// longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [_imagView addGestureRecognizer:longPress];
} - (void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 根据状态执行事件
if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"longPress");
}
} #warning tap
- (void)addTap
{
// tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 点按多少次才能触发手势
// tap.numberOfTapsRequired = 2;
//
// // 必须多少个手指触摸才能触发手势
// tap.numberOfTouchesRequired = 2; tap.delegate = self; [_imagView addGestureRecognizer:tap];
} // 这个触摸点能否触发手势
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
//{
// CGPoint currentPoint = [touch locationInView:_imagView];
//
// if (currentPoint.x < _imagView.bounds.size.width * 0.5) {
// return NO;
// }else{
//
// return YES;
// }
//} - (void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@"tap");
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
IOS第15天(3,事件处理,手势处理)的更多相关文章
- IOS第15天(2,事件处理,侧滑菜单,抽屉效果)
******HMDrawViewController.m #import "HMDrawViewController.h" @interface HMDrawViewControl ...
- IOS第15天(2,事件处理hitTest练习)
***hitTest 获取最合适的点 @implementation HMGreenView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEv ...
- IOS第15天(1,事件处理View的拖拽)
*******view 一些方法 #import "HMView.h" @implementation HMView // 一个完整的触摸过程 // touchesBegan -& ...
- iOS中的触摸事件和手势处理
iOS中的事件可以分为三大类: 1> 触摸事件 2> 加速计事件 3> 远程控制事件 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...
- iOS开发之触摸事件及手势
1.iOS中的事件 在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: 2.响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...
- iOS边练边学--UIGestureRecognizer手势识别器简单介绍
iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度. 一.UIGestureRecognizer UIGestureRe ...
- iOS学习笔记——触控与手势
触控 此部分内容已学良久,恨记之甚晚,忙矣,懒矣!本文简而记焉,恐日后忘也. 在iOS的触控事件中,有触控.事件以及响应者这三个角色,一个触摸则代表了一只手指和屏幕接触这个动作所包含的信息:而事件则包 ...
- iOS开发——实用技术OC篇&事件处理详解
事件处理详解 一:事件处理 事件处理常见属性: 事件类型 @property(nonatomic,readonly) UIEventType type; @property(nonatomic ...
- iOS利用Runtime自定义控制器POP手势动画
前言 苹果在iOS 7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操 ...
随机推荐
- Python与Hack之守护进程
1.什么是守护进程: 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在linux中,每个 ...
- 原生Ajax写法(GET)
ajax的GET提交方式的原生代码: var xhr = null; if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); }else if(w ...
- Kalman滤波器原理和实现
Kalman滤波器原理和实现 kalman filter Kalman滤波器的直观理解[1] 假设我们要测量一个房间下一刻钟的温度.据经验判断,房间内的温度不可能短时大幅度变化,也就是说可以依经验认为 ...
- 树形DP水题杂记
BZOJ1131: [POI2008]Sta 题意:给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大. 题解:记录每个点的深度,再根据根节点的深度和逐层推导出其他点的深度和. ...
- Nginx 利用 X-Accel-Redirect response.setHeader 控制文件下载
nginx.conf location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP ...
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
关于证明可以参考题解http://codeforces.com/blog/entry/12739 就是将概率从大到小排序然后,然后从大到小计算概率 #include <iostream> ...
- NOIp 2013 #2 花匠 Label:爆0的Water
题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希 望剩下的花排列得比较别致. 具 ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- 如何在WORD2010中取消自动编号?
如何在WORD2010中取消自动编号? 使用WORD2010有一个很大的问题就是WORD2010的自动编号问题,WORD2010的自动编号是符合外国人的写作习惯的,对中国人来说不适用. WORD201 ...
- php归档函数(按时间)实现
今日开发本站需要用到按时间归档文章的功能,即按文档发布时间将文章文类,以实现检索和统计功能,于是自己写了一个, 现分享给大家,相信大家工作和学习中有可能会用到,实现原理很简单,即取出文章发布时间戳的年 ...