我有一个UITextView,它具有一个平移手势识别器,因此可以在屏幕上拖动。我正在尝试在编辑文本字段时禁用此手势识别器,以使文本停留在屏幕中间,并且用户无法将其拖动到键盘下方。
我尝试了this和this之类的答案,但似乎无法使其正常工作。我还尝试过使用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)
}
}
}
感谢您的帮助!
答案 0 :(得分:0)
与其尝试禁用手势,不如在编辑时不执行手势操作?
<form action="{{ route('offices.update', ['id'=>$office->id,'locale'=>app()->getLocale()])}}" method="POST">
答案 1 :(得分:0)
private func getPanGesture() -> [UIPanGestureRecognizer]? {
let panGesture = textField.gestureRecognizers?.filter({$0 is UIPanGestureRecognizer})
return panGesture as? [UIPanGestureRecognizer]
}
private func disablePanGesture() {
let enabledGestures = getPanGesture()?.filter({$0.isEnabled})
enabledGestures?.forEach({ (gesture) in
gesture.isEnabled = false
})
}
private func enablePanGesture() {
let disabledGestures = getPanGesture()?.filter({!$0.isEnabled})
disabledGestures?.forEach({ (gesture) in
gesture.isEnabled = true
})
}
@objc func handleKeyboardShowNotification(notification: NSNotification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
..
disablePanGesture()
}
}
@objc func handleKeyboardHideNotification() {
enablePanGesture()
..
}