標籤:
一:效果
二:代碼
#import "ViewController.h"#import "ProgressView.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *valueTitle;@property (weak, nonatomic) IBOutlet ProgressView *progressView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)valueChange:(UISlider *)sender { //擷取進度值 NSLog(@"%f",sender.value); //%在stringWithFormat有特殊的含義,不能直接使用,如果想要使用用兩個%代表一個% self.valueTitle.text = [NSString stringWithFormat:@"%.2f%%",sender.value * 100]; self.progressView.progressValue = sender.value; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
1.搭建介面.
2.拖動滑竿的時候讓他裡面的能夠跟著我的拖動,數字在改變.
數字改變時有一個注意點, 就是要顯示%,它是一個特殊的符號,要用兩個%%代表一個%
3.拖動滑竿的時候就是在上面畫弧.
從最上面,按順時針畫,所以,它的起始角度是-90度.結束角度也是-90度
也是從起始角度開始畫,
起始角度-90度, 看你下載進度是多少
假如說你下載進度是100,就是1 * 360度
也就是說這個進度占你360度多少分之一
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPoint center = CGPointMake(50, 50);
CGFloat radius = rect.size.width * 0.5;
CGFloat startA = -M_PI_2;
CGFloat endA = -M_PI_2 + M_PI * 2 * progress;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
radius:radius
startAngle:startA
endAngle:endA
clockwise:YES];
要獲得Progress的值,這個進度值沒有, 所以要傳進來才能畫.弄一個成員變數
要在值改變的時候就要傳進來.
要拿到ProgressView才能夠傳進來,所以要拖線,拿到ProgressView
所有都做好的, 發現沒有畫圓孤?
為什麼?
問題:drawRect方法總共調用多少次?
總共就調用一次, 第一次Progress為0,以後都不會執行了
解決:每次傳的時候,就要畫一次,
重寫Progress方法
-(void)setProgress:(CGFloat)progress{
_progress = progress;
手動調用drawRect方法, 讓它重新繪製
[self drawRect:self.bounds];
}
運行發現還是不畫,為什麼?
原因:drawRect方法是不能手動調用,因為在drawRect方法中才能擷取跟View相關聯的上下文
系統在調用DrawRect方法時,會自動幫你建立一個跟View相關聯的上下文,並且傳遞給它.
自己調用的,沒有給drawRect方法傳遞上下文.所以在draw方法中拿不到上下文.
解決辦法:想要重繪,調用[self setNeedsDisplay];
告訴系統重新繪製View,系統就會自動幫你調用drawRect方法,系統在調用
drawRect方法,它會幫你建立上下文
iOS開發Quartz2D 三 進度條的應用