2016-08-09 255 views
0

我試圖實現這個動畫,其中UIButton在用戶保存文件時淡出。當按鈕淡出時,複選標記圖像的UIImageView淡出而不是按鈕。一旦ImageView的充分漸強,那麼它淡出和按鈕消失回來。連續淡入淡出動畫

UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
     self.recButton.alpha = 0.0 
     }, completion: { 
      (value: Bool) in 
      UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { 
       self.checkView.alpha = 1.0 
       }, completion: { 
        (value: Bool) in 
        UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
         self.checkView.alpha = 0.0 
         }, completion: { 
          (value: Bool) in 
          UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { 
           self.recButton.alpha = 1.0 
           self.recButton.enabled = true 
           }, completion: nil) 
        }) 
      }) 
    }) 

雖然上面的方法做工作,它不是一帆風順的,我想。有沒有更好的方法來解決這個問題?

+0

你可以使用一個關鍵幀動畫[如圖所示](http://commandshift.co.uk/blog/2014/04/01/stop-nesting-animation-blocks/) – Palle

+0

@Palle這看起來像清潔工的方法,但我沒有看到如何使用'UIViewKeyframeAnimationOptions'選項來製作出色的淡入/淡出動畫效果。 – Brosef

回答

0

你需要知道你想要的動畫四個關鍵幀:按鈕淡出,圖像淡入,圖像淡出,在按鍵褪色

let button: UIButton 
let checkmarkImage: UIImageView 

button.alpha = 1.0 
checkmarkImage = 0.0 

UIView.animateKeyframesWithDuration(2.4, delay: 0, options: .CalculationModeLinear, animations: { 
    UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.25, animations: { 
    button.alpha = 0.0 
    }) 
    UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.25, animations: { 
    checkmarkImage.alpha = 1.0 
    }) 
    UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.25, animations: { 
    checkmarkImage.alpha = 0.0 
    }) 
    UIView.addKeyframeWithRelativeStartTime(0.75, relativeDuration: 0.25, animations: { 
    button.alpha = 1.0 
    }) 
}, completion: nil) 

這個問題引起了我的好奇心 - 此關鍵的事情是很酷。