1、UITouch简介

  • 当用户触摸屏幕时,会创建一个UITouch对象;
  • UITouch的作用保存着触摸相关的信息,比如触摸的位置、时间、阶段等;
  • 当从开始到结束,系统会更新UITouch对象,结束时会被销毁。
  • 期间所有的UITouch对象都被包含在UIEvent事件对象中,由管理程序UIApplication对象将事件分发。

2、触摸事件调用方法

//响应触摸事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指按下的时候调用
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指移动的时候调用
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指抬起的时候调用
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//取消(非正常离开屏幕,意外中断)
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);// Apple Pencil 产生的 touch 事件的部分信息(如 Pencil 的方向等)传递到 iPad 或 iPhone 上会有一定的延时。
//UIKit 的回调方法 touchBegan 是立即产生的,其返回的参数 touch 中包含了 Pencil 产生的额外信息,这个额外信息是有延时的。所以,首次回调时会给出额外信息的预估值,延时获取真实值之后会调用 touchesEstimatedPropertiesUpdated 方法更新额外信息。

3、UITouch相关API

//
// UITouch.h
// UIKit
//
// Copyright (c) 2007-2017 Apple Inc. All rights reserved.
// #import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKitDefines.h> NS_ASSUME_NONNULL_BEGIN @class UIWindow, UIView, UIGestureRecognizer; typedef NS_ENUM(NSInteger, UITouchPhase) {
UITouchPhaseBegan, // 开始触摸
UITouchPhaseMoved, // 移动
UITouchPhaseStationary, // 停留
UITouchPhaseEnded, // 结束
UITouchPhaseCancelled, // 取消
}; typedef NS_ENUM(NSInteger, UIForceTouchCapability) {
UIForceTouchCapabilityUnknown = , // 3D Touch检测失败
UIForceTouchCapabilityUnavailable = , // 3D Touch不可用
UIForceTouchCapabilityAvailable = // 3D Touch可用
}; typedef NS_ENUM(NSInteger, UITouchType) {
UITouchTypeDirect, // 手指和屏幕直接接触
UITouchTypeIndirect, // 间接接触(不直接接触屏幕)
UITouchTypeStylus NS_AVAILABLE_IOS(9_1), // 笔触
} NS_ENUM_AVAILABLE_IOS(9_0); typedef NS_OPTIONS(NSInteger, UITouchProperties) {
UITouchPropertyForce = (1UL << ), //力度
UITouchPropertyAzimuth = (1UL << ), //方位
UITouchPropertyAltitude = (1UL << ), //高度
UITouchPropertyLocation = (1UL << ), //位置
} NS_AVAILABLE_IOS(9_1); NS_CLASS_AVAILABLE_IOS(2_0) @interface UITouch : NSObject @property(nonatomic,readonly) NSTimeInterval timestamp; // 时间
@property(nonatomic,readonly) UITouchPhase phase; // 状态
@property(nonatomic,readonly) NSUInteger tapCount; // 点击次数
@property(nonatomic,readonly) UITouchType type NS_AVAILABLE_IOS(9_0); //接触类型 // 接触面积的半径
// 接触半径的误差
@property(nonatomic,readonly) CGFloat majorRadius NS_AVAILABLE_IOS(8_0);
@property(nonatomic,readonly) CGFloat majorRadiusTolerance NS_AVAILABLE_IOS(8_0); @property(nullable,nonatomic,readonly,strong) UIWindow *window; //触摸所在窗口
@property(nullable,nonatomic,readonly,strong) UIView *view; //触摸所在视图
@property(nullable,nonatomic,readonly,copy) NSArray <UIGestureRecognizer *> *gestureRecognizers NS_AVAILABLE_IOS(3_2); //触摸手势 - (CGPoint)locationInView:(nullable UIView *)view; //在view上的触摸位置
- (CGPoint)previousLocationInView:(nullable UIView *)view; //记录前一个触摸点的在view上的位置,view为nil就是整个窗口 //现在触摸的精确的坐标
//上一次触摸的精确的坐标
- (CGPoint)preciseLocationInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);
- (CGPoint)precisePreviousLocationInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1); // 触摸压力值
@property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0);
// 最大触摸压力值
@property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0); //沿着x轴正向的方位角,当与x轴正向方向相同时,该值为0;当view参数为nil时,默认为keyWindow返回触针的方位角(弧度)。
- (CGFloat)azimuthAngleInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);
//当前触摸对象的方向上的单位向量当view参数为nil时,默认为keyWindow返回在触针的方位角的方向指向的单位矢量。
- (CGVector)azimuthUnitVectorInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1); //当笔平行于平面时,该值为0
//当笔垂直于平面时,该值为Pi / 2
//触针的高度(单位为弧度)。
@property(nonatomic,readonly) CGFloat altitudeAngle NS_AVAILABLE_IOS(9_1); //当每个触摸对象的触摸特性发生变化时,该值将会单独增加,返回值是NSNumber 索引号,让您关联与原来的触摸更新的联系
@property(nonatomic,readonly) NSNumber * _Nullable estimationUpdateIndex NS_AVAILABLE_IOS(9_1);
//当前触摸对象估计的触摸特性,返回值是UITouchPropertyies一组触摸属性,这些属性将得到更新。
@property(nonatomic,readonly) UITouchProperties estimatedProperties NS_AVAILABLE_IOS(9_1);
//一组期望在未来的更新报文的触摸性能。
@property(nonatomic,readonly) UITouchProperties estimatedPropertiesExpectingUpdates NS_AVAILABLE_IOS(9_1); @end NS_ASSUME_NONNULL_END

UITouch相关API

4、其它

  4.1、类型:直接接触、隔空、笔触;

typedef NS_ENUM(NSInteger, UITouchType) {
UITouchTypeDirect, // 手指和屏幕直接接触
UITouchTypeIndirect, // 间接接触(不直接接触屏幕)
UITouchTypeStylus NS_AVAILABLE_IOS(9_1), // 笔触
} NS_ENUM_AVAILABLE_IOS(9_0);

  4.2、多点触控:重写 isMultipleTouchEnabled 返回为YES

- (BOOL)isMultipleTouchEnabled{
return YES;
}

  4.3、视图上多个Button单独触发

- (BOOL)isExclusiveTouch{
return YES;
}

  4.4、同时设置 isMultipleTouchEnabled 和 isExclusiveTouch时,isExclusiveTouch无效。

5、简单使用

  5.1、获取一个UITouch对象

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
}

  5.2、开启多点触控获取touches集合

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取所有的触摸对象
NSArray * array = [touches allObjects];
}

最新文章

  1. leetcode--5. Longest Palindromic Substring
  2. linux下解压war文件命令
  3. Validform 学习笔记---代码练习
  4. php利用递归函数实现无限级分类
  5. 用jQuery创建HTML中不存在的标签元素碰到的问题
  6. android layout_weight 使用总结
  7. Android UI学习1:控件和基本事件的响应
  8. javascript基础之自执行函数
  9. extend vg(pv,lv)use HotPlug Storage PV for VMI(ECC Env)
  10. TP3.2写上下篇
  11. 基于WanAndroid开放API实现的文章阅读APP
  12. 一次Spring Bean初始化顺序问题排查记录
  13. My ajaxwrapper tool
  14. 【测量实战技术】Cad中导入坐标高程点并可以提取坐标带高程
  15. .NET高级代码审计(第三课)Fastjson反序列化漏洞
  16. Eclipse从SVN检出maven项目后的一些配置
  17. 怎样用纯HTML和CSS更改默认的上传文件按钮样式
  18. sprint初步计划(第一天)
  19. [label][Apache] VirtualHost
  20. java 线程 wait join sleep yield notify notifyall synchronized

热门文章

  1. python3 schedule定时任务
  2. 管理员技术(四): 配置NTP网络时间客户端、 创建一个备份包、 配置用户和组账号、配置一个cron任务
  3. shutil库文件的操作
  4. Ext 可编辑的GridPanel
  5. XSS的原理分析与解剖:第三章(技巧篇)**************未看*****************
  6. eclipse中svn的各种图标详解
  7. Ubuntu 16.04系统上修改Docker镜像的存储路径 (转)
  8. ajax请求是的动画实现
  9. SonarQube搭建和使用教程
  10. CodeForces-1249D2-Too Many Segments (hard version) -STL+贪心