【发布时间】:2023-03-28 00:56:01
【问题描述】:
我希望能够创建一个具有超大响应区域的 UIButton。我知道这样做的一种方法是覆盖子类中的 hitTest 方法,但我如何首先实例化我的自定义按钮对象?
[OversizedButton buttonWithType: UIButtonTypeDetailDisclosure];
无法开箱即用,因为 buttonWithType 返回的是 UIButton,而不是 OversizedButton。
看来我也需要重写 buttonWithType 方法。有谁知道怎么做?
@implementation OversizedButton
+ (id)buttonWithType:(UIButtonType)buttonType
{
// Construct and return an OversizedButton rather than a UIButton
// Needs to handle special types such as UIButtonTypeDetailDisclosure
// I NEED TO KNOW HOW TO DO THIS PART
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// Return results if touch event was in oversized region
// I ALREADY KNOW HOW TO DO THIS PART
}
@end
或者,也许我可以使用 alloc/initWithFrame 创建按钮。但是 buttonType 属性是只读的,那么如何创建自定义按钮类型呢?
注意:我知道还有其他方法可以做到这一点,例如在可见按钮后面设置一个不可见按钮。我不喜欢这种方法,宁愿避免它。对上述方法的任何帮助都会非常有帮助。谢谢
【问题讨论】: