IOS-一个自定义进度条的小例子

    //  processDIY.m
    
    #import "processDIY.h"
    
    
    
    static const CGFloat kBorderWidth = 2.0f;
    
    
    
    @interface THProgressLayer : CAReplicatorLayer
    
    @property (nonatomic, strong) UIColor* progressTintColor;
    
    @property (nonatomic, strong) UIColor* borderTintColor;
    
    @property (nonatomic) CGFloat progresstemp;
    
    @end
    
    
    
    @implementation THProgressLayer
    
    
    
    @dynamic progressTintColor;
    
    @dynamic borderTintColor;
    
    
    
    + (BOOL)needsDisplayForKey:(NSString *)key
    
    {
        
        return [key isEqualToString:@"progresstemp"] ? YES : [super needsDisplayForKey:key];
        
    }
    
    
    
    - (void)drawInContext:(CGContextRef)context
    
    {
        
        [self isKindOfClass:[NSString class]];
        
        
        
        CGRect rect = CGRectInset(self.bounds, kBorderWidth, kBorderWidth);
        
        CGFloat radius = CGRectGetHeight(rect) / 2.0f;
        
        CGContextSetLineWidth(context, kBorderWidth);
        
        CGContextSetStrokeColorWithColor(context, self.borderTintColor.CGColor);
        
        
        
        [self drawRectangleInContext:context inRect:rect withRadius:radius];
        
        CGContextStrokePath(context);
        
        
        
        CGContextSetFillColorWithColor(context, self.progressTintColor.CGColor);
        
        CGRect progressRect = CGRectInset(rect, 2 * kBorderWidth, 2 * kBorderWidth);
        
        CGFloat progressRadius = CGRectGetHeight(progressRect) / 2.0f;
        
        progressRect.size.width = fmaxf(self.progresstemp * progressRect.size.width, 2.0f * progressRadius);
        
        [self drawRectangleInContext:context inRect:progressRect withRadius:progressRadius];
        
        CGContextFillPath(context);
        
    }
    
    - (void)drawRectangleInContext:(CGContextRef)context inRect:(CGRect)rect withRadius:(CGFloat)radius
    
    {
        
        CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
        
        CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
        
        CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
        
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
        
        CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
        
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
        
        CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1);
        
        CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
        
        CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1);
        
    }
    
    @end
    
    
    
    
    
    @implementation processDIY
    
    
    
    -(instancetype)initWithFrame:(CGRect)frame
    
    {
        
        self = [super initWithFrame:frame];
        
        if (self) {
            
            self.backgroundColor = [UIColor clearColor];
            
            UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
            
            tapGesture.numberOfTapsRequired=1;
            
            [self addGestureRecognizer:tapGesture];
            
        }
        
        return self;
        
    }
    
    -(IBAction)handleTapGesture:(UITapGestureRecognizer*)sender{
        
        CGPoint tapPoint = [sender locationInView:self];
        
        NSLog(@"x:%f,y:%f",tapPoint.x,tapPoint.y);
        
    }
    
    -(void)click
    
    {
        
        NSLog(@"click me");
        
    }
    
    + (Class)layerClass
    
    {
        
        return [THProgressLayer class];
        
    }
    
    - (void)didMoveToWindow
    
    {
        
        self.progressLayer.contentsScale = self.window.screen.scale;
        
    }
    
    - (THProgressLayer *)progressLayer
    
    {
        
        return (THProgressLayer *)self.layer;
        
    }
    
    -(CGFloat)progress
    
    {
        
        return self.progressLayer.progresstemp;
        
    }
    
    -(void)setProcessnow:(CGFloat)progress
    
    {
        
        [self.progressLayer removeAnimationForKey:@"progresstemp"];
        
        CGFloat pinnedProgress = MIN(MAX(progress, 0.0f), 1.0f);
        
        
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"progresstemp"];
        
        animation.fromValue = [NSNumber numberWithFloat:self.progress];
        
        animation.toValue = [NSNumber numberWithFloat:pinnedProgress];
        
        CGFloat time = fabs(self.progress-pinnedProgress)+0.3;
        
        animation.duration = time;
        
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        
        
        
        [self.progressLayer addAnimation:animation forKey:@"progresstemp"];
        
        self.progressLayer.progresstemp = pinnedProgress;
        
    }
    
    - (UIColor *)progressTintColor
    
    {
        
        return self.progressLayer.progressTintColor;
        
    }
    
    
    
    - (void)setProgressTintColor:(UIColor *)progressTintColor
    
    {
        
        self.progressLayer.progressTintColor = progressTintColor;
        
        [self.progressLayer setNeedsDisplay];
        
    } 
    
    - (UIColor *)borderTintColor
    
    {
        
        return self.progressLayer.borderTintColor;
        
    }
    
    - (void)setBorderTintColor:(UIColor *)borderTintColor
    
    {
        
        self.progressLayer.borderTintColor = borderTintColor;
        
        [self.progressLayer setNeedsDisplay];
        
    }
    
    @end

你可能感兴趣的:(IOS-一个自定义进度条的小例子)