繁体   English   中英

在同一个 UIButton 上同时识别 UILongPressGestureRecognizer 和 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM