首页 > 代码库 > iOS Quartz2D模拟下载进度条
iOS Quartz2D模拟下载进度条
效果图:
步骤:
1.在StoryBoard中拖入一个控制器添加UISlider和UIView 2个控件
2.在控制器中连线监听UISlider的值变化事件、HJProgressView属性,把变化的值传递给自定义UIView
3.自定义HJProgressView重写progressValue属性set方法,重绘视图中得文字和弧度值
控制器代码:
#import "ViewController.h"#import "HJProgressView.h"@interface ViewController ()@property (nonatomic,strong) IBOutlet HJProgressView *progressView;- (IBAction)didSliderValueChange:(UISlider *)sender;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/** * sender.value值 0.0~1.0 * * @param sender <#sender description#> */- (IBAction)didSliderValueChange:(UISlider *)sender { NSLog(@"%f",sender.value); self.progressView.progressValue = http://www.mamicode.com/sender.value;>
自定义视图代码:
//// HJProgressView.m// 画进度条//// Created by HJiang on 15/1/2.// Copyright (c) 2015年 HJiang. All rights reserved.//#import "HJProgressView.h"@interface HJProgressView()// 定义属性保存上次的弧度起始//@property (nonatomic,assign) CGFloat endAngle;@end@implementation HJProgressView- (void)setProgressValue:(float)progressValue{ _progressValue = http://www.mamicode.com/progressValue;"%.02f",self.progressValue]; // 输出进度百分比数字 NSMutableString *progressText = [NSMutableString string]; [progressText appendFormat:@"%.01f",self.progressValue * 100]; [progressText appendString:@"%"]; // 控制文字字体大小和颜色属性 NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor redColor]}; [progressText drawInRect:CGRectMake(x, y, w, h) withAttributes:attrs]; CGContextRef ctx = UIGraphicsGetCurrentContext(); // 设置颜色 CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1); // 设置线条大小 CGContextSetLineWidth(ctx, 3); // 2.绘制弧形进度 CGFloat endAngle = self.progressValue * 2 * M_PI - M_PI_4; CGContextAddArc(ctx, 30, 30, 25,-M_PI_4, endAngle, 0); CGContextStrokePath(ctx); }@end
iOS Quartz2D模拟下载进度条
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。