Search code examples
iosswiftcgrect

CGRect scale up animation not working in Xcode 10?


I am following a tutorial (that is using ios 8, but I am using ios 12) that teaches me to animate by scaling up an image using CGRect. I followed exactly to use the override functions viewDidLayoutSubviews and viewDidAppear, but my simulator is not showing the image increasing in size when I open the app.

    override func viewDidLayoutSubviews() {

        pandaImage.frame = CGRect(x: 100, y: 20, width: 0, height: 0)

    }

    override func viewDidAppear(_ animated: Bool) {

        UIView.animate(withDuration: 1, animations: { () -> Void in

            self.pandaImage.frame = CGRect(x: 5, y: 20, width: 400, height: 500)

        })

    }

I want the image to first be very tiny hence scaled down, so the width and the height should be 0. Then when I open the app, the image should resize itself to having 400 in width and 500 in height. The actual results are that the image itself is not showing, and so I am getting a blank interface on the simulator.


Solution

  • You can use CGAffineTransform for scale effect:

    Grow/decrease animation:

    UIView.animate(withDuration: 1, animations: {
        self.pandaImage?.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
    }) { (_ finished: Bool) in
        UIView.animate(withDuration: 1, animations: {
            self. pandaImage?.transform = CGAffineTransform(scaleX: 1, y: 1)
        })
    }