【发布时间】:2021-08-17 18:20:20
【问题描述】:
如果手势 minimumPressDuration 设置为“.zero”,MyView 和 ViewController 的 touchesBegan 将不会触发,但每当我将其设置为“.leastNormalMagnitude”时它就会触发。代码如下:
class ViewController: UIViewController {
let mySuperView: UIView = {
let v = UIView()
v.backgroundColor = .blue
v.frame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 500, height: 500))
return v
}()
let mySubview: MyView = {
let v = MyView()
v.backgroundColor = .orange
v.frame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 250, height: 250))
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(mySuperView)
mySuperView.addSubview(mySubview)
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(tapped(_:)))
gesture.minimumPressDuration = .zero // when it's set to ".leastNormalMagnitude",
// MyView's and VC's touchesBegan fires.
mySuperView.addGestureRecognizer(gesture)
}
@objc func tapped(_ gesture: UITapGestureRecognizer) {
print("long pressed.")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan in vc") // -> Won't get called.
super.touchesBegan(touches, with: event)
}
}
class MyView: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan in MyViewClass") // -> Won't get called.
super.touchesBegan(touches, with: event)
}
}
我想我可以将它与“.leastNormalMagnitude”一起使用,但我仍然想知道为什么会这样?
【问题讨论】:
-
按零时间是什么意思?我想这意味着你没有按下它。
-
@matt 仅用于激活时间。我希望长按手势立即触发。
-
但这不会是一个长按
标签: ios swift touchesbegan