iOS_UITouch 事件
2015-10-02 本文已影响1487人
738bc070cd74
UITouch 基本事件函数
UITouch 包含如下四个基本函数,touches 集合中存储的事UITouch 的集合,UITouch包含了触摸所在的窗口、视图、当前点击位置、上一次点击位置。
- (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:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
在编写代码的时候,可以通过设置是否接受用户交互和多点触摸
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
获取 UITouch 对象
UITouch *touch = [touches anyObject];
移动的偏移量
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
CGPoint preLocation = [touch previousLocationInView:self.view];
CGPoint offset = CGPointMake(location.x - preLocation.x, location.y - preLocation.y);
}
响应者链条
当用户点击屏幕时,会产生一个 UITouch 对象,传递给 UIApplication,然后由 window 负责查找最合适响应触摸事件的对象。由 window 以递归的方式调用界面上的所有视图的 hitTest 方法。找到合适的视图之后,Touch 方法由对应的视图去完成,上级视图不再接管。
- UIResponder 有一个 nextResponder 属性,通过该属性可以组成一个响应者链,事件或消息在其路径上进行传递;
- 如果UIResponder 没有处理传给它的事件,会讲未处理的消息发给它自己的 nextResponder;
- 事件最终被传递给 UIapplication 对象;
- 如果没有配置任何对象来处理事件,该事件被丢弃。
UI View 不接收处理事件的三种情况
- userInteractionEnabled = NO;
- hidden = YES;
- alpha = 0-0.01;