简体   繁体   English

出现UIView时重新启动CALayer动画

[英]Restart CALayer animation when UIView appears

I have a custom CALayer with an animatable property called progress . 我有一个自定义CALayer其中包含一个名为progress的动画属性。 The basic code is: 基本代码是:

@implementation AnimatedPieChartLayer

@dynamic progress;

+ (BOOL)needsDisplayForKey:(NSString *)key {
    return [key isEqualToString: @"progress"] || [super needsDisplayForKey: key];
}

- (id <CAAction>)actionForKey:(NSString *)key {
    if ([self presentationLayer] != nil) {
        if ([key isEqualToString: @"progress"]) {
            CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath: key];
            [anim setFromValue: [[self presentationLayer] valueForKey: key]];
            [anim setDuration: 0.75f];
            return anim;
        }
    }
    return [super actionForKey: key];
}

- (void)drawInContext:(CGContextRef)context {
  // do stuff
}

@end

In the view controller I do this, to try and get my animation to start from the beginning every time: 在视图控制器中我这样做,尝试每次从头开始动画:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];

    [pieChart setProgress: 0.0];
}

This all works perfectly. 一切都很完美。 I put the layer on a view, the view in a view controller, and it all works as expected. 我将图层放在视图上,视图控制器中的视图,它都按预期工作。

The complication is that my view controller is inside a UIScrollView . 复杂的是我的视图控制器在UIScrollView This presents a situation where the user could swipe away from my view controller before the CALayer animation completes. 这表示在CALayer动画完成之前用户可以从我的视图控制器滑动的情况。 If they swipe back quickly, the CALayer animation does something weird. 如果他们快速向后滑动, CALayer动画会做一些奇怪的事情。 It reverses. 它逆转了。 It's like the animation is trying to go back to the beginning. 这就像动画试图回到起点。 Once it gets there it starts over, and animates the way it is supposed to. 一旦它到达那里它重新开始,并以它应该的方式动画。

So the question is, how can I prevent this. 所以问题是,我该如何防止这种情况发生。 I want the animation to start from the beginning every time the view is displayed – regardless of what happened last time. 我希望每次显示视图动画都从头开始 - 无论上次发生了什么。

Any suggestions? 有什么建议么?

UPDATE UPDATE

Here's a visual example of the problem: 以下是问题的可视化示例:

Working: 工作:

在此输入图像描述

Failing: 失败:

在此输入图像描述

In the setup you have there, you have an implicit animation for the key "progress" so that whenever the progress property of the layer changes, it animates. 在那里的设置中,您有一个关键“progress”的隐式动画,这样每当图层的progress属性发生变化时,它就会生成动画。 This works both when the value increases and when it decreases (as seen in your second image). 这在值增加和减小时都有效(如第二张图片所示)。

To restore the layer to the default 0 progress state without an animation, you can wrap the property change in a CATransaction where all actions are disabled. 要在没有动画的情况下将图层恢复为默认的0进度状态,可以将属性更改包装在禁止所有操作的CATransaction中。 This will disable the implicit animation so that you can start over from 0 progress. 这将禁用隐式动画,以便您可以从0进度开始。

[CATransaction begin];
[CATransaction setDisableActions:YES]; // all animations are disabled ...
[pieChart setProgress: 0.0];
[CATransaction commit]; // ... until this line

Probably too simple a suggestion to merit an 'answer,' but I would suggest canceling the animation. 可能过于简单的建议值得“回答”,但我建议取消动画。 Looks like you could rewrite this code pretty easily so that the progress updates were checking some complete flag and then when the view get hidden, kill it. 看起来你可以很容易地重写这段代码,以便进度更新检查一些完整的标志,然后当视图被隐藏时,杀死它。 This thread has some good ideas on it. 这个帖子有一些很好的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM