如何使路径向前和向后动画?

时间:2017-02-02 19:31:25

标签: ios objective-c xcode

我已经动画了一个UIBezier路径来围绕一个圆圈并改变它的笔触颜色,同时这样做。当它一路走来时,我希望它在相反的方向上一路走来。

以下是使其前进的代码:

 [CATransaction begin];
    {
        [CATransaction setAnimationDuration: 3.0];//Dynamic Duration
        [CATransaction setCompletionBlock:^{
            [tutorialCircle removeFromSuperlayer];
            tutorialCircle.opacity = 0.0;
            strokePart.opacity = 0.0;
            [strokePart removeFromSuperlayer];
        }];

        const double portion = 1.0 / ((double)3);

        for (NSUInteger i = 0; i < 3; i++)
        {
            strokePart = [[CAShapeLayer alloc] init];
            strokePart.fillColor = [[UIColor clearColor] CGColor];
            strokePart.frame = tutorialCircle.bounds;
            strokePart.path = tutorialCircle.path;
            strokePart.lineCap = tutorialCircle.lineCap;
            strokePart.lineWidth = tutorialCircle.lineWidth;

            if (i == 0) {
                strokePart.strokeColor = [UIColor greenColor].CGColor;
            }

            else if (i == 1) {
                strokePart.strokeColor = [UIColor orangeColor].CGColor;
            }

            else if (i == 2) {
                strokePart.strokeColor = [UIColor redColor].CGColor;
            }
            strokePart.strokeStart = i * portion;
            strokePart.strokeEnd = (i + 1) * portion;

            [tutorialCircle addSublayer: strokePart];

            animation = [CAKeyframeAnimation animationWithKeyPath: @"strokeEnd"];
            NSArray* times = @[ @(0.0), // Note: This works because both the times and the stroke start/end are on scales of 0..1
                                @(strokePart.strokeStart),
                                @(strokePart.strokeEnd),
                                @(1.0) ];
            NSArray* values = @[ @(strokePart.strokeStart),
                                 @(strokePart.strokeStart),
                                 @(strokePart.strokeEnd),
                                 @(strokePart.strokeEnd) ];

            animation.keyTimes = times;
            animation.values = values;
            animation.removedOnCompletion = YES;
            animation.fillMode = kCAFillModeForwards;
            [animation setDelegate:self];
            [strokePart addAnimation: animation forKey: @"whatever"];
        }
    }
    [CATransaction commit];

所以在我的第一个动画的animationDidStop委托方法中,我知道我会调用第二个动画向后移动,但是,我正在努力创建动画以使其向后移动。

1 个答案:

答案 0 :(得分:0)

你试过吗?

animation.autoreverses = YES