I have width constraint which I update through SnapKit. Expanding animation:
self.snp.updateConstraints({(make) in
make.width.equalTo(150.0)
})
Collapsing animation:
self.snp.updateConstraints({(make) in
make.width.equalTo(150.0)
})
When I animate this through:
UIView.animate(withDuration: 0.5,animations: {
self.layoutIfNeeded()
}, completion: nil)
My view change width by expanding first jumping to left, and then expanding from left to right because my "animated" view trailing anchor equal to superview trailing anchor.
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)
})
So i would like to animation be expanding from right to left
I achieved desired result by changing view.center.x
. I tried to change leading constraint and width but it didn't work out. I also happen to change width of my view but it happens without animation and before/after for expand/collapse respectively. So here is my code:
self.snp.updateConstraints({(make) in
make.width.equalTo(150.0)
})
UIView.animate(withDuration: 0.2,
animations: {
self.center.x -= 90
self.layoutIfNeeded()
}, completion: nil)
And reverse :
UIView.animate(withDuration: 0.2,
animations: {
self.center.x += 90
self.layoutIfNeeded()
}, completion: nil)
self.snp.updateConstraints({(make) in
make.width.equalTo(30.0)
})
Animation is a bit rough when it comes to collapsing but works just fine for expanding. Still appreciate any suggestion.