知识

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 方法由对应的视图去完成,上级视图不再接管。

UI View 不接收处理事件的三种情况

上一篇 下一篇

猜你喜欢

热点阅读