使用方法,我創建了一個協議,該協議的擴展:在協議
protocol SomeProtocol: class {
var someView: UIView { get set }
func handlePan(recognizer: UIPanGestureRecognizer)
}
extension SomeProtocol where Self: UIViewController {
func handlePan(recognizer: UIPanGestureRecognizer) {
// implementation
}
}
class SomeViewController: UIViewController, SomeProtocol {
var someView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
someView.frame = CGRect(x: 100, y: 200, width: 50, height: 50)
someView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan)))
someView.backgroundColor = .black
view.addSubview(someView)
}
}
但是,這是給我的錯誤的IM創造UIPanGestureRecognizer
:
Error: Argument of '#selector' refers to instance method 'handlePan(recogniser:)' that is not exposed to Objective-C
是否有方法來解決這個問題,而不是在視圖控制器中添加handlePan
方法?
由於該方法需要簽名應該是'#selector參數(handlePan(_ :) )' – vadian
@vadian實際上,正式簽名將是'#selector(handlePan(recognitionizer :))' - 儘管'handlePan'是明確的,'#selector(handlePan)'是完全可以接受的。 – Hamish