是否可以在UITextView上实现长按手势?基本上,如果用户在文本视图上点击一次,我希望他/她能够编辑文本。但是,如果他/她点击并按住文本视图(让我们说两秒钟),将执行一个动作?如果答案是肯定的,请指导我如何实现这一目标?
按照kchromik的asnwer解决我的问题:
(1)第一步是在ViewController类开始之前定义以下扩展名:
extension ViewController: UIGestureRecognizerDelegate { func gestureRecognizer (_ gestureRecognizer: UIGestureRecognizer, shouldRecognizerSimultaneouslyWithotherGestureRecognizer: UIGestureRecognizer) -> Bool { return true}}
(2)第二步是将UITextView Outlet从主故事板链接到swift代码文件:
@IBOutlet weak var testTextView: UITextView!
(3)第三步是从想要在其上实现Longpress Gesture识别器的UITextView顶部的对象库中拖放GestureRecognizer。
(4)第四步是在viewDidLoad(){
下添加以下代码 let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)))
uilpgr.minimumPressDuration = 2
testTextView.addGestureRecognizer(uilpgr)
uiplgr.delegate = self
}
(5)最后一步是定义与之前定义的Longpress Gesture Recognizer一起使用的函数:
func longpress(gestureRecognizer: UIGestureRecognizer) {
print("Long tap") // Execute what you want to do
}
答案 0 :(得分:0)
默认情况下,UILabel
禁用了用户互动。在testLabel.isUserInteractionEnabled = true
中尝试viewDidLoad
或在故事板中启用它:
<强>更新强>
如果您UIView
拥有自己的手势识别器,则可以实施以下代理:
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
你不要忘记设置uilpgr.delegate = self