iOS指南系列:关于进度条的问题

项目主要是演示如何使用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);
}


//如果是下载,需要在没n个接受数据结束后,更新一下百分比?在示范程序中,用NStimer来简化这个过程,定时调用inc函数,increase number:

  //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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值