使用延迟动画暂停CALayer动画

Sea*_*ean 8 core-animation objective-c uiview ios

我有一组嵌套UIView动画(在给定时间深2或3级),我希望能够暂停和恢复.其中一些动画使用-animateWithDuration:animations:completion:而其他动画使用-animateWithDuration:delay:options:animations:completion:以延迟动画块的执行.

我阅读并实现了关于暂停层树中所有动画的技术问答QA1673,但我遇到了使用延迟参数的动画的问题.我可以暂停和恢复动画,但是当动画恢复时,任何与其相关的延迟的动画块似乎都会延迟图层树暂停的时间.因此,例如,如果其中一个块的延迟为1秒,并且图层树暂停了3秒,则动画在恢复后会延迟4秒.我猜这与beginTime房产有关?任何帮助,将不胜感激.

// Pause and Resume methods, right from the technical Q&A
- (void)pauseAnimationsOnLayer:(CALayer *)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

- (void)resumeAnimationsOnLayer:(CALayer *)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

// Chained animations
- (void)animateNextPopup
{
    [UIView animateWithDuration:kRFPVictorySequenceStatePopupDuration
                     animations:^{
                         [_currentStateImageView setHidden:NO];
                         [_currentStateImageView setTransform:CGAffineTransformIdentity];

                     }
                     completion:^(BOOL finished) {
                         [UIView animateWithDuration:kRFPVictorySequenceStateSlideOffDuration
                                               delay:kRFPVictorySequenceStateVoteDelay
                                             options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              if (winnerIsDem) {
                                                  [_currentStateImageView setFrame:CGRectMake(-_currentStateImageView.frame.size.width, 
                                                                                              _currentStateImageView.frame.origin.y, 
                                                                                              _currentStateImageView.frame.size.width, 
                                                                                              _currentStateImageView.frame.size.height)];
                                              }
                                              else {
                                                  [_currentStateImageView setFrame:CGRectMake(1024, 
                                                                                              _currentStateImageView.frame.origin.y, 
                                                                                              _currentStateImageView.frame.size.width, 
                                                                                              _currentStateImageView.frame.size.height)];
                                              }
                                          }
                                          completion:^(BOOL finished) {
                                              // Do some stuff
                                          }
                          ];
                     }
     ];
}
Run Code Online (Sandbox Code Playgroud)

Bep*_*ppe -1

我建议采用不同的方法。

动画块很容易实现,但仅当您不需要对动画进行任何控制时才有用。

否则,您应该使用计时器并手动创建自己的动画。

[NSTimer scheduledTimerWithTimeInterval:0.1
                                 target:self
                               selector:@selector(timerFired)
                               userInfo:nil
                                repeats:YES];

- (void)timerFired
{
    if (isPaused) {
        // Do nothing
    } else {
        // Animate
    }
}

- (IBAction)pauseTapped:(id)sender
{
    if (isPaused) {
        isPaused = NO;
    } else {
        isPaused = YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

isPaused是控制动画状态的标志。

  • 感谢您的回复,但这个想法实际上也有其自身的问题。使用标准的“NSTimer”不会将动画与设备的显示速率同步,这可能会导致动画看起来不稳定。更好的方法是使用“CADisplayLink”,它与显示器类似,但其设计目的正是为了与显示器同步。尽管如此,使用一个显示链接对象运行大约 10 个序列化动画可能会导致一些严重的意大利面条式代码。最终,我的问题是*为什么*暂停图层树中的动画会影响*后续*动画的延迟值?看起来很奇怪,不是吗? (2认同)