ios - 宽度约束随 UIView 动画变化
问题描述
我有通过 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
。我试图改变前导约束和宽度,但没有成功。我也碰巧改变了我的视图的宽度,但它在没有动画的情况下发生,并且分别在展开/折叠之前/之后发生。所以这是我的代码:
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)
})
动画在折叠时有点粗糙,但在展开时效果很好。仍然感谢任何建议。