Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zooming out animation Swift

Tags:

ios

swift

I'm trying to do some animation for a UIImageView (only zooming out)

1- I set the frame to be bigger than the screen to make it zoomed in like so :

private lazy var imageView: UIImageView = {
    let image = UIImageView()
    image.frame = CGRect(x: 0, y: 0, width: view.frame.width + 300, height: view.frame.height + 300)
    image.center = view.center
    image.image = UIImage(named: "1111")
    image.contentMode = .scaleAspectFill
    return image
}()

and I want to make the frame go back to the screen size like so :

func firstAnimation() {
    UIView.animate(withDuration: 15, animations: {

        // HERE 
        self.imageView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    }) { (finished) in
        self.secondAnimation()
    }
}

I didn't work with me unfortunately.

Here the full class code : github

like image 648
cs4alhaider Avatar asked Dec 14 '22 14:12

cs4alhaider


2 Answers

Swift 4+

Use CGAffineTransform.identity to resize your image to it's first identity

UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {
        
       // HERE
       self.imageview.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2) // Scale your image

 }) { (finished) in
     UIView.animate(withDuration: 1, animations: {
       
      self.imageview.transform = CGAffineTransform.identity // undo in 1 seconds

   })
}
like image 75
excitedmicrobe Avatar answered Jan 19 '23 05:01

excitedmicrobe


Code for zoom in:

[UIView animateWithDuration:1.0 animations:^{
    imgv.transform = CGAffineTransformMakeScale(1.0,1.0);
}completion:^(BOOL finished){
}];

Code for zoom out:

[UIView animateWithDuration:1.0 animations:^{
    imgv.transform = CGAffineTransformMakeScale(0.1,0.1);           
}completion:^(BOOL finished){
    imgv.transform = CGAffineTransformMakeScale(.0,.0);    
}];

Here imvv is my UIImageview object. You can replace it with your view object. In zoom out code we hide the view by scaling it to 0.

like image 43
Jamil Hasnine Tamim Avatar answered Jan 19 '23 03:01

Jamil Hasnine Tamim