首页 > 其他 > 详细

自定义进度条

时间:2016-03-30 19:32:05      阅读:367      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!