1、Touch事件

//系统自动调用

//一个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];

2、事件分发

事件产生和传递的过程:

当发生触摸事件的时候,先传递给UIApplication,以队列结构接收事件。传递给主窗口,主窗口传递给控制器的UIView,遍历UIView的子视图,寻找最合适的UIView来接收。

响应者链:

该类的上一级如果是UIView,子视图处理事件,如果处理不了,找他的父视图。

该类的上一级如果是UIViewController,则是由所属的控制器来处理。

//返回最合适的UIView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {}

//判断当前的点在不在View上

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

return YES;

}

3、UIImageView点击事件

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;

}

(1)点击手势 Tap

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];

(2)长按手势 LongPress
    UILongPressGestureRecognizer * longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];

[self.imageView addGestureRecognizer:longGesture];

(3)轻扫手势  Swipe

UISwipeGestureRecognizer * swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

//设置轻扫方向

swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;

[self.imageView addGestureRecognizer:swipeGesture];

(4)捏合事件 Pinch

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 手势大全的更多相关文章

  1. 墙裂推荐 iOS 资源大全

    这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...

  2. iOS 资源大全整理

    这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...

  3. ios手势

    iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小   1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...

  4. iOS手势学习UIGestureRecognizer & cocos2d 手势推荐

    iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer  // 长按UIPanGestur ...

  5. iOS手势处理

    iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...

  6. iOS github大全 & iOS7的学习blog

    iOS github大全 :有600多个iOS各方面的开源库,并分类了 一天天学习iOS7 :每天学习一点iOS7的新特性

  7. iOS 手势识别器概述

    手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...

  8. swift 实现iOS手势密码、指纹密码、faceID

    本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...

  9. iOS手势解锁、指纹解锁--Swift代码

    一.手势密码 1. 1.1.用UIButton组成手势的节点. 1.2.当手指接触屏幕时,调用重写的 touchesBegan:withEvent方法(在touchesBegan里调用setNeeds ...

随机推荐

  1. socket对于大数据的发送和接收

    大数据是指大于32K或者64K的数据. 大数据的发送和接收通过TSTREAM对象来进行是非常方便的. 我们把大数据分割成一个个4K大小的小包,然后再依次传输. 一.大数据的发送的类语言描述: 1)创建 ...

  2. linux 分区与挂载

    流程:分区-格式化-挂载 1.添加硬盘 使用fdisk -l命令查看磁盘状态 此处可以看到两块硬盘sda和sdb,第一块硬盘sda是装好系统的.sdb硬盘是未进行分区的. 本例将这个20G的硬盘分区, ...

  3. 在ASP.NET MVC中验证checkbox 必须选中 (Validation of required checkbox in Asp.Net MVC)

    转载自 http://blog.degree.no/2012/03/validation-of-required-checkbox-in-asp-net-mvc/ Why would you want ...

  4. HDU2947Bicycle Puzzle(组合原理)

    题目大意: 你和朋友两人玩游戏,将一个图片均等切割成W* H块,打乱每一小块的位置.拼图游戏开始.每次,可以交换任意两块,记下交换的次数,图片还原游戏结束.得分为执行交换的次数.得分越小越好. 现在, ...

  5. 关于JPA封装数据库数据到实体不调用属性的get和set的方法解决办法

    今天发现JPA封装数据库数据到实体并不调用属性的get和set的,郁闷,本来想在set方法做改字段的值处理的谁知道遇到这个情况: @Column(name = acode) @Access(value ...

  6. junit学习(3.x)

    自动化测试 测试所有测试类 import junit.framework.TestCase; import junit.framework.Assert; /** *测试类必须要继承TestCase类 ...

  7. Ubuntu 搭建PHP开发环境

    Ubuntu确实很好玩.有喜欢的命令行,简洁的界面,不同于Window要的感觉.偶尔换换环境工作,学习Linux的思维方式,是一种不错的做 法.之前也折腾过Ubuntu,不过,因为网络的问题,一直没有 ...

  8. 浏览器URL访问网页具体发生了什么

    [详细讲解:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html]   ——浏览器输入URL ——DNS解析过程 解析域名,找到 ...

  9. Combox和DropDownList控件的区别

    共同点:都是下拉框控件 不同点:Combox用在winform上,DropDownList用在网页上,且两者绑定方式略有不同 绑定数据例子如下—— 1.Combox绑定 DataTable dtBus ...

  10. C++ 类的静态成员详细讲解[转]

    在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...