即使调用view.layoutIfNeeded(),Swift iOS -Constraint也不是动画

时间:2018-06-17 05:45:41

标签: ios swift nslayoutconstraint uiviewanimation

我有一个圆形的LikeButton,它位于一个圆形的moreButton后面。 likeButton被固定到moreButton的centerX和centerY。当我按下moreButton时,我想在moreButton上面设置likeButton 200点的动画。

我使用NSLayoutConstraint来跟踪likeButton的centerY并对其进行更改。当我调用UIView.animate并在其关闭内部时,我调用self.view.layoutIfNeeded()视图不会更新。

@objc func moreButtonTapped(){

    likeButtonCenterY?.isActive = false
    likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
    likeButtonCenterY?.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

我哪里错了?

let moreButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "more"), for: .normal)
    button.tintColor = .red
    button.clipsToBounds = true
    button.addTarget(self, action: #selector(moreButtonTapped), for: .touchUpInside)
    return button
}()

let likeButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "like"), for: .normal)
    button.tintColor = .blue
    button.clipsToBounds = true
    return button
}()

var likeButtonCenterY: NSLayoutConstraint?

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    setAnchors()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    moreButton.layer.cornerRadius = moreButton.frame.size.width / 2
    likeButton.layer.cornerRadius = likeButton.frame.size.width / 2
}

@objc func moreButtonTapped(){

    likeButtonCenterY?.isActive = false
    likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
    likeButtonCenterY?.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func setAnchors(){
    view.addSubview(likeButton)
    view.addSubview(moreButton)

    moreButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    moreButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200).isActive = true
    moreButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    moreButton.heightAnchor.constraint(equalToConstant: 100).isActive = true

    likeButtonCenterY = likeButton.centerYAnchor.constraint(equalTo: moreButton.centerYAnchor)
    likeButtonCenterY?.isActive = true

    likeButton.centerXAnchor.constraint(equalTo: moreButton.centerXAnchor).isActive = true
    likeButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    likeButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

2 个答案:

答案 0 :(得分:1)

试试这个

@objc func moreButtonTapped(){

likeButtonCenterY?.isActive = false
likeButtonCenterY = likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
likeButtonCenterY?.isActive = true

UIView.animate(withDuration: 0.3) {
    self.view.layoutIfNeeded()
}
}

您忘了写likeButtonCenterY =

答案 1 :(得分:0)

如果要同时激活/停用多个约束(如批处理),则可能需要覆盖updateConstraints()方法。更多信息请访问:https://developer.apple.com/documentation/uikit/uiview/1622512-updateconstraints

基本上,moreButtonTapped()中的代码将包含在updateConstraints()方法的重写实现中。不要忘记在方法的最后一行调用super()

然后您的动画代码应如下所示:

UIView.animate(withDuration: 0.3) {
    self.view.setNeedsUpdateConstraints()
    self.view.layoutIfNeeded()
})