gpt4 book ai didi

swift - UIGestureRecognizer 没有响应 swift 3.0

转载 作者:行者123 更新时间:2023-11-28 15:12:56 25 4
gpt4 key购买 nike

当我在 View 上平移时,它没有反应。以下代码显示了我如何设置手势识别器。我的代码有什么问题?

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")
}
}

最佳答案

您正在覆盖 loadView。只有当您实际创建一个 View 并将其分配给 self.view 时,才应该覆盖它。

更改您的代码以覆盖 viewDidLoad

override func viewDidLoad() {
super.viewDidLoad()

self.view.addGestureRecognizer(gestureRecognizerForSetCardFace)
}

关于swift - UIGestureRecognizer 没有响应 swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47343720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com