Animer un CAShapeLayer pour dessiner un cercle de progression

Je suis en train de dessiner un cercle qui sera utilisée pour indiquer la progression. La progression des mises à jour pourrait venir rapidement, et je veux animer les changements à l'intérieur du cercle.

J'ai essayé de le faire avec les méthodes ci-dessous, mais rien ne semble fonctionner. Est-ce possible?

- (id)initWithFrame:(CGRect)frame strokeWidth:(CGFloat)strokeWidth insets:(UIEdgeInsets)insets
{
    if (self = [super initWithFrame:frame])
    {

        self.strokeWidth = strokeWidth;

        CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        CGFloat radius = CGRectGetMidX(self.bounds) - insets.top - insets.bottom;

        self.circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter
                                                         radius:radius
                                                     startAngle:M_PI
                                                       endAngle:-M_PI
                                                      clockwise:YES];
        [self addLayer];
    }
    return self;
}


- (void)setProgress:(double)progress {
    _progress = progress;
    [self updateAnimations];
}

- (void)addLayer {

    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.path = self.circlePath.CGPath;
    progressLayer.strokeColor = [[UIColor whiteColor] CGColor];
    progressLayer.fillColor = [[UIColor clearColor] CGColor];
    progressLayer.lineWidth = self.strokeWidth;
    progressLayer.strokeStart = 10.0f;
    progressLayer.strokeEnd = 40.0f;

    [self.layer addSublayer:progressLayer];
    self.currentProgressLayer = progressLayer;

}

- (void)updateAnimations{

    self.currentProgressLayer.strokeEnd = self.progress;
    [self.currentProgressLayer didChangeValueForKey:@"endValue"];
}

Actuellement, ce code n'a même pas de tracer le cercle, mais de supprimer le progressLayer de strokeStart et strokeEnd va dessiner le cercle complet.

Comment puis-je obtenir il pour commencer à un certain point, et de commencer à dessiner le cercle pour moi la définition de mon progress propriété?

Je ne veux pas utiliser un code tiers. Je veux comprendre comment le faire moi-même.
"Je ne veux pas utiliser un code tiers. Je veux comprendre comment le faire moi-même." +1. Je ressens la même chose.

OriginalL'auteur Nic Hubbard | 2014-02-19