项目主要是演示如何使用progresview以及适当自定义:
先看自定义得怎么做的?我们看到画图中唯一和value有关系得就是计算填充颜色得宽度,这个就是进度得一个表示,如果要做垂直得进度条,那么就选用高度*value
@interface MyProgressView : UIView
@synthesize value; //进度表示的值
- (void)drawRect:(CGRect)rect { //看到这个方法得明白要调用 setNeedsDisplay 来更新view
CGContextRef c = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] set];
CGFloat ins = 1.0;//2.0; the large of ins cause small hight
CGRect r = CGRectInset(self.bounds, ins, ins); //the default width/height is large...determined in NIB
CGFloat radius = r.size.height / 2.0;
CGMutablePathRef path = CGPathCreateMutable(); //cgmutablePathRef
CGPathMoveToPoint(path, NULL, CGRectGetMaxX(r) - radius, ins); //拿到要画圆弧得点
CGPathAddArc(path, NULL,
radius+ins, radius+ins, radius, -M_PI/2.0, M_PI/2.0, true); //180度
CGPathAddArc(path, NULL,
CGRectGetMaxX(r) - radius, radius+ins, radius, M_PI/2.0, -M_PI/2.0, true);
CGPathCloseSubpath(path);
CGContextAddPath(c, path); //把path画到C
CGContextSetLineWidth(c, 2);
CGContextStrokePath(c);
CGContextAddPath(c, path);
CGContextClip(c);//按path裁图,由矩形变成两边是圆弧得图形
CGContextFillRect(c, CGRectMake(
r.origin.x, r.origin.y, r.size.width * self.value, r.size.height));
//
CFRelease(path);
}
//scheduled with NSTimer....
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(inc:) userInfo:nil repeats:YES];
当inc中发现进度结束,那么停止timer通过invalidate
[t invalidate];
想象一下,我们在inc中要做什么,首先是更新value,就是把progress bar的当前值更新:(这里用的是先获取当前的,然后plus一个增量)-(void)inc:(NSTimer*)t {
CGFloat val = prog.value;
val += 0.05;
prog.value = val;
//use default one...
prog2.progress = val;
prog3.progress = val;
//this call will invoke the method drawrectangle:- (void)drawRect:(CGRect)rect
[prog setNeedsDisplay]; //对于自定义的progresview是,必要的调用
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//scheduled with NSTimer....
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(inc:) userInfo:nil repeats:YES];
okay,总结一下:
1. viewdidappear,通过设置nstimer
2. 在nstimer的tick中更新progressview的value
3. 如果是默认的progressview,会自动render新内容,这个应该是通过setter来做的,在setter里面调用了setneedsdisplay;如果是自定义样式的,那么就需要自己实现这个东东/感觉不应该放在VC里面,也应该使用setter来实现/
4. setneedsdisplay调用了drawrectangle,所以要实现这个函数。
等一下:如果纯粹使用默认的,你看到的进度那个是白色的,如果要这这些方面做文章:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(9,9), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(con, [UIColor blackColor].CGColor);
CGContextMoveToPoint(con, 0, 4.5);
CGContextAddLineToPoint(con, 4.5, 9);
CGContextAddLineToPoint(con, 9, 4.5);
CGContextAddLineToPoint(con, 4.5, 0);
CGContextClosePath(con);
CGPathRef p = CGContextCopyPath(con);
CGContextFillPath(con);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
//
CGContextSetFillColorWithColor(con, [UIColor greenColor].CGColor);
CGContextAddPath(con, p);
CGContextFillPath(con);
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
CGPathRelease(p);
UIGraphicsEndImageContext();
//Creates and returns a new image object with the specified cap insets.
im = [im resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
im2 = [im2 resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
prog3.trackImage = im;
prog3.progressImage = im2;
通过设置以下两个属性,就完成了自定义的东西。比如这里的绿色的条
prog3.trackImage = im;
prog3.progressImage = im2;
hint:ios5 samples