我们在实际研发中能不会全部使用到,着重梳理以下常常使用到的。
UIResponder
该类定义了对象响应和控制事件的接口,它的实例通常被称为应答对象。想要开启多点触控必须setMutipleTouchEnabled:这个消息给接受的视图对象。
[self.view setMultipleTouchEnabled:YES];
UIResponder用户触摸事件回调:
[- touchesBegan:withEvent:]
// 当有1个或多个手指接触到屏幕上的时候
[- touchesMoved:withEvent:]
// 当事件与event绑定并且手指在屏幕上移动的时候
[- touchesEnded:withEvent:]
// 手指移开的时候
[- touchesCancelled:withEvent:]
// 当系统事件中断了触摸事件的时候,比如系统低电量警告
[- touchesEstimatedPropertiesUpdated:]
Sent to the responder when the estimated properties for a touch have changed so that they are no longer estimated, or an update is no longer expected.
继承了UIControl的子类都可以通过addTarget添加事件,如果不是继承自它的控件可以设置userInteractionEnabled属性为yes,给这个控件添加手势,所有继承自UIResponder的子类都可以添加手势识别。
UIControl的常用事件:
typedef enum UIControlEvents : NSUInteger {
UIControlEventTouchDown = 1 << 0,
UIControlEventTouchDownRepeat = 1 << 1,
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12,
UIControlEventPrimaryActionTriggered = 1 << 13,
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
UIControlEventAllTouchEvents = 0x00000FFF,
UIControlEventAllEditingEvents = 0x000F0000,
UIControlEventApplicationReserved = 0x0F000000,
UIControlEventSystemReserved = 0xF0000000,
UIControlEventAllEvents = 0xFFFFFFFF
} UIControlEvents;
UIEvent
iOS将触摸事件定义为第一个手指开始触摸屏幕到最后一个手指离开屏幕定义为一个触摸事件。用类UIEvent表示。
@interface UIEvent : NSObject
@property(nonatomic,readonly) UIEventType type NS_AVAILABLE_IOS(3_0); // 触摸事件、加速器事件、远程控制事件
@property(nonatomic,readonly) UIEventSubtype subtype NS_AVAILABLE_IOS(3_0);
@property(nonatomic,readonly) NSTimeInterval timestamp;
#if UIKIT_DEFINE_AS_PROPERTIES
//UITouch SET
@property(nonatomic, readonly, nullable) NSSet *allTouches;
//省略部分代码
@end
UITouch
一个手指第一次点击屏,会形成一个UITouch对象,直到离开销毁。表示触碰。UITouch对象能表明了当前手指触碰的屏幕位置,状态。状态分为开始触碰、移动、离开。
根据定义,UIEvent实际包括了多个UITouch对象。有几个手指触碰,就会有几个UITouch对象。
//UITouch
@interface UITouch : NSObject
@property(nonatomic,readonly) NSTimeInterval timestamp;
@property(nonatomic,readonly) UITouchPhase phase;
@property(nonatomic,readonly) NSUInteger tapCount; // touch down within a certain point within a certain amount of time
@property(nonatomic,readonly) UITouchType type NS_AVAILABLE_IOS(9_0);
@property(ullable,nonatomic,readonly,strong) UIWindow *window;
@property(nullable,nonatomic,readonly,strong) UIView *view;
@property(nullable,nonatomic,readonly,copy) NSArray *gestureRecognizers
NS_AVAILABLE_IOS(3_2);
//省略部分代码
@end
//Touch 状态枚举
typedef NS_ENUM(NSInteger, UITouchPhase) {
UITouchPhaseBegan, // whenever a finger touches the surface.
UITouchPhaseMoved, // whenever a finger moves on the surface.
UITouchPhaseStationary, // whenever a finger is touching the surface but hasn't moved since the previous event.
UITouchPhaseEnded, // whenever a finger leaves the surface.
UITouchPhaseCancelled, // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)
};