title: Launch Image Splash Screen Animation in SWIFT
body: <p>I want to create a twitter like launch image for my ios app in swift.</p>
  
  <p>I do understand the general problem with animated launch images, and I basically know the steps to create such an animation:</p>
  
  <ol>
  <li>Add a new view, right after the app launched, with the exact same content as the launchimage</li>
  <li>Animate that view</li>
  <li>Let the view disappear </li>
  </ol>
  
  <p>I found this brilliant source: <a href="http://iosdevtips.co/post/88481653818/twitter-ios-app-bird-zoom-animation" rel="nofollow">http://iosdevtips.co/post/88481653818/twitter-ios-app-bird-zoom-animation</a></p>
  
  <p>But this guy uses a mask and I am not able to change his code to get the "real" twitter like animation without a mask, but just an animated (zoom in) image.</p>
  
  <p>So how do I add a new view to the current view? If I use a subview, how do I identify that view later on the progress when I need to delete the subview in animationDidStop?</p>
  
  <p>Oh and I'd love to do all that in the AppDelegate.</p>
  
  <p>Here is my current approach:</p>
  
  <p>Setting up the view in AppDelegate:</p>
  
  <pre><code>import UIKit
  import QuartzCore
  
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
  
      var viewForLayer: UIView!
      var window: UIWindow?
  
      var emoji: CALayer {
        return viewForLayer.layer
      }
  
      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
  
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // step 1: recreate the launch image
  
        self.emoji.backgroundColor = UIColor(red: 52/255, green: 52/255, blue: 52/255, alpha: 0).CGColor
        self.emoji.contents = UIImage(named: "SplashScreenEmoji")!.CGImage
  
        self.emoji.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        self.emoji.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        self.emoji.position = CGPoint(x: self.window!.frame.size.width/2, y: self.window!.frame.size.height/2)
  
        // self.window!.addSubview(viewForLayer) or
        viewForLayer.layer.addSublayer(emoji)
  
        // step 2: add the animation to that view
        animateEmoji()
  
        self.window!.makeKeyAndVisible()
        UIApplication.sharedApplication().statusBarHidden = true
        return true
      }
  }
  </code></pre>
  
  <p>For the animation:</p>
  
  <pre><code>func animateEmoji() {
      let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
      keyFrameAnimation.delegate = self
      keyFrameAnimation.duration = 1
      keyFrameAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
      let initalBounds = NSValue(CGRect: emoji.bounds)
      let secondBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 90, height: 90))
      let finalBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 1500, height: 1500))
      keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
      keyFrameAnimation.keyTimes = [0, 0.3, 1]
      keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
      self.emoji.addAnimation(keyFrameAnimation, forKey: "bounds")
  }
  
  override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
      // step 3: remove the view
      self.viewForLayer.removeFromSuperview()
  }
  </code></pre>
  
  <p>I always get an fatal error: unexpectedly found nil while unwrapping an Optional value, i guess this is because of the</p>
  
  <pre><code>var emoji: CALayer {
     return viewForLayer.layer
  }
  </code></pre>
  
  <p>I definitely stuck and need the help of the stackoverflow community, please don't judge since I am new here and just learn how to code swift ;)</p>
  
  <p>Thanks,
  jo</p>