问题
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.
回答1:
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:
来源:https://stackoverflow.com/questions/46701140/swift-3-4-how-to-animate-contentmode-of-uiimageview