【问题标题】:How to remove strange delay between two sequential CAKeyframeAnimation animations?如何消除两个连续 CAKeyframeAnimation 动画之间的奇怪延迟?
【发布时间】:2017-10-11 22:26:01
【问题描述】:

iOS 编码器!

此代码用于动画小红方块以在UIView 上绘制大符号“8”。首先是上环(animSequence1 = 5 秒),然后是下环(animSequence2 = 再过 5 秒)。
无需延迟!
但是我在两个连续动画之间遇到了奇怪的延迟(大约 1 秒)。这个延迟是从哪里来的?!?

代码:

- (void)drawRect:(CGRect)drawRect {
   CGContextRef context = UIGraphicsGetCurrentContext();
   drawRect = CGRectMake(0, 0, 320, 320);
   CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor;);
   CGContextFillRect(context, drawRect);

   // Little red square
   CALayer *layerTest;
   layerTest = [CALayer layer];
   layerTest.bounds = CGRectMake(0, 0, 20, 20);
   layerTest.anchorPoint = CGPointMake(0, 0);
   layerTest.backgroundColor = [UIColor redColor].CGColor;
   [[self layer] addSublayer:layerTest];

   // Upper ring
   CAKeyframeAnimation *animSequence1;
   animSequence1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   animSequence1.fillMode = kCAFillModeForwards;
   animSequence1.removedOnCompletion = NO;
   animSequence1.autoreverses = NO;
   animSequence1.repeatCount = 0;
   animSequence1.duration = 5.0;
   animSequence1.beginTime = 0.0;
   animSequence1.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 120) radius:80 startAngle:DEG_TO_RAD(90) endAngle:DEG_TO_RAD(450) clockwise:YES].CGPath;
   animSequence1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

   // Lower ring
   CAKeyframeAnimation *animSequence2;
   animSequence2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   animSequence2.fillMode = kCAFillModeForwards;
   animSequence2.removedOnCompletion = NO;
   animSequence2.autoreverses = NO;
   animSequence2.repeatCount = 0;
   animSequence2.duration = 5.0;
   animSequence2.beginTime = 5.0;
   animSequence2.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 280) radius:80 startAngle:DEG_TO_RAD(-90) endAngle:DEG_TO_RAD(-450) clockwise:NO].CGPath;
   animSequence2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

   // A sequence of animations
   CAAnimationGroup *animGroup;
   layerTest.position = CGPointMake(200, 200);
   animGroup = [CAAnimationGroup animation];
   animGroup.duration = 10.0;
   animGroup.animations = [NSArray arrayWithObjects:animSequence1, animSequence2, nil];

   [layerTest addAnimation:animGroup forKey:nil];
}

我也试过不用CAAnimationGroup
为第一个CAKeyframeAnimation (animSequence1) 设置[animSequence1 setDelegate:self];,然后由(void)animationDidStop 启动第二个CAKeyframeAnimation (animSequence2)。
它的工作原理相同,但是两个动画之间的这种奇怪的延迟(大约 1 秒)没有消失!

问题:如何消除两个连续的CAKeyframeAnimation 动画之间的奇怪延迟? 无需延迟!

【问题讨论】:

    标签: ios objective-c xcode animation cakeyframeanimation


    【解决方案1】:

    不要在-drawRect: 中创建图层或动画——您无法完全控制何时调用它,因此您最终会在视图中添加多个layerTests,所有人都试图单独制作动画。在初始化程序中创建图层/动画,或响应某些用户交互。

    我还建议在animSequence1 上删除您的fillModeremovedOnCompletion 设置;一旦animSequence2 启动,您实际上是在要求 CA 在同一属性上运行两个非累加动画一次。不确定这种行为是否定义明确,但这是怪异可能来自的另一个地方。

    【讨论】:

    • 诺亚,感谢您的建议。我将我的代码移至-(void)didMoveToWindow 方法。并从animSequence1 中删除了fillModeremovedOnCompletion。但问题依然存在!两个连续的动画animSequence1animSequence2 之间有一个奇怪的延迟(大约 1 秒)。还有其他想法如何消除这种奇怪的延迟吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多