wrong setStrokeEnd CAShapeLayer

99 views Asked by At

I try to crate a simple circle progress, the proress work but with a wrong value, the cirlce is complete on 0.8 instead 1, half circle is complete at 0.4, I don't find the error:

NSLog(_pathLayer) print 0.1,0.2,...1.0 so seems to be ok.

- (void)drawRect:(CGRect)rect {

    /* Cerchio rosso centrale */
    UIBezierPath *circlePath = [UIBezierPath bezierPath];
    [circlePath addArcWithCenter:CGPointMake(self.frame.size.width/2,self.frame.size.height/2)
                          radius:60
                      startAngle:degreesToRadians(-90)
                        endAngle:degreesToRadians(360)
                       clockwise:YES];

    [self.layer addSublayer:[self disegnaLinea:circlePath
                                        andWidth:_strokeWidth
                                        andColor:_color
                                           toEnd:0]];
}


- (void)setProgress:(double)progress {

    if (progress != _progress)
    {
        _progress = progress;
        [self updateAnimations];


        NSLog(@"_pathLayer: %f",_pathLayer.strokeEnd);
    }


}

- (CAShapeLayer *) disegnaLinea:(UIBezierPath *)path andWidth:(float)width andColor:(UIColor *)color toEnd:(int)end {

    _pathLayer = [CAShapeLayer layer];
    _pathLayer.frame = self.bounds;
    _pathLayer.path = path.CGPath;
    _pathLayer.strokeColor = [color CGColor];
    _pathLayer.fillColor = nil;
    _pathLayer.lineWidth = width;
    [_pathLayer setStrokeStart:0];
    [_pathLayer setStrokeEnd:end];

    return _pathLayer;
}

- (void) updateAnimations {

    [_pathLayer setStrokeEnd:_progress];
    [_pathLayer didChangeValueForKey:@"endValue"];

    [_progressLabel setText:[NSString stringWithFormat:@"%f",_progress]];
}
1

There are 1 answers

0
Uros On BEST ANSWER

If you do (endAngle - startAngle) you'll get 450 degrees, which basically means your path is longer than one full circle. My guess would be to use 270 for endAngle. Cheers.