ios - 带动画圆形旋转的进度条

#import <UIKit/UIKit.h>

@interface TJCircleProgressView : UIView
/**
 *  图标
 */
@property(nonatomic,strong)UIImage *imgIcon;
/**
 *  进度条值
 */
@property(nonatomic,assign)CGFloat progressValue;
/**
 *  进度条宽度
 */
@property(nonatomic,assign)CGFloat progressWidth;

/**
 *  进度条颜色
 */
@property(nonatomic,strong)UIColor *progressColor;

@end
#import "TJCircleProgressView.h"
@interface TJCircleProgressView ()
{
    UIBezierPath *circlePath;//布赛尔曲线
    CAShapeLayer *shapeLayer;// 圆形图层
    CAShapeLayer *imgLayer;//图标图层

}
@end
@implementation TJCircleProgressView
@synthesize progressColor = _progressColor;
@synthesize imgIcon = _imgIcon;
@synthesize progressValue = _progressValue;
@synthesize progressWidth = _progressWidth;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        circlePath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
        
        shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = self.bounds;
        shapeLayer.strokeColor = [UIColor redColor].CGColor;
        shapeLayer.fillColor   = [UIColor clearColor].CGColor;
        shapeLayer.path = circlePath.CGPath;
        
        shapeLayer.lineWidth   = 1.0f;
        [self.layer addSublayer:shapeLayer];
    }
    return self;
}
- (void)setImgIcon:(UIImage *)imgIcon
{
    _imgIcon = imgIcon;
    imgLayer = [CAShapeLayer layer];
    imgLayer.frame = CGRectMake(0, 0, imgIcon.size.width, imgIcon.size.height);
    imgLayer.contents = (__bridge id)imgIcon.CGImage;
    imgLayer.position = shapeLayer.position;
    [self.layer addSublayer:imgLayer];

}
- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    shapeLayer.strokeEnd = progressValue;
    [self startAnimation];
    /**延时4秒后移除动画以及view*/
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self stopAnimation];
    });
  }
- (void)setProgressWidth:(CGFloat)progressWidth
{
    _progressWidth = progressWidth;
    shapeLayer.lineWidth = progressWidth;
}
- (void)setProgressColor:(UIColor *)progressColor
{
    _progressColor = progressColor;
    shapeLayer.strokeColor = progressColor.CGColor;
}
-(void)startAnimation
{
    [shapeLayer removeAnimationForKey:@"RotationAnimation"];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = @(0);
    animation.toValue = @(M_PI * 2);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 0.5f;
    animation.fillMode = kCAFillModeForwards;
    
    [shapeLayer addAnimation:animation forKey:@"RotationAnimation"];
}
- (void)stopAnimation
{
    [shapeLayer removeAllAnimations];
    [self removeFromSuperview];
}
@end
#import "ViewController.h"
#import "TJCircleProgressView.h"
@interface ViewController ()
{
    TJCircleProgressView *circleProgressView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    circleProgressView = [[TJCircleProgressView alloc]initWithFrame:CGRectMake(0,0, 100, 100)];
    circleProgressView.center = self.view.center;
    [circleProgressView setProgressValue:0.75];
    [circleProgressView setProgressColor:[UIColor grayColor]];
    [circleProgressView setProgressWidth:1.0f];
    [circleProgressView setImgIcon:[UIImage imageNamed:@"refresh"]];
    [self.view addSubview:circleProgressView];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ps:licecap录取动画没录好

posted @ 2015-05-26 14:03  曹县三胖暴打大猩猩  阅读(759)  评论(0编辑  收藏  举报