iOS中事件响应会先依次调用个层级view的[UIView pointInSide] 方法, 如果返回true, 则会走改view的hittest方法, 所以如果我们想让view或者它的子view响应超出它的frame的点击事件, 则需要让view的pointInside方法返回为true, 然后在hittest方法中通过转换坐标的方法, 来选择我们想要处理该事件的targetView, 调用targetView.hittest方法, 即可.
案例:
- cell上的子view想要响应其frame以外的点击事件, 此处注意子view是加在contentView上的
extension BigImageCell {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
for subv in self.contentView.subviews {
let newP = subv.convert(point, from: self.contentView)
if subv.bounds.contains(newP) {
return subv.hitTest(newP, with: event)
}
}
return super.hitTest(point, with: event)
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for subv in self.contentView.subviews {
let localPoint = subv.convert(point, from: self.contentView)
if subv.bounds.contains(localPoint) {
return true
}
}
return super.point(inside: point, with: event)
}
}