IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    IOS–用Quartz2D画一个进度条

    键盘上的舞者发表于 2015-08-14 09:48:43
    love 0

    效果如图所示。

    利用Quart2D进行绘图的步骤:

    1.设置上下文

    2.设置拼接路径

    3.添加路径到上下文

    上面所实现的效果的主要代码:

    Clock类:

    //
    //  Clock.m
    //  下载进度条
    //
    //  Created by 键盘上的舞者 on 15/8/14.
    //  Copyright (c) 2015年 键盘上的舞者. All rights reserved.
    //
    
    #import "Clock.h"
    
    @interface Clock()
    @property(nonatomic,weak) UILabel *lable;
    @end
    
    @implementation Clock
    
    -(UILabel *)lable
    {
        if(_lable==nil){
            UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(50,50, 100, 100)];
            //lable.backgroundColor=[UIColor redColor];
            lable.textAlignment=NSTextAlignmentCenter;
            [self addSubview:lable];
            _lable=lable;
        }
        return _lable;
    }
    
    -(void)setProgress:(CGFloat)progress
    {
        _progress=progress;
        self.lable.text=[NSString stringWithFormat:@"%.2f%%",progress*100];
        //重绘
        //在view上做一个重绘的标记,当下次屏幕刷新的时候,就会调用drawRect方法
        [self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect{
        CGContextRef ctf=UIGraphicsGetCurrentContext();
        CGPoint center=CGPointMake(125, 125);
        CGFloat radius=100;
        CGFloat startAngle=-M_PI_2;
        CGFloat endAngle=-M_PI_2+_progress*M_PI*2;
        UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
        CGContextAddPath(ctf, path.CGPath);
        CGContextStrokePath(ctf);
    }
    
    
    @end

    控制器类:

    //
    //  ViewController.m
    //  下载进度条
    //
    //  Created by 键盘上的舞者 on 15/8/14.
    //  Copyright (c) 2015年 键盘上的舞者. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Clock.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet Clock *progress;
    
    @end
    
    @implementation ViewController
    
    - (IBAction)valueChange:(UISlider *)sender {
        _progress.progress=sender.value;
    }
    
    
    @end

     



沪ICP备19023445号-2号
友情链接