2016-08-17 146 views
-1

我試圖在屏幕上出現淡入淡出的標籤。目前,我只是隱藏它使用(Swift)淡入淡出標籤

button.hidden = true 

並通過使用false取消隱藏它。我想用淡入淡出的方式來製作這個過程,因爲這樣看起來更平滑。感謝幫助!

以下是我使用的代碼出錯。 http://imgur.com/a/HI4eg

class ViewController: UIViewController { 
@IBOutlet weak var yeah: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: { 
     self.yeah.alpha = 0.0 
     }, completion: { 
      (finished: Bool) -> Void in 

      //Once the label is completely invisible, set the text and fade it back in 
      self.yeah.text = "your Text " 

      // Fade in 
      UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: { 
       self.yeah.alpha = 1.0 
       }, completion: { 
        (finished: Bool) -> Void in 

        //Once the label is completely invisible, set the text and fade it back in 
        self.yeah.text = "your Text " 

        // Fade in 
        UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: { 
         self.yeah.alpha = 1.0 
         }, completion:nil) 




      }) 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

} }

+0

您是否正在尋找與淡出一躲,並與褪色顯示有效?由於默認隱藏不具動畫性? – Shripada

回答

5

正如andrew bancroft's blog

 // Move our fade out code from earlier 
    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: { 
     self.yourLabel.alpha = 0.0 
     }, completion: { 
      finished in 

      if finished { 
       //Once the label is completely invisible, set the text and fade it back in 
       self.yourLabel.text = "your Text " 

       // Fade in 
       UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: { 
        self.yourLabel.alpha = 1.0 
       }, completion: nil) 
      } 
    }) 

修正的應答解釋

UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: { 
     self.yourLabel.alpha = 0.0 
     }, completion: { 
      finished in 

      if finished { 
       //Once the label is completely invisible, set the text and fade it back in 
       self.yourLabel.text = "your Text " 

       // Fade in 
       UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: { 
        self.yourLabel.alpha = 1.0 
        }, completion: { 
         finished in 

         if finished { 
          //Once the label is completely invisible, set the text and fade it back in 
           self.yourLabel.text = "your Text " 

          // Fade in 
          UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: { 
           self.yourLabel.alpha = 0.0 
           }, completion: nil) 
         } 
       }) 
      } 
    }) 
+1

你應該添加'if finished {}'到你的關閉。完成塊_might_在未完成時被調用。 –

+0

如果您推薦某人的代碼,請先將其作爲參考。 –

+0

@DipenPanchasara - 確定我的兄弟,你能修改答案我按照你的方式 –