我的目标是当我点击按钮时切换 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()
}
}
}
但对我的目标没有效果。请帮我修好,来自柬埔寨的感谢。
最佳答案
contentMode
本身不是一个动画属性。但是您可以使用 transition(with:duration:options:animations:completion:)
在 View 的两个不同再现之间设置动画。 ,例如
@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)
}
产生:
关于ios - Swift 3/4 如何为 UIImageView 的 contentMode 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701140/