iOS动态启动界面(swift实现)

最近使用swift语言制作的应用使用了动态启动界面,先来看看效果吧:
iOS动态启动界面(swift实现)_第1张图片

首先,启动图只可以设置一张静态图片,而动态效果是在 “didFinishLaunchingWithOptions” 中执行的动画。
首先我试用了LaunchScreen,并再其中设置了静态的启动图片

iOS动态启动界面(swift实现)_第2张图片

之后倒入了3张需要执行动画的图片素材

iOS动态启动界面(swift实现)_第3张图片

下一步就要在AppDelegate中 didFinishLaunchingWithOptions:方法里执行动画效果了:

    var blueView: UIImageView!
    var redView: UIImageView!
    var purpleView: UIImageView!

    let animationDuration = 0.8

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        self.window?.backgroundColor = UIColor.blackColor()

        //下面这句一定得加上,否则不会有任何动画
        self.window?.makeKeyAndVisible() 

        let originalFrame = CGRectMake(-kWidth/2, 0, kWidth*2, kHeight)

        redView = UIImageView(frame: originalFrame)
        redView.contentMode = .ScaleAspectFill
        redView.image = UIImage(named: "redPart")
        self.window?.addSubview(redView)

        purpleView = UIImageView(frame: originalFrame)
        purpleView.contentMode = .ScaleAspectFill
        purpleView.image = UIImage(named: "purplePart")
        self.window?.addSubview(purpleView)


        blueView = UIImageView(frame: originalFrame)
        blueView.contentMode = .ScaleAspectFill
        blueView.image = UIImage(named: "bluePart")
        self.window?.addSubview(blueView)

                UIView.animateWithDuration(animationDuration, animations: { () -> Void in
            self.redView.frame = CGRectMake(0, 0, kWidth*2, kHeight)
            self.redView.alpha = 0
            self.purpleView.frame = CGRectMake(-kWidth, 0, kWidth*2, kHeight)
            self.purpleView.alpha = 0
            self.blueView.frame = CGRectMake(-kWidth/2, kHeight, kWidth*2, kHeight)
            self.blueView.alpha = 0

            self.window?.backgroundColor = UIColor.whiteColor()

            }) { (bool) -> Void in
            self.redView.removeFromSuperview()
            self.purpleView.removeFromSuperview()
            self.blueView.removeFromSuperview()
        }

虽然苹果不建议使用复杂的启动界面,但很多应用的动态效果几乎都是这种思路。

你可能感兴趣的:(IOS开发,Swift)