码迷,mamicode.com
首页 > 其他好文 > 详细

自定义进度条

时间:2016-03-30 19:32:48      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

//
//  ViewController.m
//  自定义进度条
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "ViewController.h"
#import "ProgressViewCustom.h"

@interface ViewController ()
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) ProgressViewCustom *progressViewCustom;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];

    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.slider];
    
    
    self.progressViewCustom = [[ProgressViewCustom alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _progressViewCustom.center = self.view.center;
    _progressViewCustom.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_progressViewCustom];
}
- (void)sliderAction:(UISlider *)sender{
    NSLog(@"%lf",sender.value);
    _progressViewCustom.progress = sender.value;
    
}

@end
//
//  ProgressViewCustom.m
//  自定义进度条
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "ProgressViewCustom.h"

@implementation ProgressViewCustom

- (void)drawRect:(CGRect)rect {
    //半径
    CGFloat radius = rect.size.width / 2;
    //中心点
    CGPoint center = CGPointMake(radius, radius);
    //结束点. - 90 + (正在转的点slider的value*360)
    CGFloat endA = -M_PI_2 + _progress * M_PI * 2;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 5 startAngle:-M_PI_2 endAngle:endA clockwise:YES];
    [path addLineToPoint:center];
    [path closePath];
    [path fill];
//    [path stroke];
}

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    //重绘 先调用此View的layer相关的上下文, 再调用drawRect方法
    [self setNeedsDisplay];
}


@end

 

自定义进度条

标签:

原文地址:http://www.cnblogs.com/HaiTeng/p/5338435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!