zoukankan      html  css  js  c++  java
  • ios 画圆环进度条

    #import <UIKit/UIKit.h>
    
    @interface SNCircleProgressView : UIView
    /**
     *  进度值0-1.0之间
     */
    @property (nonatomic,assign)CGFloat progressValue;
    
    /**
     *  边宽
     */
    @property(nonatomic,assign) CGFloat progressStrokeWidth;
    
    /**
     *  进度条颜色
     */
    @property(nonatomic,strong)UIColor *progressColor;
    
    /**
     *  进度条轨道颜色
     */
    @property(nonatomic,strong)UIColor *progressTrackColor;
    
    @end
    
    #import "SNCircleProgressView.h"
    
    @interface SNCircleProgressView ()
    {
        
        CAShapeLayer *backGroundLayer; //背景图层
        CAShapeLayer *frontFillLayer;      //用来填充的图层
        UIBezierPath *backGroundBezierPath; //背景贝赛尔曲线
        UIBezierPath *frontFillBezierPath;  //用来填充的贝赛尔曲线
        
    
    }
    @end
    
    
    @implementation SNCircleProgressView
    @synthesize progressColor = _progressColor;
    @synthesize progressTrackColor = _progressTrackColor;
    @synthesize progressValue = _progressValue;
    @synthesize progressStrokeWidth = _progressStrokeWidth;
    - (instancetype)initWithCoder:(NSCoder *)aDecoder
    {
        if (self = [super initWithCoder:aDecoder]) {
            [self setUp];
        }
        return self;
    }
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame])
        {
            [self setUp];
            
        }
        return self;
    
    }
    /**
     *  初始化创建图层
     */
    - (void)setUp
    {
        //创建背景图层
        backGroundLayer = [CAShapeLayer layer];
        backGroundLayer.fillColor = nil;
        backGroundLayer.frame = self.bounds;
        
        //创建填充图层
        frontFillLayer = [CAShapeLayer layer];
        frontFillLayer.fillColor = nil;
        frontFillLayer.frame = self.bounds;
        
      
        [self.layer addSublayer:backGroundLayer];
        [self.layer addSublayer:frontFillLayer];
    }
    
    - (void)setProgressColor:(UIColor *)progressColor
    {
        _progressColor = progressColor;
        frontFillLayer.strokeColor = progressColor.CGColor;
    }
    - (UIColor *)progressColor
    {
        return _progressColor;
    }
    - (void)setProgressTrackColor:(UIColor *)progressTrackColor
    {
        _progressTrackColor = progressTrackColor;
        backGroundLayer.strokeColor = progressTrackColor.CGColor;
        backGroundBezierPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(CGRectGetWidth(self.bounds)-self.progressStrokeWidth)/2.f startAngle:0 endAngle:M_PI*2
                                                           clockwise:YES];
        backGroundLayer.path = backGroundBezierPath.CGPath;
    }
    - (UIColor *)progressTrackColor
    {
        return _progressTrackColor;
    }
    - (void)setProgressValue:(CGFloat)progressValue
    {
        _progressValue = progressValue;
        frontFillBezierPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(CGRectGetWidth(self.bounds)-self.progressStrokeWidth)/2.f startAngle:-M_PI_4 endAngle:(2*M_PI)*progressValue-M_PI_4 clockwise:YES];
        frontFillLayer.path = frontFillBezierPath.CGPath;
    }
    - (CGFloat)progressValue
    {
        return _progressValue;
    }
    - (void)setProgressStrokeWidth:(CGFloat)progressStrokeWidth
    {
        _progressStrokeWidth = progressStrokeWidth;
        frontFillLayer.lineWidth = progressStrokeWidth;
        backGroundLayer.lineWidth = progressStrokeWidth;
    }
    - (CGFloat)progressStrokeWidth
    {
        return _progressStrokeWidth;
    }
    @end
    #import "ViewController.h"
    #import "SNCircleProgressView.h"
    @interface ViewController ()
    {
      SNCircleProgressView *progressView;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        progressView = [[SNCircleProgressView alloc]initWithFrame:CGRectMake(40,80, 100, 100)];
       
        progressView.progressColor = [UIColor redColor];
        progressView.progressStrokeWidth = 5.f;
        progressView.progressTrackColor = [UIColor whiteColor];
        
        [self.view addSubview:progressView];
        
        [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(changeProgressValue) userInfo:nil repeats:YES];
        
    }
    - (void)changeProgressValue
    {
        progressView.progressValue += 0.1;
        if (progressView.progressValue>=1.f) {
            progressView.progressValue = 0.1f;
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

  • 相关阅读:
    HDU4507 吉哥系列故事――恨7不成妻(数位dp)
    UCF Local Programming Contest 2017 G题(dp)
    ICPC Latin American Regional Contests 2019 I题
    UCF Local Programming Contest 2017 H题(区间dp)
    HDU2089 不要62
    AcWing1084 数字游戏II(数位dp)
    UCF Local Programming Contest 2017 F题(最短路)
    Google Code Jam 2019 Round 1A Pylons(爆搜+贪心)
    AcWing1083 Windy数(数位dp)
    Vue
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4516726.html
Copyright © 2011-2022 走看看