如何扩展UIButton的hitTest区域而不扩展其背景图像? [英] How to expand the hitTest area of a UIButton without extruding it's background image?

查看:67
本文介绍了如何扩展UIButton的hitTest区域而不扩展其背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了UIButton的背景图像并在其上放了一个标题(我使用的是setBackgroundImage方法而不是setImage).现在,我想扩展UIButton的hitTest区域而不扩展它的背景图像.

I have set the UIButton's background image and put a title on it(I used setBackgroundImage method not setImage). Now I want to expand the hitTest area of a UIButton without extrude it's background image.

我该怎么做?

推荐答案

好吧,您可以扩展UIButton并覆盖UIView的hitTest方法:

Well, you could extend UIButton and override UIView's hitTest method:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    int expandMargin = 20;
    CGRect extendedFrame = CGRectMake(0 - expandMargin , 0 - expandMargin , self.bounds.size.width + (expandMargin * 2) , self.bounds.size.height + (expandMargin * 2));
    return (CGRectContainsPoint(extendedFrame , point) == 1) ? self : nil;
}

这篇关于如何扩展UIButton的hitTest区域而不扩展其背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆