iOS中事件響應(yīng)會(huì)先依次調(diào)用個(gè)層級view的[UIView pointInSide] 方法, 如果返回true, 則會(huì)走改view的hittest方法, 所以如果我們想讓view或者它的子view響應(yīng)超出它的frame的點(diǎn)擊事件, 則需要讓view的pointInside方法返回為true, 然后在hittest方法中通過轉(zhuǎn)換坐標(biāo)的方法, 來選擇我們想要處理該事件的targetView, 調(diào)用targetView.hittest方法, 即可.
案例:
- cell上的子view想要響應(yīng)其frame以外的點(diǎn)擊事件, 此處注意子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)
}
}