![](/img/trans.png)
[英]How do I enable a single touch to handle both UILongPressGestureRecognizer and UIPanGestureRecognizer?
[英]Recognize both UILongPressGestureRecognizer and UIPanGestureRecognizer on same UIButton
我想制作一個UIButton
,當你長按它時,它會開始錄制視頻,如果你垂直向上平移手指(同時仍然長按),視頻會放大。
在我的按鈕上,我添加了一個UILongPressGestureRecognizer
和一個UIPanGestureRecognizer
來實現這一點。 單獨地,他們工作。 但是,它們不能一起工作。
如何在長按時錄制按鈕並允許我平移手指並識別它? 這就是我添加識別器的方式:
let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
button.addGestureRecognizer(long)
let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
button.addGestureRecognizer(pan)
您需要確認這兩個手勢的代表。 例如:
let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
long.delegate = self
button.addGestureRecognizer(long)
let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
pan.delegate = self
button.addGestureRecognizer(pan)
並且有一個委托方法可以同時識別多個手勢。
手勢識別器(_:shouldRecognizeSimultaneouslyWith:)
在你的類中定義它並返回true。
你會得到你想要的。
我知道這不完全是問題所問的問題,但您實際上可以繞過必須使用gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
並使用UILongPressGestureRecognizer
作為使用UIGestureRecognizer.State
更改的UIPanGestureRecognizer
。 這就是我過去所做的,清理事情並且比擁有兩個手勢識別器更合乎邏輯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.