我有一个文本字段,用户可以拖动它来移动。如果您没有打开键盘(即textField不处于活动状态),则可以正常工作。但是,如果键盘是打开的,并且您移动了文本字段,则在关闭键盘时它将重置为原始位置。
关闭键盘后,我的所有其他手势(“捏”,“旋转”)保持正确,只有位置重置。
@objc func addTextField() {
let textField = UITextField.makeTextField()
textField.delegate = self
view.addSubview(textField)
textField.anchorCenterSuperview()
textField.becomeFirstResponder()
addGestures(to: textField)
}
func addGestures(to textField: UITextField) {
//add pan gesture
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
gestureRecognizer.delegate = self
textField.addGestureRecognizer(gestureRecognizer)
//Enable multiple touch and user interaction for textfield
textField.isUserInteractionEnabled = true
textField.isMultipleTouchEnabled = true
//add pinch gesture
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchRecognized(pinch:)))
pinchGesture.delegate = self
textField.addGestureRecognizer(pinchGesture)
//add rotate gesture.
let rotate = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotate(recognizer:)))
rotate.delegate = self
textField.addGestureRecognizer(rotate)
}
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
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)
}
}
// MARK: Handle Pinch
@objc func pinchRecognized(pinch: UIPinchGestureRecognizer) {
if let view = pinch.view {
view.transform = view.transform.scaledBy(x: pinch.scale, y: pinch.scale)
pinch.scale = 1
}
}
// MARK: Handle Rotate
@objc func handleRotate(recognizer : UIRotationGestureRecognizer) {
if let view = recognizer.view {
view.transform = view.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
}
}
//MARK:- UIGestureRecognizerDelegate Methods
func gestureRecognizer(_: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
return true
}
private extension UITextField {
static func makeTextField() -> UITextField {
let tf = UITextField()
tf.placeholder = ""
tf.font = UIFont.boldSystemFont(ofSize: 36)
tf.textColor = .white
tf.borderStyle = UITextField.BorderStyle.none
tf.autocorrectionType = UITextAutocorrectionType.no
tf.keyboardType = UIKeyboardType.default
tf.returnKeyType = UIReturnKeyType.done
tf.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
tf.backgroundColor = .red
return tf
}
}
extension UIView() {
public func anchorCenterXToSuperview(constant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
if let anchor = superview?.centerXAnchor {
centerXAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
}
}
public func anchorCenterYToSuperview(constant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
if let anchor = superview?.centerYAnchor {
centerYAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
}
}
public func anchorCenterSuperview() {
anchorCenterXToSuperview()
anchorCenterYToSuperview()
}
}
原始位置
在没有键盘的情况下移动位置
键盘消失后,它会恢复到原始位置