IOS中的事件
响应者对象
继承了UIResponder 的对象才能接收并处理事件,我们称之为"响应者对象" UIApplication 、UIViewController 、UIView 都是继承自UIResponder ,因此他们都是响应者对象,都能接受并处理事件
触摸事件
- (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)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;
远程控制事件
- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event;
NSSet 一个无序的Array ,NSSet 可以叫做集合,里面放着UITouch ,一个 就是一个手指UITouch 是手指触碰产生的对象,有多个属性,例如点击屏幕持续时间,阶段,快速点击次数,点击的点,上一个点击的点UIView 里面有个属性是window ,能获取当前的window ,UITouch 也有
UITouch
@property(nonatomic,readonly) NSTimeInterval timestamp;
@property(nonatomic,readonly) UITouchPhase phase;
@property(nonatomic,readonly) NSUInteger tapCount;
@property(nullable,nonatomic,readonly,strong) UIWindow *window;
@property(nullable,nonatomic,readonly,strong) UIView *view;
- (CGPoint)locationInView:(nullable UIView *)view;
- (CGPoint)previousLocationInView:(nullable UIView *)view;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
UITouch *touch = touches.anyObject;
}
多点触控
touches 里面都取出来就行了
for(UITouch *touch in touches){
}
检查用户交互开启
User Interaction Enabled 记得打开,在StoryBoard 文件的属性里面就可以设置- 控件隐藏
- 透明度小于等于
0.01 - 子视图超出父控件的有效范围
hitTest函数 响应者链条
该方法会被系统调用(可重写),在视图的层次结构中寻找到一个最适合的 view (理解为最上层view )来响应触摸事件,如果返回为nil ,即事件有可能被丢弃 hitTest 会检查每个层的用户交互(上面四个)
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
touch(UIEvent)->UIApplication->UIWindow->window.subviews->...->view
|