ios - Swift 3/4 如何为 UIImageView 的 contentMode 设置动画

标签 ios swift uiimageview

我的目标是当我点击按钮时切换 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)
}

产生:

enter image description here

关于ios - Swift 3/4 如何为 UIImageView 的 contentMode 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701140/

相关文章:

objective-c - 如何制作枚举案例的开关案例(快速)

swift - 重新排列核心数据 TableView 单元格(快速)

ios - 使用 PHAsset 检索多个图像

ios - 如何从其自己的 subview Controller 中解散模态 uitabbarcontroller

ios - Storyboard界面未立即更新

iOS 在 Swift3 中添加多个可选值

ios - 当不再显示 UIViewController 时如何继续 CAKeyframeAnimation?

objective-c - 通过OperationQueue加载UIImageView的问题

ios - 旋转动画直到 UIViewController 消失

ios - 在 swift 错误 : Use of unresolved identifier 'ABXXXView' 中创建我自己的 cocoapods 库