開發(fā)中創(chuàng)建按鈕桅打,然后通過addTarget:監(jiān)聽按鈕的點擊事件页徐。我們希望按鈕的大小不變(和圖片一致)豌习,希望增大按鈕的點擊區(qū)域存谎。
可以通過以下方法:
1.自定義點擊按鈕
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
//此處需要獲取bounds,非frame
CGRect bounds = self.bounds;
CGSize maxClickSize = (self.clickSize.width != 0 && self.clickSize.height != 0)? self.clickSize : CGSizeMake(bounds.size.width * 2, bounds.size.height * 2);
CGFloat widthExtenstion = MAX(maxClickSize.width - bounds.size.width, 0);
CGFloat heightExtenstion = MAX(maxClickSize.height - bounds.size.height, 0);
bounds = CGRectInset(bounds, -0.5 * widthExtenstion, -0.5*heightExtenstion);
return CGRectContainsPoint(bounds, point);
}
- (void)create
{
UIImage *buttonImg = [UIImage imageNamed:@"compose_close_button"];
_cancelButton.cg_size = buttonImg.size;
_cancelButton.clickSize = CGSizeMake(50, 50);
}
2.可以通過hitTest:withEvent:設(shè)置按鈕的放大區(qū)域
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect covertRect = [self convertRect:self.button.frame fromView:window];
CGRectInset(covertRect, -10, -10);
if(CGRectContainsPoint(covertRect, point)) {
return button;
}
return [super hitTest:point withEvent:event];
}
判斷點是否在rect區(qū)域時肥隆,需要轉(zhuǎn)換到同一坐標(biāo)系
使用建議:建議自定義按鈕既荚,需要增大點擊區(qū)域,很方便使用栋艳。