4

I am animating a view and I want to pause it and resume it.

Using an apple guide I created a CALayer Extension

extension CALayer {

    func pause() {
        var pauseTime = self.convertTime(CACurrentMediaTime(), fromLayer: nil)
        self.speed = 0.0
        self.timeOffset = pauseTime
    }

    func resume() {
        var pausedTime = self.timeOffset
        self.speed = 1.0
        self.timeOffset = 0.0
        self.beginTime = 0.0
        var timeSincePause = self.convertTime(CACurrentMediaTime(), toLayer: nil) - pausedTime

        self.beginTime = timeSincePause
    }
}

This code is working perfectly except when that app goes to background. When I bring the App back to foreground animations is finished (even if the time is not pass) and it is not starting again when I click resume.

Ok. I tried animating CALayer but I have the same problem.

extension CALayer {

   func animateY(newY:CGFloat,time:NSTimeInterval,completion:()->Void){
    CATransaction.begin()
    CATransaction.setCompletionBlock(completion)
    let animation = CABasicAnimation(keyPath: "position.y")
    animation.fromValue = self.position.y
    animation.toValue  = newY
    animation.duration = time
    animation.delegate = self
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
    animation.removedOnCompletion = false // don't remove after finishing
    self.position.y = newY
    self.addAnimation(animation, forKey: "position.y")
    CATransaction.flush()

  }
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Have you tried setting `removedOnCompletion=NO;` for you animation. That should stop it being removed. – Rory McKinnel Jun 02 '15 at 13:42
  • You can also add an observer for the notification event `UIApplicationDidBecomeActiveNotification` inside which you can rejig your animation. You can treat this like `viewWillAppear` as it is not called on resume from background. – Rory McKinnel Jun 02 '15 at 13:55
  • there's a ton of duplicate questions on this, including the answer to "Restoring animation where it left off when app resumes from background". See http://stackoverflow.com/questions/7568567/restoring-animation-where-it-left-off-when-app-resumes-from-background/7983292#7983292 – Max MacLeod Jun 03 '15 at 08:25
  • You haven`t noticed that i tried the method in the answer and is not working for me. :) – Christos Chadjikyriacou Jun 03 '15 at 08:35

1 Answers1

0

I recommend using CABasicAnimation. Your resume/pause methods should be fine, since they are from this answer. You should try using Core Animation instead of UIViewAnimation and then the resume/pause will work.

Then you can register for the two notifications UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification to have full control over the pause/resume actions.

Community
  • 1
  • 1
Teo
  • 3,394
  • 11
  • 43
  • 73
  • That is the same code as the question code, just not implemented as a CALayer extension. – Rory McKinnel Jun 02 '15 at 13:51
  • Yeah, but he is using UIViewAnimation instead of Core Animation. That is why I think his code has no effect, because it should be used with a layer animation. – Teo Jun 02 '15 at 13:57