动画为,圆的两端以顺时针方向移动

时间:2015-02-02 08:18:48

标签: ios iphone

我必须在IOS中创建一个圆形动画,圆圈的两端增加,实际上意味着一端以慢速增加,另一端以顺时针方向高速增加。为了完成整个圈子。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码。

-(CAShapeLayer *) createArcWithCenter:(CGPoint)center radius:(CGFloat)radius
                           startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle
                           clockwise : (BOOL)clockwise duration :(CGFloat)duration
{
    CAShapeLayer *circle = [CAShapeLayer layer];
    circle.path = [UIBezierPath
                   bezierPathWithArcCenter:center radius:radius
                   startAngle:startAngle endAngle:endAngle
                   clockwise:clockwise].CGPath;

    circle.fillColor = [UIColor clearColor].CGColor;
    circle.strokeColor = [UIColor greenColor].CGColor;
    circle.lineWidth = 4;

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    animation.fromValue = @(0);
    animation.toValue = @(1);
    animation.timingFunction=  [CAMediaTimingFunction
                                functionWithName:kCAMediaTimingFunctionLinear];

    [circle addAnimation:animation forKey:@"drawCircleAnimation"];

    return  circle;
}

-(void)viewDidAppear:(BOOL)animated
{

    CGFloat startAngle = 0.0;
    CGFloat endAngle1  = M_PI * 2.0 / 2;
    CGFloat endAngle2 = M_PI * 2.0 - endAngle1;


    CGPoint center = CGPointMake(50, 50);
    CGFloat radius = 50;
    CGFloat duration = 6.0;

    UIView *circleView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, radius * 2 , radius * 2)];
    [self.view addSubview:circleView];

    CAShapeLayer *arc1 = [self
                          createArcWithCenter:center radius:radius
                          startAngle:startAngle endAngle:endAngle1
                          clockwise:YES duration:duration];

    arc1.strokeColor = [[UIColor redColor] CGColor];

    CAShapeLayer *arc2 = [self
                          createArcWithCenter:center radius:radius
                          startAngle:startAngle endAngle:endAngle2
                          clockwise:NO duration:duration];

    arc2.strokeColor = [[UIColor redColor] CGColor];

    [circleView.layer addSublayer:arc1];
    [circleView.layer addSublayer:arc2];
    //circleView.layer.anchorPoint = CGPointMake(0.5, 0.5);

    [UIView animateKeyframesWithDuration:duration delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{

        int numberOfAnimations = 8;
        CGFloat durationPerAnimation = 1.0/numberOfAnimations;
        for (int i = 0; i < numberOfAnimations; i++)
        {
            NSLog(@"%f",i*durationPerAnimation);
            [UIView addKeyframeWithRelativeStartTime:(i * durationPerAnimation) relativeDuration:durationPerAnimation animations:^{
                circleView.transform = CGAffineTransformRotate(circleView.transform, 3 * M_PI);
            }];
        }

    } completion:^(BOOL finished) {

    }];

}

您可以改变endAngle1endAngle2来改变每个弧的角度。