子控件不在父控件上能点击的解决办法

235 阅读1分钟

hitTest方法:子控件不在当前的父控件的bounds上.重写父控件的hitTest方法,内部代码

  1. 先调用super hitTest
  2. 转化坐标判断触摸点在不在这个子控件的bounds上,在就返回.
  • (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView * view = [super hitTest:point withEvent:event]; if (view == nil) { // 转换坐标系 CGPoint newPoint = [commentImageView convertPoint:point fromView:self]; // 判断触摸点是否在button上 if (CGRectContainsPoint(commentImageView.bounds, newPoint)) { view = commentImageView; } } return view; }