首页 > 解决方案 > 如何在编辑文本字段时禁用手势识别器?

问题描述

我有一个 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)
    }

}


}

谢谢你的帮助!

标签: iosswift

解决方案


与其尝试禁用手势,不如在编辑时在手势动作中什么都不做呢?

@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)
    }

}

推荐阅读