我想通过添加自定义UITableView
来扩展UIPanGestureRecognizer
:
extension UITableView {
func addCustomPanGestureRecognizer() {
let panGestureRecognizer = UIPanGestureRecognizer()
panGestureRecognizer.delegate = self
addGestureRecognizer(panGestureRecognizer)
// some additional setup
// ...
}
}
由于自定义手势识别器会干扰滚动,因此我尝试通过可选要求实现
UIGestureRecognizerDelegate
:extension UITableView: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return otherGestureRecognizer == self.panGestureRecognizer
}
}
...但是尽管委托给tableView(如上所示),但它根本没有被调用。
另外,Xcode显示警告:
实例方法“ gestureRecognizer(:shouldRecognizeSimultaneousWithWith :)”几乎与协议“ UIGestureRecognizerDelegate”的可选要求“ gestureRecognizer(:shouldRecognizeSimultaneousWithWith :)”匹配
为了摆脱警告,我尝试在方法定义之前添加
@objc
批注,但是没有运气。 最佳答案
事实证明,Xcode在寻找合适的Objective-C方法定义时还需要其他帮助。为此,我必须在@objc
注释后提供方法签名:
extension UITableView: UIGestureRecognizerDelegate {
@objc(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return otherGestureRecognizer == self.panGestureRecognizer
}
}
该警告仍然显示,但是该方法已正确调用。