Search code examples
swiftimageanimationsizescale

Resize image to its initial size


I am using 2 animations. when the screen is launched, the first animation starts in viewDidLoad.

this animation is applied to 2 images called layer2 and layer3.

func firstAnimation(){
        UIView.animate(withDuration: 1, delay: 0, options:
            UIViewAnimationOptions.repeat , animations: {

                self.layer2.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
                self.layer3.transform = CGAffineTransform(scaleX: 2, y: 2)
        }, completion: { finished in

        })
    }

when an image called layer0 is long pressed, then firstAnimation() is being stopped by this code:

layer2.layer.removeAllAnimations()
layer3.layer.removeAllAnimations()

and different animation is being applied to layer2 and layer3.

the second animation is

func secondAnimation() {
        UIView.animate(withDuration: 20, delay: 0, options:
            UIViewAnimationOptions.curveEaseOut , animations: {

                self.layer2.transform = CGAffineTransform(scaleX: 10, y: 10)
                self.layer3.transform = CGAffineTransform(scaleX: 10, y: 10)

        }, completion: { finished in

        })
    }

when I want to delete the second animation and launch the first one again, the 2 images layer2 and layer3 are starting from the size that they gained because of the second animation. how to relaunch the first animation with the initial sizes of the images?


Solution

  • You can reset views that were transformed to their original scale by using CGAffineTransform.identity.

    For example:

    layer2.transform = .identity