2017-05-02 44 views
1

我已將ImageView作爲子視圖添加到Swift中的集合視圖中。本子視圖的初始點在viewDidLoad()的錨點中進行佈置。但是,在視圖中點擊手勢後退出鍵盤後,ImageView會回到原始位置。我怎麼能最好地解決這個問題?如何保持subview,移動平移手勢,在辭職鍵盤後的位置?

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

請分享一些代碼和視覺效果,以幫助我們更好地理解你的問題。 – dmorrow

+0

我添加了一些代碼。希望這將有助於回答我的問題。一旦視圖被加載,這給我一個在屏幕中間的圖像。之後我可以隨意拖動它。但是,只有在退出鍵盤後,圖像纔會回到原來的位置。 – Bob

回答

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) 
} 
+0

謝謝你的回答。不幸的是,它不適合我。如果我已經解決了問題,我會努力解決問題。 – Bob