您的位置:首页 > 其它

简单的渐变进度条(drawRect)

2016-12-07 00:00 120 查看
摘要: 昨天做了一个简单的渐变的进度条,是使用自定义的view加渐变的图片实现的。
只后我就寻思学习下drawRect的方法怎么实现。

#import <UIKit/UIKit.h>

@interface ProgressView : UIView

/**
进度;
*/
@property (nonatomic, assign)CGFloat progress;

@end

#import "ProgressView.h"

@interface ProgressView ()

@end

@implementation ProgressView

- (void)setProgress:(CGFloat)progress {
_progress = progress;
[self setNeedsDisplay];
}

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.progress = 0.0;
}
return self;
}

- (void)drawRect:(CGRect)rect {
//生成渐变色
CAGradientLayer *gLayer = [CAGradientLayer layer];
gLayer.frame = rect;
gLayer.locations = @[@(self.progress*1.5-0.5), @(self.progress*1.5), @(1.5)];
gLayer.colors = @[(id)[UIColor orangeColor].CGColor, (id)[UIColor whiteColor].CGColor];
gLayer.startPoint = CGPointMake(0, 0.5);
gLayer.endPoint = CGPointMake(1, 0.5);
[self.layer addSublayer:gLayer];
}

@end

使用CAGradientLayer 实现的渐变效果非常的简单,但是可操作的控件感觉非常的有限。因此这个渐变的进度条效果差强人意。

*继续学习吧,看看有没有更好的方法;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息