UILongPressGestureRecognizer 未调用其目标方法
UILongPressGestureRecognizer not calling its target method
这适用于 iOS 11 上的设备,但当我的设备更新到 iOS 12 时它不再适用:
//the viewcontroller is initiated with UIGestureRecognizerDelegate
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
//in cellForRowAt:
longPressGesture.minimumPressDuration = 1.0
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
cell.addGestureRecognizer(longPressGesture)
@objc func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
//never called
}
我还尝试将手势识别器添加到 viewDidLoad 中的按钮,以确保它不是 tableview 的问题,并且仍然没有调用 longPress 函数。
//the viewcontroller is initiated with UIGestureRecognizerDelegate
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
看起来您正在尝试使 longPressGesture
成为您的 UIViewController 的一个实例 属性,同时将目标和操作作为其初始值设定项的一部分。这是行不通的,因为在初始化时,目标 self
不是实例。还没有实例;该实例是我们正在创建的!
相反,将该行移动到 cellForRowAt:
,如下所示:
//in cellForRowAt:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
cell.addGestureRecognizer(longPressGesture)
这适用于 iOS 11 上的设备,但当我的设备更新到 iOS 12 时它不再适用:
//the viewcontroller is initiated with UIGestureRecognizerDelegate
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
//in cellForRowAt:
longPressGesture.minimumPressDuration = 1.0
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
cell.addGestureRecognizer(longPressGesture)
@objc func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
//never called
}
我还尝试将手势识别器添加到 viewDidLoad 中的按钮,以确保它不是 tableview 的问题,并且仍然没有调用 longPress 函数。
//the viewcontroller is initiated with UIGestureRecognizerDelegate let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
看起来您正在尝试使 longPressGesture
成为您的 UIViewController 的一个实例 属性,同时将目标和操作作为其初始值设定项的一部分。这是行不通的,因为在初始化时,目标 self
不是实例。还没有实例;该实例是我们正在创建的!
相反,将该行移动到 cellForRowAt:
,如下所示:
//in cellForRowAt:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
cell.addGestureRecognizer(longPressGesture)