Ios Swift Override UItextField的双击
我想在UITextField上双击编写一个自定义代码,并阻止默认文本编辑和popup键盘。 我已经尝试了以下,迄今没有任何工作。 请帮我解决这个问题。
let gestureArray = NamTxtBoxVal.gestureRecognizers var tapGesture = UITapGestureRecognizer() for idxVar in gestureArray! { if let tapVar = idxVar as? UITapGestureRecognizer { if tapVar.numberOfTapsRequired == 2 { tapGesture = tapVar NamTxtBoxVal.removeGestureRecognizer(tapGesture) } } } let doubleTap = UITapGestureRecognizer(target: self, action: #selector(namFnc(_:))) doubleTap.numberOfTapsRequired = 2 doubleTap.delegate = self tapGesture.requireGestureRecognizerToFail(doubleTap) NamTxtBoxVal.addGestureRecognizer(doubleTap)
我也试过:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { return false }
我知道你做这件事的唯一方法是
- 把一个UIView放在UITextField后面
- 设置textField的userInteractionEnabled = false
- 将双击手势添加到UIView
这将允许您在TextField区域注册双击,而不是popup任何键盘或进入编辑模式。
不知道你计划在双击后用textField做什么,但你应该能够以编程方式处理大多数东西。
代码是这样的:
class ViewController: UIViewController { @IBOutlet weak var myViewBehindMyTextField: UIView! @IBOutlet weak var myTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myTextFieldTapped(_:))) tapGesture.numberOfTapsRequired = 2 myViewBehindMyTextField.addGestureRecognizer(tapGesture) } func myTextFieldTapped(sender: UITapGestureRecognizer) { print("Double tapped on textField") } }
- iOS:使用模式pageFlip在UITableViewController中产生奇怪的单元格animation
- 如果通过performSelectorInBackground控制UI,会发生什么情况?
- swift:如何截图AVPlayerLayer()
- 我应该在Objective-C中使用ivars吗?
- OTA(无线)IOS 7.1更改后http-https仍无法连接服务器?
- 为什么它不能在快速2中显示任何东西?
- 苹果推送通知 – 没有有效的“APS环境”尝试了我在网上看到的所有解决scheme?
- 将RealmSwift作为子项目添加:缺less所需的模块:“Realm.Private”,“Realm”
- 从iOS相机捕获图片
- 我们如何在iOS设备上运行时处理启用/禁用背景audiofunction?
- iOS 6.0:UISplitViewController Popover损坏或devise由Apple更改?