//// CircleView.h// animation//// Created by XUAN on 16/12/8.// Copyright © 2016年 XUAN. All rights reserved.//#import@interface CircleView : UIView
@property(strong,nonatomic)UIBezierPath *path;
@property(strong,nonatomic)CAShapeLayer *shapeLayer;
@property(strong,nonatomic)CAShapeLayer *bgLayer;
@property (nonatomic) CGFloat value;
@property (strong, nonatomic) UIColor *lineColor;
@property (nonatomic) CGFloat lineWidth;
- (void)setProgress:(float)progress animated:(BOOL)animated;
@end
//
// CircleView.m
// animation
//
// Created by 宣磊磊 on 16/12/8.
// Copyright © 2016年 宣磊磊. All rights reserved.
//
#import "CircleView.h"
@implementation CircleView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
_bgLayer = [CAShapeLayer layer];
_bgLayer.frame = self.bounds;
_bgLayer.fillColor = [UIColor clearColor].CGColor;
_bgLayer.lineWidth = 2.f;
_bgLayer.strokeColor = [UIColor grayColor].CGColor;
_bgLayer.strokeStart = 0.f;
_bgLayer.strokeEnd = 1.f;
_bgLayer.path = _path.CGPath;
[self.layer addSublayer:_bgLayer];
_shapeLayer = [CAShapeLayer layer];
_shapeLayer.frame = self.bounds;
_shapeLayer.fillColor = [UIColor clearColor].CGColor;
_shapeLayer.lineWidth = 2.f;
_shapeLayer.lineCap = kCALineCapRound;
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
_shapeLayer.strokeStart = 0.f;
_shapeLayer.strokeEnd = 0.f;
_shapeLayer.path = _path.CGPath;
[self.layer addSublayer:_shapeLayer];
}
return self;
}
@synthesize value = _value;
-(void)setValue:(CGFloat)value{
_value = value;
_shapeLayer.strokeEnd = value;
}
-(CGFloat)value{
return _value;
}
@synthesize lineColor = _lineColor;
-(void)setLineColor:(UIColor *)lineColor{
_lineColor = lineColor;
_shapeLayer.strokeColor = lineColor.CGColor;
}
-(UIColor*)lineColr{
return _lineColor;
}
@synthesize lineWidth = _lineWidth;
-(void)setLineWidth:(CGFloat)lineWidth{
_lineWidth = lineWidth;
_shapeLayer.lineWidth = lineWidth;
_bgLayer.lineWidth = lineWidth;
}
-(CGFloat)lineWidth{
return _lineWidth;
}
- (void)setProgress:(float)progress animated:(BOOL)animated {
_shapeLayer.strokeEnd = progress;
}
@end
使用方法:
CircleView *view = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; *
[self.view addSubview:view];
view.center = self.view.center;
[view setLineWidth:6.f];
[view setLineColor:[UIColor redColor]];
timers = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(animate) userInfo:nil repeats:YES];
-(void)animate{
progressCount += 0.1;
[view setProgress:progressCount animated:YES];
}