我已将一个ImageView
作为子视图添加到Swift中的集合视图中。此子视图的初始点使用viewDidLoad()
中的锚点进行布局。但是,在视图中点击手势后重新调整键盘后,ImageView
会返回到原始位置。我怎么能最好地解决这个问题?
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
}
答案 0 :(得分:0)
我假设键盘被解除后,调用view.setNeedsLayout()
,将约束重置为原始位置。
仅使用约束来尝试此操作,而不是调整边界(或中心)
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)
}