【发布时间】:2017-12-08 05:04:38
【问题描述】:
是否可以在 UITextField 上使用 UILongPressGestureRecognizer 而不触发字段编辑,同时仍然能够在常规点击时编辑文本字段?
我尝试向 UITextField 添加长按手势识别器,但长按似乎只在一小部分时间内起作用。
init(frame: CGRect, userCompany: WLUserCompany) {
super.init(frame: frame)
var textField: UITextField?
var longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(gesture:)))
textField?.addGestureRecognizer(longPress)
self.addSubview(textField!)
}
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
【问题讨论】:
-
你想对 UILongPressGestureRecognizer 上的 UITextField 做什么?
-
@Maddyヅヅ 我想让一个按钮出现在 UITextField 旁边只长按
-
可能默认的
UIPressGestureRecognizer与您的UILongPressGestureRecognizer冲突。我认为你可以改变行为,例如,当这个UITextField被聚焦时显示按钮。 -
无论如何,我从未见过有这种行为的应用程序:长按文本字段时显示隐藏按钮。
-
@Macabeus 我也在考虑在顶部放置一个透明层 - 唯一的问题是我只想处理长按的情况,同时让其余的手势通过层在它后面。不确定如何停止传播是否发生了特定类型的手势。
标签: ios iphone swift uitextfield uigesturerecognizer