UILabel没有像预期的那样出现延迟序列

时间:2015-11-13 11:58:26

标签: ios swift uiview core-animation

我想让我的UILabel以延迟的顺序出现。因此一个接一个。 当我使用alpha值使它们淡入时,下面的代码可以工作,但是当我使用UILabels的.hidden属性时,它并没有做我想要它做的事情。

代码使我的UILabel同时出现,而不是在5秒后首先出现sum1TimeLabel,在30秒后出现sum2TimeLabel秒,最后在60秒后出现sum3TimeLabel。我究竟做错了什么?

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    UIView.animateWithDuration(5.0, animations:  {
        self.sum1TimeLabel!.hidden = false;
    })

    UIView.animateWithDuration(30.0, animations: {
        self.sum2TimeLabel!.hidden = false;
    })

    UIView.animateWithDuration(60.0, animations: {
        self.sum3TimeLabel!.hidden = false;
    })
}

3 个答案:

答案 0 :(得分:5)

正如[{3}}答案中的SO用户@matt所解释的那样,您可以使用Grand Central Dispatch创建delay函数,而不是使用animateWithDuration,{{1}通常用于在一段时间内制作动画,并且由于您动画的值为animateWithDuration,因此无法为其设置动画。

Bool

答案 1 :(得分:0)

// Try this

override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(animated)
   self.sum1TimeLabel!.hidden = true;
   self.sum2TimeLabel!.hidden = true;
   self.sum3TimeLabel!.hidden = true;

   UIView.animateWithDuration(5.0, animations: {

   }, completion: {
       self.sum1TimeLabel!.hidden = false;
   })

   UIView.animateWithDuration(30.0, animations: {

   }, completion: {
       self.sum2TimeLabel!.hidden = false;
   })

   UIView.animateWithDuration(60.0, animations: {

   }, completion: {
       self.sum3TimeLabel!.hidden = false;
   })
}

答案 2 :(得分:0)

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        label1.alpha = 0
        label2.alpha = 0
        label3.alpha = 0
        UIView.animateWithDuration(5.0, animations: { () -> Void in
            print("animation on label1")
            self.label1.alpha = 0.2
            }) { (b) -> Void in
                print("complete on label1")
                self.label1.alpha = 1
                UIView.animateWithDuration(5.0, animations: { () -> Void in
                    print("animation on label2")
                    self.label2.alpha = 0.2
                    }, completion: { (b) -> Void in
                        print("complete on label2")
                        self.label2.alpha = 1
                        UIView.animateWithDuration(5.0, animations: { () -> Void in
                            print("animation on label3")
                            self.label3.alpha = 0.2
                            }, completion: { (b) -> Void in
                                print("complete on label3")
                                self.label3.alpha = 1
                        })
                })
        }
        // 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.
    }

}

您无法为隐藏属性设置动画。只有两个州。如何随着时间的推移不断改变隐藏?相反,你可以动画阿尔法:-)。尝试将动画块内的alpha值更改为0或1。