ios - 如何在编辑文本字段时禁用手势识别器?
问题描述
我有一个 UITextView,它有一个平移手势识别器,所以它可以在屏幕上拖动。我试图在编辑文本字段时禁用此手势识别器,以便文本保持在屏幕中间,并且用户无法将其拖到键盘下方。
我已经尝试过这样的答案,但似乎无法让它们工作。我也尝试使用删除手势识别器,textField.removeGestureRecognizer(textField.panGestureRecognizer)
但也无法使其正常工作。有谁知道这里出了什么问题?
这是我的代码;
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
let textField = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
//the current center of the text being edited
var currentCenter = CGPoint()
override func viewDidLoad() {
super.viewDidLoad()
// tap gesture recognizer for keyboard hide
let HideKeyboardTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(HideKeyboardTap)
textField.text = "hello"
textField.font = UIFont.systemFont(ofSize: 80)
textField.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
textField.sizeToFit()
self.view.addSubview(textField)
//Enable multiple touch and user interaction for textfield
textField.isUserInteractionEnabled = true
textField.isMultipleTouchEnabled = true
//add pan gesture
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGestureRecognizer.delegate = self
textField.addGestureRecognizer(panGestureRecognizer)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShowNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHideNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
@objc func handleKeyboardShowNotification(notification: NSNotification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
currentCenter = textField.center
textField.center = CGPoint(x: UIScreen.main.bounds.midX , y: UIScreen.main.bounds.height - (keyboardHeight + textField.bounds.height + 10))
textField.panGestureRecognizer.isEnabled = false
}
}
@objc func handleKeyboardHideNotification() {
textField.panGestureRecognizer.isEnabled = true
textField.center = currentCenter
}
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
}
谢谢你的帮助!
解决方案
与其尝试禁用手势,不如在编辑时在手势动作中什么都不做呢?
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if(textField.isEditing){return;}
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
推荐阅读
- python-3.x - 在 bash 中多行打印 python 文件的结果
- amazon-web-services - 以编程方式检查用户是否对 S3 存储桶具有某些权限
- javascript - 如何在运行函数之前弹出或提示验证密码
- c++ - 当前的实例化是什么?
- python - Pymongo 无法连接到数据库:pymongo.errors.ServerSelectionTimeoutError: connection closed
- reactjs - 如何在静态服务器上托管 ReactJS 项目
- excel - 如何在excel中的两个日期列之间查找日期
- python - Django - 如何为 values_list 中的每个元组添加一个值
- celery - Celery 任务在后端失败时重试
- sql - 别名、分组依据、拥有