理解iOS的事件传递

2020-04-02  本文已影响0人  hui8685291

理解事件传递

  1. 点是否落在目标视图上
  2. 如果需要扩大目标视图的点击范围(热区),则需要触碰的点是在在目标视图的父视图范围上,否则无效;
  3. 如果目标视图被其他视图遮盖住了,此时还想目标视图触发事件,此时利用事件传递的方式,hitTest:event:方法,找到合适的view,让其处理事件.

CGRectContainsPoint(Rect, Point); //判断Rect中是否包含Point
CGRectContainsRect(Rect1,Rect2); //判断Rect1中是否包含Rect2

CGPoint redCenterInView = [self.grayView convertPoint:self.redView.center toView:self.view];

CGPoint redCenterInView = [self.view convertPoint:self.redView.center fromView:self.grayView];

1.使用convertPoint:toView:时,调用者应为covertPoint的父视图。即调用者应为point的父控件。toView即为需要转换到的视图坐标系,以此视图的左上角为(0,0)点。
2.使用convertPoint:fromView:时正好相反,调用者为需要转换到的视图坐标系。fromView为point所在的父控件。
3.toView可以为nil。此时相当于toView传入self.view.window

-(CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;//点转换
-(CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;//点转换
-(CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;//矩形转换
-(CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;//矩形转换

利用以下pointInside方法进行判断点是否落在在其内部;

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; // default returns YES if point is in bounds
上一篇 下一篇

猜你喜欢

热点阅读