2015-01-16 80 views
1

小規模動畫按預期工作,但縮放動畫恢復到正常大小確實有點奇怪;它將動畫做到正常範圍,但是它再次執行相同的動畫,但速度更快。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 

        } 
       } 
      } 
     } 
    } 

回答

3

發現了問題:

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