Restoring animation where it left off when app resumes from background

后端 未结 10 1309
广开言路
广开言路 2020-11-28 18:57

I have an endlessly looping CABasicAnimation of a repeating image tile in my view:

a = [CABasicAnimation animationWithKeyPath:@\"position\"];
a         


        
10条回答
  •  爱一瞬间的悲伤
    2020-11-28 19:29

    I was able to restore the animation (but not the animation position) by saving a copy of the current animation and adding it back on resume. I called startAnimation on load and when entering the foreground and pause when entering the background.

    - (void) startAnimation {
        // On first call, setup our ivar
        if (!self.myAnimation) {
            self.myAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
            /*
             Finish setting up myAnimation
             */
        }
    
        // Add the animation to the layer if it hasn't been or got removed
        if (![self.layer animationForKey:@"myAnimation"]) {
            [self.layer addAnimation:self.spinAnimation forKey:@"myAnimation"];
        }
    }
    
    - (void) pauseAnimation {
        // Save the current state of the animation
        // when we call startAnimation again, this saved animation will be added/restored
        self.myAnimation = [[self.layer animationForKey:@"myAnimation"] copy];
    }
    

提交回复
热议问题