我的UITableViewCell中有一个UIButton
我做了一个UIButton的子类覆盖了pointInside函数:
var touchMargin:CGFloat = 20.0 override func pointInside(point: CGPoint,withEvent event: UIEvent?) -> Bool { let extendedArea = CGRectInset(self.bounds,-touchMargin,-touchMargin) return CGRectContainsPoint(extendedArea,point) }
但是,触摸区域不会增加.如果我触摸到UIButton外部,触摸表格单元格.
由于将按钮放置在单元格中,此代码是否无法正常工作?
如何解决?
解决方法
您应该在要放大的视图上覆盖hitTest.
例如
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) { return nil; } CGRect touchRect = CGRectInset(self.bounds,-10,-10); if (CGRectContainsPoint(touchRect,point)) { //TODO!? check supviews return self; } return nil; }