日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 自定义控件 progressView(环形进度条)

發布時間:2025/3/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 自定义控件 progressView(环形进度条) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉帖:http://blog.csdn.net/xiangzhang321/article/details/42688133

之前做項目的時候有用到環形進度條,先是在網上找了一下第三方控件,發現好用是好用,就是東西太多了,有點復雜,還不如自己寫一個簡單點適合自己用的。

先把自定義控件的效果圖貼出來。

??? ??

其實我寫的這個控件很簡單。索性就直接把源碼貼出來吧。

.h文件的內容就是一些聲明

?

?

#import?<UIKit/UIKit.h>

?

@interface?ProgressView :?UIView

?

//中心顏色

@property?(strong,?nonatomic)UIColor?*centerColor;

//圓環背景色

@property?(strong,?nonatomic)UIColor?*arcBackColor;

//圓環色

@property?(strong,?nonatomic)UIColor?*arcFinishColor;

@property?(strong,?nonatomic)UIColor?*arcUnfinishColor;

?

?

//百分比數值(0-1)

@property?(assign,?nonatomic)float?percent;

?

//圓環寬度

@property?(assign,?nonatomic)float?width;

?

@end



?

.m文件里就是具體實現了

?

?

#import?"ProgressView.h"

?

@implementation?ProgressView

?

- (id)initWithFrame:(CGRect)frame{

? ??self?= [super?initWithFrame:frame];

? ??if?(self) {

? ? ? ??self.backgroundColor?=?ClearColor;

? ? ? ??_percent?=?0;

? ? ? ??_width?=?0;

? ? }

?? ?

? ??return?self;

}

?

- (void)setPercent:(float)percent{

? ??_percent?= percent;

? ? [self?setNeedsDisplay];

}

?

- (void)drawRect:(CGRect)rect{

? ? [self?addArcBackColor];

? ? [self?drawArc];

? ? [self?addCenterBack];

? ? [self?addCenterLabel];

}

?

- (void)addArcBackColor{

? ??CGColorRef?color = (_arcBackColor?==?nil) ? [UIColorlightGrayColor].CGColor?:?_arcBackColor.CGColor;

?

? ??CGContextRef?contextRef =?UIGraphicsGetCurrentContext();

? ??CGSize?viewSize =?self.bounds.size;

? ??CGPoint?center =?CGPointMake(viewSize.width?/?2, viewSize.height?/?2);

?? ?

? ??// Draw the slices.

? ??CGFloat?radius = viewSize.width?/?2;

? ??CGContextBeginPath(contextRef);

? ??CGContextMoveToPoint(contextRef, center.x, center.y);

? ??CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI,?0);

? ??CGContextSetFillColorWithColor(contextRef, color);

? ??CGContextFillPath(contextRef);

}

?

- (void)drawArc{

? ??if?(_percent?==?0?||?_percent?>?1) {

? ? ? ??return;

? ? }

?? ?

? ??if?(_percent?==?1) {

? ? ? ??CGColorRef?color = (_arcFinishColor?==?nil) ? [UIColorgreenColor].CGColor?:?_arcFinishColor.CGColor;

?? ? ? ?

? ? ? ??CGContextRef?contextRef =?UIGraphicsGetCurrentContext();

? ? ? ??CGSize?viewSize =?self.bounds.size;

? ? ? ??CGPoint?center =?CGPointMake(viewSize.width?/?2, viewSize.height?/?2);

? ? ? ??// Draw the slices.

? ? ? ??CGFloat?radius = viewSize.width?/?2;

? ? ? ??CGContextBeginPath(contextRef);

? ? ? ??CGContextMoveToPoint(contextRef, center.x, center.y);

? ? ? ??CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI,?0);

? ? ? ??CGContextSetFillColorWithColor(contextRef, color);

? ? ? ??CGContextFillPath(contextRef);

? ? }else{

?? ? ? ?

? ? ? ??float?endAngle =?2*M_PI*_percent;

?? ? ? ?

? ? ? ??CGColorRef?color = (_arcUnfinishColor?==?nil) ? [UIColorblueColor].CGColor?:?_arcUnfinishColor.CGColor;

? ? ? ??CGContextRef?contextRef =?UIGraphicsGetCurrentContext();

? ? ? ??CGSize?viewSize =?self.bounds.size;

? ? ? ??CGPoint?center =?CGPointMake(viewSize.width?/?2, viewSize.height?/?2);

? ? ? ??// Draw the slices.

? ? ? ??CGFloat?radius = viewSize.width?/?2;

? ? ? ??CGContextBeginPath(contextRef);

? ? ? ??CGContextMoveToPoint(contextRef, center.x, center.y);

? ? ? ??CGContextAddArc(contextRef, center.x, center.y, radius,0,endAngle,?0);

? ? ? ??CGContextSetFillColorWithColor(contextRef, color);

? ? ? ??CGContextFillPath(contextRef);

? ? }

?? ?

}

?

-(void)addCenterBack{

? ??float?width = (_width?==?0) ??5?:?_width;

?? ?

? ??CGColorRef?color = (_centerColor?==?nil) ? [UIColorwhiteColor].CGColor?:?_centerColor.CGColor;

? ??CGContextRef?contextRef =?UIGraphicsGetCurrentContext();

? ??CGSize?viewSize =?self.bounds.size;

? ??CGPoint?center =?CGPointMake(viewSize.width?/?2, viewSize.height?/?2);

? ??// Draw the slices.

? ??CGFloat?radius = viewSize.width?/?2?- width;

? ??CGContextBeginPath(contextRef);

? ??CGContextMoveToPoint(contextRef, center.x, center.y);

? ??CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI,?0);

? ??CGContextSetFillColorWithColor(contextRef, color);

? ??CGContextFillPath(contextRef);

}

?

- (void)addCenterLabel{

? ??NSString?*percent =?@"";

?

? ??float?fontSize =?14;

? ??UIColor?*arcColor = [UIColor?blueColor];

? ??if?(_percent?==?1) {

? ? ? ? percent =?@"100%";

? ? ? ? fontSize =?14;

? ? ? ? arcColor = (_arcFinishColor?==?nil) ? [UIColorgreenColor] :?_arcFinishColor;

?? ? ? ?

? ? }else?if(_percent?<?1?&&?_percent?>=?0){

?? ? ? ?

? ? ? ? fontSize =?13;

? ? ? ? arcColor = (_arcUnfinishColor?==?nil) ? [UIColorblueColor] :?_arcUnfinishColor;

? ? ? ? percent = [NSStringstringWithFormat:@"%0.2f%%",_percent*100];

? ? }

?? ?

? ??CGSize?viewSize =?self.bounds.size;

? ??NSMutableParagraphStyle?*paragraph = [[NSMutableParagraphStyle?alloc]?init];

? ? paragraph.alignment?=?NSTextAlignmentCenter;

? ??NSDictionary?*attributes = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontboldSystemFontOfSize:fontSize],NSFontAttributeName,arcColor,NSForegroundColorAttributeName,[UIColorclearColor],NSBackgroundColorAttributeName,paragraph,NSParagraphStyleAttributeName,nil];

?

? ? [percent?drawInRect:CGRectMake(5, (viewSize.height-fontSize)/2, viewSize.width-10, fontSize)withAttributes:attributes];

}

?

@end

具體的調用就是

??ProgressView?*progress = [[ProgressViewalloc]initWithFrame:CGRectMake(detil.width-65,?10,?60,?60)];

? ? progress.arcFinishColor?=?COLOR_STRING(@"#75AB33");

? ? progress.arcUnfinishColor?=?COLOR_STRING(@"#0D6FAE");

? ? progress.arcBackColor?=?COLOR_STRING(@"#EAEAEA");

? ? progress.percent?=?1;

? ? [detil?addSubview:progress];

轉載于:https://www.cnblogs.com/zixiadaxian/p/4704030.html

總結

以上是生活随笔為你收集整理的iOS 自定义控件 progressView(环形进度条)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。