スウィフト5
タプジェスチャ付きスーパービューのボタン
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if let _ = touch.view as? UIButton { return false }
return true
}
私の場合、 hitTestの実装は私のために働きました。ボタン付きのコレクションビューがありました
このメソッドは、point(inside:with:)
各サブビューのメソッドを呼び出して、どのサブビューがタッチイベントを受信するかを決定することにより、ビュー階層をトラバースします。trueが返された場合point(inside:with:)
、指定されたポイントを含む最前面のビューが見つかるまで、サブビューの階層が同様にトラバースされます。
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard isUserInteractionEnabled else { return nil }
guard !isHidden else { return nil }
guard alpha >= 0.01 else { return nil }
guard self.point(inside: point, with: event) else { return nil }
for eachImageCell in collectionView.visibleCells {
for eachImageButton in eachImageCell.subviews {
if let crossButton = eachImageButton as? UIButton {
if crossButton.point(inside: convert(point, to: crossButton), with: event) {
return crossButton
}
}
}
}
return super.hitTest(point, with: event)
}