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 = 0, // 3D Touch检测失败 UIForceTouchCapabilityUnavailable = 1, // 3D Touch不可用 UIForceTouchCapabilityAvailable = 2 // 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 << 0), //力度 UITouchPropertyAzimuth = (1UL << 1), //方位 UITouchPropertyAltitude = (1UL << 2), //高度 UITouchPropertyLocation = (1UL << 3), //位置 } 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; //触摸所在窗口