Swift 3/4 How to animate contentMode of UIImageView

南笙酒味 提交于 2019-12-13 01:28:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!