CABasicAnimation seemingly performing two animations

10,762

Found the problem:

func animateSolidAway(){

        let scaleAnimate:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
        scaleAnimate.fromValue = 1
        scaleAnimate.toValue = 0
        scaleAnimate.duration = 0
        scaleAnimate.delegate = self
        scaleAnimate.removedOnCompletion = false
        scaleAnimate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
        insideSolidLayer.addAnimation(scaleAnimate, forKey: "scaleSmallAnimation")


    }

    func animateSolidIn(){
        insideSolidLayer.transform = CATransform3DMakeScale(1, 1, 1)
        let scaleAnimate:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
        scaleAnimate.fromValue = 0
        scaleAnimate.toValue = 1
        scaleAnimate.duration = 0
        scaleAnimate.delegate = self
        scaleAnimate.removedOnCompletion = false
        scaleAnimate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        insideSolidLayer.addAnimation(scaleAnimate, forKey: "scaleNormalAnimation")
    }
Share:
10,762
Petri
Author by

Petri

Updated on June 04, 2022

Comments

  • Petri
    Petri almost 2 years

    The small scale animation works as expected but the scale animation to return to the normal size does something weird; it does the animation to the normal scale right but then it does the same animation again but a lot faster.

    Here is my code:

    func changeState(sender:UIButton){
            println(insideSolidLayer.animationKeys())
            if (shouldAnimate){
                shouldAnimate = false
                if (on){
                    animateSolidAway()
                }else{
                    animateSolidIn()
                }
                on = !on
            }
        }
    
        func animateSolidAway(){
            insideSolidLayer.transform = CATransform3DMakeScale(1, 1, 1)
            let scaleAnimate:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
            scaleAnimate.fromValue = 1
            scaleAnimate.toValue = 0
            scaleAnimate.duration = 0.2;
            scaleAnimate.delegate = self
            scaleAnimate.removedOnCompletion = false
            scaleAnimate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
            insideSolidLayer.addAnimation(scaleAnimate, forKey: "scaleSmallAnimation")
    
        }
    
        func animateSolidIn(){
            insideSolidLayer.transform = CATransform3DMakeScale(0, 0, 0)
            let scaleAnimate:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
            scaleAnimate.fromValue = 0
            scaleAnimate.toValue = 1
            scaleAnimate.duration = 3;
            scaleAnimate.delegate = self
            scaleAnimate.removedOnCompletion = false
            scaleAnimate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
            insideSolidLayer.addAnimation(scaleAnimate, forKey: "scaleNormalAnimation")
        }
    
        override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
            if(anim.isKindOfClass(CABasicAnimation)){
                if (insideSolidLayer != nil && insideSolidLayer.animationKeys() != nil && insideSolidLayer.animationKeys().count > 0){
                    if contains(insideSolidLayer.animationKeys() as [String], "scaleSmallAnimation"){
                        if (anim == insideSolidLayer.animationForKey("scaleSmallAnimation")){
                            println("-animationDidStop- -scaleSmallAnimation-")
                            insideSolidLayer.transform = CATransform3DMakeScale(0, 0, 0)
                            shouldAnimate = true
                        }
                    }
    
                    if contains(insideSolidLayer.animationKeys() as [String], "scaleNormalAnimation"){
                        if (anim == insideSolidLayer.animationForKey("scaleNormalAnimation")){
                            println("-animationDidStop- -scaleNormalAnimation-")
                            insideSolidLayer.transform = CATransform3DMakeScale(1, 1, 1)
                            shouldAnimate = true
    
                        }
                    }
                }
            }
        }