方法:
为UIButton增加一个分类,在分类中重写UIButton的pointInside方法,在该方法中改变UIButton的bounds
代码:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
[super pointInside:point withEvent:event];
//获取bounds 实际大小
CGRect bounds = self.bounds;
if (self.clickArea) {
CGFloat area = [self.clickArea floatValue];
CGFloat widthDelta = MAX(area * bounds.size.width - bounds.size.width, .0);
CGFloat heightDelta = MAX(area * bounds.size.height - bounds.size.height, .0);
//扩大bounds
bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
}
//点击的点在新的bounds 中 就会返回YES
return CGRectContainsPoint(bounds, point);
}
网友评论