进度条(贝塞尔曲线)

鉴于之前那篇关于进度条的文章有点繁琐,利用了扇形和遮盖,比较麻烦,现在以另一种方法-贝塞尔曲线画进度条

直接代码

#import <UIKit/UIKit.h>

@interface test : UIView
@property(nonatomic, assign)float progressRate;
@property(nonatomic, strong)UIColor *color;
@property(nonatomic, assign)float x;//cetner x
@property(nonatomic, assign)float y;//center y
@property(nonatomic, assign)float radius;//半径
@property(nonatomic, assign)float startAngle;//起始位置

-(void)animateProgress:(float)progress animate:(BOOL)animate;

@end
#import "test.h"
#import <QuartzCore/QuartzCore.h>

@interface test()
@property(nonatomic,assign) float step;
@property(nonatomic,assign) float total;
@end
@implementation test


-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGPoint point = CGPointMake(_x, _y);//中心位置
    CGFloat endAngle = self.startAngle+2*M_PI*self.progressRate;
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:point radius:self.radius startAngle:self.startAngle endAngle:endAngle clockwise:YES];
    CGContextSetLineCap(context, kCGLineCapRound);

    CGContextSetLineWidth(context, 10.0);
    CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor);
    CGContextAddPath(context, bezierPath.CGPath);
    CGContextStrokePath(context);//渲染


}

-(void)animateProgress:(float)progress animate:(BOOL)animate
{
//    _progressRate = progress;
    if (animate) {
        self.total = progress;
        /*
         利用定时器做进度动画
         */
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                                  target:self
                                                  selector:@selector(numberAnimation:)
                                                userInfo:nil
                                                 repeats:YES];
    }else{
        [self setNeedsDisplay];
    }
}

-(void)numberAnimation:(NSTimer*)timer
{
    _step += 0.01;
    if (self.step > self.total) {
        [timer invalidate];
        timer = nil;
        self.step = 0;
        return;
    }
    self.progressRate = self.step;
    [self setNeedsDisplay];

}

@end

调用

#import "ViewController.h"
#import "test.h"
@interface ViewController ()
@property(nonatomic,strong) test *progressView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _progressView = [[test alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
    self.progressView.x = 50;
    self.progressView.y = 50;
    self.progressView.radius = 40;
    //    self.progressView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    self.progressView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.progressView];
}

- (IBAction)tapProgress:(id)sender {

//    self.progressView.progressRate = 0.8;
    [self.progressView animateProgress:0.8 animate:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值