美文网首页
绘图的实现

绘图的实现

作者: cj小牛 | 来源:发表于2017-08-08 20:10 被阅读7次

import "testView.h"

@interface testView ()

@property(nonatomic,strong)CAShapeLayer * trackeLayer;
@property(nonatomic,strong)UIBezierPath * trackPath;
@property(nonatomic,strong)CAShapeLayer * progressLayer;
@property(nonatomic,strong)UIBezierPath * progressPath;

@property(nonatomic,strong)CAGradientLayer * gradientLayer;
@end

@implementation testView

-(instancetype)initWithFrame:(CGRect)frame{

if (self = [super initWithFrame:frame]) {
    [self setUpTrackeLayer];
    [self setUpProgressLayer];
    self.progressWidth = 5;
}
return  self;

}

-(void)setUpTrackeLayer{
_trackeLayer = [CAShapeLayer layer];
[self.layer addSublayer:_trackeLayer];
_trackeLayer.fillColor = nil;
_trackeLayer.frame = self.bounds;

}

-(void)setUpProgressLayer{
//创建
_progressLayer = [CAShapeLayer layer];
// 添加
[self.layer addSublayer:_progressLayer];

//设置填充色
_progressLayer.fillColor = [UIColor clearColor].CGColor;
// 设置尺寸
_progressLayer.frame = self.bounds;

// 设置开始的位置
_progressLayer.strokeStart = 0;
// 设置结束的位置
_progressLayer.strokeEnd = 0.6;

}

-(void)setTrack{

_trackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];


_trackeLayer.path = _trackPath.CGPath;

}

-(void)SetprogressPath{

// 这种贝塞尔曲线的patch 是可以改变的。可以多🏠思考实现掐的
_progressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:- M_PI_2 endAngle:(M_PI * 2) * _progress - M_PI_2 clockwise:YES];
//.创建贝塞尔曲线
_progressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:-M_PI/2 endAngle:M_PI*1.5 clockwise:YES];

// 给进度的layer确定范围
_progressLayer.path = _progressPath.CGPath;

}

-(void)setProgressColor:(UIColor *)progressColor{

_progressLayer.strokeColor = progressColor.CGColor;

}

  • (void)setTrackColor:(UIColor *)trackColor
    {
    _trackeLayer.strokeColor = trackColor.CGColor;
    }

-(void)setProgress:(float)progress{

_progress = progress;
[self SetprogressPath];
[self set_gradientLayer];

}

-(void)setProgressWidth:(float)progressWidth{

_progressWidth = progressWidth;
_trackeLayer.lineWidth = _progressWidth;
_progressLayer.lineWidth = _progressWidth;
[self setTrack];
[self SetprogressPath];

}

//给进度的layer 设置渐变色
-(void)set_gradientLayer{

_gradientLayer = [CAGradientLayer layer];

[_gradientLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor orangeColor] CGColor],(id)[UIColor greenColor].CGColor, nil]];

_gradientLayer.frame = self.bounds;
_gradientLayer.startPoint = CGPointMake(0, 0);
_gradientLayer.endPoint = CGPointMake(0, 1);
[_gradientLayer setMask:self.progressLayer];
[self.layer addSublayer:_gradientLayer];}

相关文章

网友评论

      本文标题: 绘图的实现

      本文链接:https://www.haomeiwen.com/subject/xxjxrxtx.html