Reputation: 21
My goal is when I click on button to toggle contentMode: scaleAspectFit & scaleAspectFill with animation.
@IBAction func toggleContentModeAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
UIView.animate(withDuration: 0.3) {
self.popImageView.contentMode = .scaleAspectFit
self.view.layoutIfNeeded()
}
}else {
UIView.animate(withDuration: 0.3) {
self.popImageView.contentMode = .scaleAspectFill
self.view.layoutIfNeeded()
}
}
}
but no effective to my goal. Please fix for me, thanks from Cambodia.
Upvotes: 1
Views: 862
Reputation: 437927
The contentMode
, itself, is not an animatable property. But you can animated between two different renditions of a view with transition(with:duration:options:animations:completion:)
, e.g.
@IBAction func didTapButton(_ sender: UIButton) {
let contentMode: UIViewContentMode = imageView.contentMode == .scaleAspectFill ? .scaleAspectFit : .scaleAspectFill
UIView.transition(with: imageView, duration: 1, options: .transitionCrossDissolve, animations: {
self.imageView.contentMode = contentMode
}, completion: nil)
}
That yields:
Upvotes: 4