標籤:自訂 源碼 下載 ui 效果
貓貓分享,必須精品
素材代碼地址:http://download.csdn.net/detail/u013357243/8640353
原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents
效果
代碼NYProgressView.m
//// NYProgressView.m// 下載進度條//// Created by apple on 15-4-27.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYProgressView.h"@interface NYProgressView()@property (nonatomic,weak) UILabel *label;@end@implementation NYProgressView/**label的懶載入*/-(UILabel *)label{ if (_label == nil) { UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; label.textAlignment = NSTextAlignmentCenter; [self addSubview:label]; _label = label; } return _label;}-(void)setProgress:(CGFloat)progress{ _progress = progress; self.label.text = [NSString stringWithFormat:@"%.2f%%", progress*100]; //重新繪製 在view上做一個重繪標記,當下次螢幕重新整理的時候,調用drawRect。 [self setNeedsDisplay];}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}/**畫畫 當視圖顯示的時候,預設只調用一次 解決辦法://重新繪製 在view上做一個重繪標記,當下次螢幕重新整理的時候,調用drawRect。 [self setNeedsDisplay]; */- (void)drawRect:(CGRect)rect{ // 1:擷取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2:拼接路徑 /*我們需要畫一個圓圖*/ CGPoint center = CGPointMake(50, 50);//圓心 CGFloat radius = 43;//半徑 CGFloat startA = -M_PI_2 ;//起始角度 CGFloat endA = -M_PI_2 + _progress * M_PI * 2 ;//結束角度。 UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; //clockwise 順時針方向。 //3:把路徑添加到上下文。 CGContextAddPath(ctx, path.CGPath); //設定顏色為紅色 [[UIColor redColor] set]; //設定線條的寬度 CGContextSetLineWidth(ctx, 10); //設定兩端的樣式為圓角 CGContextSetLineCap(ctx,kCGLineCapRound); //4:把上下文渲染到視圖。 CGContextStrokePath(ctx);}@end
NYViewController.m
//// NYViewController.m// 下載進度條//// Created by apple on 15-4-27.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYViewController.h"#import "NYProgressView.h"@interface NYViewController ()@property (weak, nonatomic) IBOutlet NYProgressView *progressView;@end@implementation NYViewController/**滑動slider發生的事件*/- (IBAction)valueChange:(UISlider *)sender { NSLog(@"%f",sender.value); _progressView.progress = sender.value;}- (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.}@end
(素材源碼)貓貓學IOS(二十九)UI之Quartz2D自訂下載控制項