title: How to prevent an animation from completing when user leaves and returns to app
body: <p>I am making an app where I have a <code>button</code> who is moving from one side of the screen to the other. I have created a pause <code>button</code> which pauses the animation once that <code>button</code> is selected. This <code>button</code> and function works fine. I also added a function which determines if the user has exited the application. In this function, I added the pause <code>button</code> function. However, when the user returns to the app after exiting, it shows the pause screen, but the animation is completed. Here is the code: </p>
  
  <pre><code>@IBAction func pauseButton(sender: UIButton) {
      let button = self.view.viewWithTag(1) as? UIButton!
      let layer = button!.layer
      pauseLayer(layer) // Pausing the animation
      view2.hidden = false // Showing pause screen
  }
  func pauseLayer(layer: CALayer) {
      let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
      layer.speed = 0.0
      layer.timeOffset = pausedTime
  }
  @IBAction func startButton(sender: UIButton) {
      let button = UIButton()
      button.tag = 1 // so I can retrieve it in the pause function
      // Extra code to make the button look nice
      button.translatesAutoresizingMaskIntoConstraints = false // Allow constraints
      let topAnchorForButton = button.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 400)
      NSLayoutConstraint.activateConstraints([ // Adding constraints
          topAnchorForButton,
          button.leadingAnchor.constraintEqualToAnchor(view.topAnchor, constant: 120),
          button.widthAnchor.constraintEqualToConstant(65),
          button.heightAnchor.constraintEqualToConstant(65)
          ])
      self.view.layoutIfNeeded()
      [UIView.animateWithDuration(5.0, delay: 0.0, options: [.CurveLinear, .AllowUserInteraction], animations: {
          topAnchorForButton.constant = 0
          self.view.layoutIfNeeded()
          }, completion: nil )] // the animation
  
  override func viewDidLoad() {
      super.viewDidLoad()
           NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseButton:", name: UIApplicationWillResignActiveNotification, object: nil) // When exists app
           NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseButton:", name: UIApplicationDidEnterBackgroundNotification, object: nil) // when goes to home screen
  }
  </code></pre>
  
  <p>When I re-enter the app, the animation is completed.</p>
  
  <p>Here's is a code I've tried which only plays and stops the animation. I've also added something in the App Delegate but it still doesn't work:</p>
  
  <p>ViewController:</p>
  
  <pre><code>class Home: UIViewController {
  
  func pauseAnimation(){
  let button = self.view.viewWithTag(1) as? UIButton!
  let layer = button!.layer
  pauseLayer(layer) // Pausing the animation
  }  
  @IBAction func pauseButton(sender: UIButton) {
     pauseAnimation()
  }
  func pauseLayer(layer: CALayer) {
      let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
      layer.speed = 0.0
      layer.timeOffset = pausedTime
  }
  @IBAction func startButton(sender: UIButton) {
      let button = UIButton()
      button.tag = 1 // so I can retrieve it in the pause function
      // Extra code to make the button look nice
      button.setTitle("Moving", forState: .Normal)
      button.setTitleColor(UIColor.redColor(), forState: .Normal)
      view.addSubview(button)
      button.translatesAutoresizingMaskIntoConstraints = false // Allow constraints
      let topAnchorForButton = button.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 400)
      NSLayoutConstraint.activateConstraints([ // Adding constraints
          topAnchorForButton,
          button.leadingAnchor.constraintEqualToAnchor(view.topAnchor, constant: 120),
          button.widthAnchor.constraintEqualToConstant(65),
          button.heightAnchor.constraintEqualToConstant(65)
          ])
      self.view.layoutIfNeeded()
      [UIView.animateWithDuration(10.0, delay: 0.0, options: [.CurveLinear, .AllowUserInteraction], animations: {
          topAnchorForButton.constant = 0
          self.view.layoutIfNeeded()
          }, completion: nil )] // the animation
  }
  </code></pre>
  
  <p>App Delegate:</p>
  
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {
  
  var window: UIWindow?
  var vc = Home()
  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
  }
  func applicationWillResignActive(application: UIApplication) {
     vc.pauseAnimation()
  }
  </code></pre>
  
  <p>Just in case you want to know, here is the code for resuming the animation:</p>
  
  <pre><code>@IBAction func resume(sender: UIButton) {
      let button = self.view.viewWithTag(100) as? UIButton!
      let layer = button!.layer 
      resumeLayer(layer)
  }
  func resumeLayer(layer: CALayer) {
      let pausedTime: CFTimeInterval = layer.timeOffset
      layer.speed = 1.0
      layer.timeOffset = 0.0
      layer.beginTime = 0.0
      let timeSincePause: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
      layer.beginTime = timeSincePause
  }
  </code></pre>
  
  <p>When I exit the app, it says that <code>button</code> or/and <code>layer</code> is nil (fatal error... optional value is nil), so it crashes.</p>
  
  <p>Please help. Thanks in advance... Anton</p>