Quartz 2D - 自定义进度控件

2016-04-21  本文已影响59人  其实朕是一只程序猿

简单自定义下载进度控件

#import "ViewController.h"
#import "GZDProgressView.h"
@interface ViewController ()

/** 进度view */
@property (weak, nonatomic) IBOutlet GZDProgressView *progressView;
/** sliderBar */
@property (weak, nonatomic) IBOutlet UISlider *sliderView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)sliderViewValueChange:(id)sender {
    self.progressView.progress = self.sliderView.value;
}
#import <UIKit/UIKit.h>
@interface GZDProgressView : UIView
@property (assign, nonatomic) CGFloat progress;

@end
#import "GZDProgressView.h"

@interface GZDProgressView ()
//进度label
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@end

@implementation GZDProgressView

//重写progress的set方法
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",progress * 100];
    //调用方法重新绘制
    [self setNeedsDisplay];
    
}

// drawRect方法画线
- (void)drawRect:(CGRect)rect {
    
    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:35 startAngle: -M_PI_2 endAngle:(self.progress * M_PI * 2) - M_PI_2 clockwise:YES];
    
    [path setLineCapStyle:kCGLineCapRound];
    
    [path setLineWidth:5];
    
    [path stroke];
}
@end

上一篇下一篇

猜你喜欢

热点阅读