ArcToCircleLayer.m 1.86 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
//
//  ArcToCircleLayer.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/1/24.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "ArcToCircleLayer.h"
#import "UIColor+SAMAdditions.h"

static CGFloat const kLineWidth = 1.2;

@implementation ArcToCircleLayer

@dynamic progress;

+ (BOOL)needsDisplayForKey:(NSString *)key {
    if ([key isEqualToString:@"progress"]) {
        return YES;
    }
    
    return [super needsDisplayForKey:key];
}

- (void)drawInContext:(CGContextRef)ctx {
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) / 2 - kLineWidth / 2;
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    // O
//    CGFloat originStart = M_PI * 7 / 2;
//    CGFloat originEnd = M_PI * 2;
//    CGFloat currentOrigin = originStart - (originStart - originEnd) * self.progress;
    
    CGFloat originStart = M_PI * 5;
    CGFloat originEnd = M_PI * 1;
  
    
    CGFloat currentOrigin = originStart - (originStart - originEnd) * (1-cosf(M_PI*self.progress/2));
    if(self.progress<0){
        currentOrigin = originStart;
    }
    
    // D
//    CGFloat destStart = M_PI * 3;
//    CGFloat destEnd = 0;
//    CGFloat currentDest = destStart - (destStart - destEnd) * self.progress;
    
    CGFloat destStart = M_PI * 5;
    CGFloat destEnd = M_PI * 1;
    CGFloat currentDest = destStart - (destStart - destEnd) * self.progress;
    
    [path addArcWithCenter:center radius:radius startAngle: currentOrigin endAngle:currentDest clockwise:NO];
    CGContextAddPath(ctx, path.CGPath);
    CGContextSetLineWidth(ctx, kLineWidth);
//    CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
    //EF8891
    CGContextSetStrokeColorWithColor(ctx, [UIColor sam_colorWithHex:@"EF8891"].CGColor);
    CGContextStrokePath(ctx);
}

@end