UIView 动画的宽度约束变化

Width constraint change with UIView animation

我有通过 SnapKit 更新的宽度限制。展开动画:

self.snp.updateConstraints({(make) in
    make.width.equalTo(150.0)
})

折叠动画:

self.snp.updateConstraints({(make) in
    make.width.equalTo(150.0)
})

当我通过以下方式制作动画时:

UIView.animate(withDuration: 0.5,animations: {
    self.layoutIfNeeded()
}, completion: nil)

我的视图通过先向左跳跃,然后从左向右扩展来改变宽度,因为我的“动画”视图尾随锚点等于超级视图尾随锚点。

slider.snp.makeConstraints({(make) in
    make.trailing.equalToSuperview()
    make.centerY.equalToSuperview()
    make.height.equalTo(slider.getContentHeight())
    make.width.equalTo(slider.labels.first?.intrinsicContentSize.width ?? 30.0)
})

所以我希望动画从右向左展开

我通过更改 view.center.x 获得了预期的结果。我试图更改前导约束和宽度,但没有成功。我也碰巧改变了我的视图宽度,但它没有动画, before/after 分别为 expand/collapse。所以这是我的代码:

self.snp.updateConstraints({(make) in
    make.width.equalTo(150.0)
})

UIView.animate(withDuration: 0.2,
    animations: {
        self.center.x -= 90
        self.layoutIfNeeded()
    }, completion: nil)

反之:

UIView.animate(withDuration: 0.2,
    animations: {
        self.center.x += 90
        self.layoutIfNeeded()
    }, completion: nil)

self.snp.updateConstraints({(make) in
    make.width.equalTo(30.0)
})

动画在折叠时有点粗糙,但在展开时效果很好。仍然感谢任何建议。