当我对视图进行平移时,它没有做出反应。以下代码显示了我如何设置手势识别器。我的代码出了什么问题?
class MyViewController : UIViewController {
override func loadView() {
self.view.addGestureRecognizer(gestureRecognizerForSetCardFace)
}
var gestureRecognizerForSetCardFace: UIPanGestureRecognizer = {
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(handleSetGesture(gesture:)))
recognizer.maximumNumberOfTouches = 1
return recognizer
}()
@objc func handleSetGesture(gesture: UIPanGestureRecognizer){
print("pan")
}
}
答案 0 :(得分:1)
你压倒loadView
。如果您实际创建了一个视图并将其分配给self.view
,那么这只应该被覆盖。
更改代码以覆盖viewDidLoad
。
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(gestureRecognizerForSetCardFace)
}
答案 1 :(得分:0)
您需要添加一些东西 -
Lazy var
。 当属性的初始值为时,延迟属性很有用 取决于外部因素,其值直到a之后才知道 实例的初始化已完成。
lazy var gestureRecognizerForSetCardFace: UIPanGestureRecognizer = {
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(handleSetGesture(gesture:)))
recognizer.maximumNumberOfTouches = 1
return recognizer
}()
viewdidLoad
在控制器的视图加载到内存后调用。
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(gestureRecognizerForSetCardFace)
}