一个简单的动画,如果该应用程序是第一次启动?

时间:2014-06-12 02:23:04

标签: ios swift

在swift中,我有一个应用程序,在第一次启动时会显示一个"教程。"我想要一个简单的动画,其中第一个动画进入,第二个进入后。我希望它快速非常。虽然,使用我当前的代码,当视图/应用程序加载时,它们都显示正确。

代码:

let welcomeLabel = UILabel()
let learnToPlayButton = UIButton()
welcomeLabel.alpha = 0
learnToPlayButton.alpha = 0
welcomeLabel.text = "Welcome to ReactFast!"
        welcomeLabel.font = UIFont(name: "HelveticaNeue-Light", size: 35)
        welcomeLabel.textAlignment = .Center
        welcomeLabel.textColor = UIColor.whiteColor()
        welcomeLabel.frame = CGRectMake(85, 30, 400, 200)
   learnToPlayButton.setTitle("Click here to learn how to play.", forState: .Normal)
        learnToPlayButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        learnToPlayButton.titleLabel.font = UIFont(name: "HelveticaNeue-UltraLight", size: 35)
        learnToPlayButton.titleLabel.textAlignment = .Center
        learnToPlayButton.addTarget(self, action: "learnToPlay:", forControlEvents: .TouchUpInside)
        learnToPlayButton.frame = CGRectMake(37, 70, 500, 200)
if NSUserDefaults.standardUserDefaults().boolForKey("hasLaunchedOnce") {
            //app has already launched before
        } else {
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "hasLaunchedOnce")
            NSUserDefaults.standardUserDefaults().synchronize()
            //first time launch

            UIView.animateWithDuration(1.5, animations: {
                self.view.addSubview(self.welcomeLabel)
                self.view.addSubview(self.learnToPlayButton)
                self.welcomeLabel.alpha = 1.0
                self.learnToPlayButton.alpha = 1.0
                })
        }

1 个答案:

答案 0 :(得分:2)

动画代码的放置可能导致缺少动画。 viewDidLoad在视图控制器的生命周期中为时过早,你试图在那里制作动画。

您应该做的是将所有设置代码保留在viewDidLoad中,然后在viewDidAppear:方法中添加动画代码,以便在视图实际显示在scree上时动画开始。