简体   繁体   中英

How to keep subview, moved with pan gesture, in position after resigning keyboard?

I have added an ImageView as a subview to a collection view in Swift. The initial point of this subview is laid out with anchor points in viewDidLoad() . However, after resigning the keyboard after a tap gesture in the view, the ImageView goes back to it's original position. How could I best solve this problem?

import UIKit

class MessageViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

     let imageButton: UIImageView = {
         let imageButtonView = UIImageView()
         imageButtonView.image = UIImage(named: "image.png")
         imageButtonView.layer.cornerRadius = 30
         imageButtonView.layer.masksToBounds = true
         imageButtonView.isUserInteractionEnabled = true
         return imageButtonView
     }()

     override func viewDidLoad() {
         super.viewDidLoad()

         //add imageButton
         view.addSubview(imageButton)

     //        recognize tapgesture for removing keyboard
         let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tap(gesture:)))
         self.view.addGestureRecognizer(tapGesture)

         let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(gesture:)))
         imageButton.addGestureRecognizer(panGesture)
     }


    func handlePan(gesture: UIPanGestureRecognizer) {

        let translation = gesture.translation(in: self.view)
            if let view = gesture.view {
                view.center = CGPoint(x:view.center.x + translation.x, y:view.center.y + translation.y)
    }
    gesture.setTranslation(CGPoint.zero, in: self.view)
  }


    imageButton.translatesAutoresizingMaskIntoConstraints = false

    imageButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imageButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 30).isActive = true
    imageButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    imageButton.widthAnchor.constraint(equalToConstant: 60).isActive = true

 }

I assume that after the keyboard is dismissed, view.setNeedsLayout() is called, which resets the constraints to their original positions.

Try this using only constraints, not adjusting the bounds (or center)

let imageCenterXConstraint:NSLayoutConstraint = NSLayoutConstraint()
let imageCenterYConstraint:NSLayoutConstraint = NSLayoutConstraint()

func viewDidLoad() {
    super.viewDidLoad()

    //add imageButton
    view.addSubview(imageButton)

    //        recognize tapgesture for removing keyboard
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tap(gesture:)))
    self.view.addGestureRecognizer(tapGesture)

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(gesture:)))
    imageButton.addGestureRecognizer(panGesture)
    imageButton.translatesAutoresizingMaskIntoConstraints = false

    imageCenterXConstraint = imageButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
    imageCenterXConstraint.isActive = true
    imageCenterYConstraint = imageButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 30)
    imageCenterYConstraint.isActive = true
    imageButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    imageButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
}

func handlePan(gesture: UIPanGestureRecognizer) {
    let translation = gesture.translation(in: self.view)
    if let view = gesture.view {
        imageCenterXConstraint.constant = translation.x
        imageCenterYConstraint.constant = 30 + translation.y
        view.layoutIfNeeded()
    }
    gesture.setTranslation(CGPoint.zero, in: self.view)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM