Home > Back-end >  How to Change UIImageView ContentMode With Animation using swift?
How to Change UIImageView ContentMode With Animation using swift?

Time:02-27

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
 }

CodePudding user response:

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).

CodePudding user response:

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)
    }
    
  • Related