排队CAAnimations时出现故障

时间:2015-10-15 11:53:59

标签: ios animation visual-glitch

我有一个CAShapeLayer,我在其中设置一个圆形动画。动画首先顺时针“展开”圆圈,然后顺时针重绘圆圈。一种“旋转圆”。放置它的另一种方法:将路径行程终点移动到开始,然后将起点移动到结束。

动画本身有效,但它偶尔会产生毛刺。当它被认为是“未绘制的”时,它会在整个圆圈中略显一瞥。

为什么会发生这种情况?如何解决?

谢谢,

// Shape creation
layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.width - 2 * OUTER_BORDER_WIDTH, self.width - 2* OUTER_BORDER_WIDTH)].CGPath;

// Animation queuing
-(void) applyNextAnimation
{

    CABasicAnimation* animation;

    if (self.animatingOpening)
    {
        animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        self.animatingOpening = NO;
    }
    else
    {
    animation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        self.animatingOpening = YES;
    }

    animation.duration = 1.0f;
    animation.autoreverses = NO;
    animation.delegate = self;
    animation.removedOnCompletion = YES;
    [self.outerCircleLayer addAnimation:animation forKey:@"stroke"];
}

// Animation stop callback
-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (self.isAnimating)
    {
        [self applyNextAnimation];
    }
}

1 个答案:

答案 0 :(得分:1)

它闪烁,因为你没有在图层上设置相应的属性。因此,当动画完成时,图层的模型仍处于预先动画状态,这就是您在两个动画之间暂时看到的内容。

这会让你走向你想要的......

if (self.animatingOpening)
{

    self.outerCircleLayer.strokeStart = 0.0;

    animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    self.animatingOpening = NO;
}
else
{
    self.outerCircleLayer.strokeStart = 1.0;

    animation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    self.animatingOpening = YES;
}

animation.duration = 1.0f;
animation.autoreverses = NO;

几乎可以正常工作,但是当您从未拉伸状态转换到开始绘制状态动画时,您会注意到更微妙的故障。圆圈的开头在开始时有一个小的反向动画。这是一个通过将strokeStart从1.0设置为0.0而触发的隐式动画:您需要摆脱它,以便所有动画效果都在您的控制之下。您可以通过在CATransaction上将disableActions设置为YES来实现最简单的目的:

[CATransaction setDisableActions:YES];

(将其添加到if (self.animatingOpening)上方)