简体   繁体   中英

How to Change UIImageView ContentMode With Animation using swift?

I will try with this code, but not work

let contentMode: UIView.ContentMode = imageView.contentMode == .scaleAspectFill ?
.scaleAspectFit : .scaleAspectFill
 
 UIView.animate(withDuration: 0.5) {
     self.imageView.contentMode = contentMode
 }

You can't do it. This is not an animatable property. It is unclear what kind of animation would even make sense.

An alternative would be to substitute a second image view with a different content mode, and cross-dissolve from one image view to the other (transformation).

Why don't you just animate UIImageView itself? The code below is an example for growing animation.

    let height = imageView.frame.height
    let width = imageView.frame.width
    
    let x = imageView.frame.origin.x
    let y = imageView.frame.origin.y
            
    imageView.frame = CGRect(x: x, y: y, width: 0, height: 0)
    
    UIView.animate(withDuration: 0.5) {
        self.imageView.frame = CGRect(x: x, y: y, width: width, height: height)
    }
    

I'm done this with help of transition

let contentMode: UIView.ContentMode = imageView.contentMode == .scaleAspectFill ? .scaleAspectFit : .scaleAspectFill
        UIView.transition(with: imageView, duration: 0.5, options: .transitionCrossDissolve, animations: {
                self.imageView.contentMode = contentMode
            })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM