在不改变 UIButton frame 的情况下如何扩大它的响应区域?

方法

override point 方法

1
2
3
4
5
6
7
8
class ExtendButton: UIButton {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
// 要扩大的事件响应范围偏移
let offset: CGFloat = -10
let bounds = self.bounds.insetBy(dx: offset, dy: offset)
return bounds.contains(point)
}
}