CABasicAnimation似乎表演了两部动画

时间:2015-01-16 09:46:58

标签: swift xcode6 cabasicanimation

小规模动画按预期工作,但缩放动画恢复到正常尺寸会产生奇怪的效果;它将动画制作到正常比例但是然后它再次执行相同的动画,但速度要快得多。

这是我的代码:

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

                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:4)

发现问题:

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")
    }