iOS 手势大全
//系统自动调用
//一个UITouch代表一根手指 按住option变成两根手指
//虽然是两个手指,但只执行一次触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {} 开始触摸事件
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {} 手指移动事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {} 结束触摸时间
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {} 取消触摸手势
//获取任意手指
UITouch * touch = [touches anyObject];
//手指触摸的View
NSLog(@"touch.view:%@",touch.view);
//点击次数
NSLog(@"点击次数:%ld",touch.tapCount);
//当前手指的位置
CGPoint currentPonit = [touch locationInView:touch.view];
//上一次手指所处的位置
CGPoint previousPonit = [touch previousLocationInView:touch.view];
事件产生和传递的过程:
当发生触摸事件的时候,先传递给UIApplication,以队列结构接收事件。传递给主窗口,主窗口传递给控制器的UIView,遍历UIView的子视图,寻找最合适的UIView来接收。
响应者链:
该类的上一级如果是UIView,子视图处理事件,如果处理不了,找他的父视图。
该类的上一级如果是UIViewController,则是由所属的控制器来处理。
//返回最合适的UIView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {}
//判断当前的点在不在View上
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return YES;
}
self.imageView.userInteractionEnabled = NO;
//无法响应事件的3种情况:
//1、当userInteractionEnabled未开启
//2、当hidden属性为YES
//3、当alpha属性为0时
//将以YellowView为坐标系的点,转化为以button为坐标系的点 self = yellowView
CGPoint buttonPoint = [self convertPoint:point toView:self.button];
这个函数的用处是判断当前的点击或者触摸事件的点是否在当前的view中。
它被hitTest:withEvent:调用,
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; // default returns YES if point is in bounds
4、手机摇一摇
//事件的种类
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"摇一摇");
}
}
5、**Gesture手势
UIGestureRecognizerState:
开始: UIGestureRecognizerStateBegan
改变: UIGestureRecognizerStateChanged
结束: UIGestureRecognizerStateEnded
取消: UIGestureRecognizerStateCancelled
失败: UIGestureRecognizerStateFailed,
//同时使用两个手势 捏合 旋转
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction1:)];
//点击一次便触发
tap1.numberOfTapsRequired = 1;
[self.imageView addGestureRecognizer:tap1];
UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction2:)];
//双击一次便触发
tap2.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:tap2];
//如果tap2成功执行,让tap1失败
[tap1 requireGestureRecognizerToFail:tap2];
[self.imageView addGestureRecognizer:longGesture];
(3)轻扫手势 Swipe
UISwipeGestureRecognizer * swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//设置轻扫方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageView addGestureRecognizer:swipeGesture];
UIPinchGestureRecognizer * pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
pinchGesture.delegate = self;
[self.imageView addGestureRecognizer:pinchGesture];
self.pin = pinchGesture;
//对应事件
- (void)pinchAction:(UIPinchGestureRecognizer *)pin {
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pin.scale, pin.scale);
//复位
pin.scale = 1;
}
(5)旋转事件 Rotation
UIRotationGestureRecognizer * rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
rotationGesture.delegate = self;
[self.imageView addGestureRecognizer:rotationGesture];
//对应事件
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
//复位
rotation.rotation = 0;
}
(6)拖动手势 Pan
UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.imageView addGestureRecognizer:panGesture];
//对应事件
- (void)panAction:(UIPanGestureRecognizer *)pan {
//获取拖动的偏移量
CGPoint ponit = [pan translationInView:self.view];
self.imageView.center = CGPointMake(self.imageView.center.x + ponit.x, self.imageView.center.y + ponit.y);
//复位
[pan setTranslation:CGPointZero inView:self.imageView];
}
iOS 手势大全的更多相关文章
- 墙裂推荐 iOS 资源大全
这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...
- iOS 资源大全整理
这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...
- ios手势
iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...
- iOS手势学习UIGestureRecognizer & cocos2d 手势推荐
iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer // 长按UIPanGestur ...
- iOS手势处理
iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...
- iOS github大全 & iOS7的学习blog
iOS github大全 :有600多个iOS各方面的开源库,并分类了 一天天学习iOS7 :每天学习一点iOS7的新特性
- iOS 手势识别器概述
手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...
- swift 实现iOS手势密码、指纹密码、faceID
本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...
- iOS手势解锁、指纹解锁--Swift代码
一.手势密码 1. 1.1.用UIButton组成手势的节点. 1.2.当手指接触屏幕时,调用重写的 touchesBegan:withEvent方法(在touchesBegan里调用setNeeds ...
随机推荐
- HDU 1160 FatMouse's Speed (sort + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...
- How to bind to data when the DataContext is not inherited【项目】
http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherit ...
- CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”
刚安装完CentOS,使用yum命令安装一些常用的软件,使用如下命令:yum –y install gcc. 提示如下错误信息: Loaded plugins: fastestmirror, refr ...
- stl map高效遍历删除的方法 [转]
for(:iter!=mapStudent.end():) { if((iter->second)>=aa) { //满足删除条件,删除当前结点,并指 ...
- chrome 41 空格
chrome 41对半角空格的解析 当做一个汉字宽度来处理了. 导致很多网站出现异常. 目前能想到的方法是删掉用来规范格式的空格. 老版chrome chrome41 和讯网也有这种问题 有更好的处理 ...
- CriminalIntent程序中Fragment相关内容
Activity中托管UI fragment有两种方式: 添加fragment到acitivity中 在activity代码中添加fragment 第一种方法即将fragment添加到acitivit ...
- jeecg团队招新人(5人)
jeecg团队招新人(5人) http://www.jeecg.org/forum.php? mod=viewthread&tid=2046&page=1&extra=#pid ...
- Swift学习笔记十
枚举 一个枚举为一组相关联的值定义一个通用类型,并且让你可以在代码中类型安全地操作这些值. C中的枚举将关联的名字指派给一系列整型值.Swift中的枚举类型更为活泼,并不需要为每个成员指定值,如果指定 ...
- .net MVC 碰到的问题
1:问:回车会默认会触发页面从左边至右,从上到下索引位置第一的按钮事件.如何取消? 答:在不需要触发按钮事件的按钮中加一个属性:UseSubmitBehavior="false" ...
- [转载]GMT地形数据总结
[转载]GMT地形数据总结 原文地址:GMT地形数据总结作者:Jason 转载:http://seisman.info/gmt-topo-grid-datas.html 目前接触到的地形数 ...