iOS 簡易環形進度條

來源:互聯網
上載者:User

標籤:

demo:https://github.com/haozheMa/LoopProgressDemo/tree/master

ViewController中的代碼

#import "ViewController.h"#import "ProgressView.h"@interface ViewController ()@property (strong, nonatomic) UISlider *progressSlider;@property (strong, nonatomic) ProgressView *progressView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _progressView = [[ProgressView alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2 - 80, 100, 160, 160)];    [self.view addSubview:_progressView];    _progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-80, 400, 160, 10)];    [_progressSlider addTarget:self action:@selector(sliderChange) forControlEvents:UIControlEventValueChanged];    _progressSlider.maximumValue = 1.0;    _progressSlider.minimumValue = 0.0;    [self.view addSubview:_progressSlider];}-(void)sliderChange{    _progressView.percentage = _progressSlider.value;}@end

 ProgressView中的代碼

.h#import <UIKit/UIKit.h>@interface ProgressView : UIView@property(assign, nonatomic)CGFloat percentage;@end.m#import "ProgressView.h"@interface ProgressView ()@property (strong,nonatomic) UILabel *label;@end@implementation ProgressView-(instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.percentage = 0.0;        self.backgroundColor = [UIColor whiteColor];        [self loadSubviews];    }    return self;}-(void)loadSubviews{    _label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.frame.size.height/2 - 10, self.frame.size.width - 20, 20)];    _label.textAlignment = NSTextAlignmentCenter;    _label.font = [UIFont systemFontOfSize:12];    _label.textColor = [UIColor blackColor];    [self addSubview:_label];}-(void)setPercentage:(CGFloat)percentage{    _percentage = percentage;    _label.text = [NSString stringWithFormat:@"%.2f%%",_percentage*100];    [self setNeedsDisplay];}-(void)drawRect:(CGRect)rect{    CGContextRef contextRef = UIGraphicsGetCurrentContext();    CGSize viewSize = self.bounds.size;    CGPoint center = CGPointMake(viewSize.width/2, viewSize.height/2);    CGFloat radius = viewSize.width/2;    CGContextBeginPath(contextRef);    CGContextMoveToPoint(contextRef, center.x, center.y);    CGContextAddArc(contextRef, center.x, center.y, radius, - M_PI_2, 2*M_PI*_percentage - M_PI_2, 0);    CGContextSetFillColorWithColor(contextRef, [UIColor redColor].CGColor);    CGContextFillPath(contextRef);        //填充圓,無邊框    CGContextAddArc(contextRef, center.x, center.y, radius - 10, 0, 2*M_PI, 0); //添加一個圓    CGContextSetFillColorWithColor(contextRef, [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1].CGColor);    CGContextClosePath(contextRef);    CGContextDrawPath(contextRef, kCGPathFill);//繪製填充    CGContextStrokePath(contextRef);//繪畫路徑}

 

iOS 簡易環形進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.