Demo
ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.//CGContext —> view drawRect
//frame —> 不能为0
//[self.view setNeedsDisplay];
//UIBezierPath —> layer CAShapeLayerself.testView = [[TQView alloc] initWithFrame:self.view.bounds];
self.testView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.testView];
}
-(void)touchesBegan:(NSSet UITouch > )touches withEvent:(UIEvent *)event
{
// CGMutablePathRef path = CGPathCreateMutable();
// //const CGAffineTransform * _Nullable m
// //CGPathAddRect(path, NULL, CGRectMake(100, 100, 100, 100));
//
// CGPathAddArc(path, nil, 200, 200, 100, 0, 2 * M_PI , 0);
// CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@”position”];
//
// keyFrame.duration = 2.0f;
//
// keyFrame.repeatCount = MAXFLOAT;
//
// keyFrame.path = path;
//
// [self.testView.layer addAnimation:keyFrame forKey:@”“];
}
TQView.m
@interface TQView ()
@property (nonatomic) CGContextRef context;
@end
@implementation TQView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(void)drawImage
{
UIImage *image = [UIImage imageNamed:@”1”];
[image drawAtPoint:CGPointMake(100, 100)];
}
-(void)drawText
{
NSString *string = @”hello”;
[string drawAtPoint:CGPointMake(100, 100) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f]}];
}
-(void)drawEllisp
{
CGContextAddEllipseInRect(self.context, CGRectMake(100, 100, 100, 100));
CGContextStrokePath(self.context);
}
-(void)drawCircle
{
CGContextAddArc(self.context, 100, 100, 50, 0, M_PI * 2, 0);
//CGContextClosePath(self.context);
CGContextStrokePath(self.context);
}
-(void)drawRect
{
CGContextAddRect(self.context, CGRectMake(100, 100, 100, 100));
CGContextStrokePath(self.context);
}
-(void)drawLine
{
//设置画线的起点
CGContextMoveToPoint(self.context, 100, 100);
if 1
CGFloat array[] = {10,5};
//设置虚线
CGContextSetLineDash(self.context, 5, array, 2);
//设置线的终点
CGContextAddLineToPoint(self.context, 200, 200);
//CGContextAddLineToPoint(self.context, 150, 500);
//CGContextClosePath(self.context);
CGContextStrokePath(self.context);
endif
if 0
//CGContextFillPath(<#CGContextRef _Nullable c#>)
CGPoint array[] = {CGPointMake(200, 200),CGPointMake(300, 200),CGPointMake(150, 500)};
CGContextAddLines(self.context, array, sizeof(array)/ sizeof(array[0]));
//开始划线
CGContextStrokePath(self.context);
endif
}
-(void)drawBea
{
CGContextMoveToPoint(self.context, 20, 400);//移动到起始位置
/*绘制二次贝塞尔曲线
c:图形上下文
cpx:控制点x坐标
cpy:控制点y坐标
x:结束点x坐标
y:结束点y坐标
*/
CGContextAddQuadCurveToPoint(self.context, 160, -100, 300, 400);
CGContextMoveToPoint(self.context, 20, 500);
/*绘制三次贝塞尔曲线
c:图形上下文
cp1x:第一个控制点x坐标
cp1y:第一个控制点y坐标
cp2x:第二个控制点x坐标
cp2y:第二个控制点y坐标
x:结束点x坐标
y:结束点y坐标
*/
CGContextAddCurveToPoint(self.context, 80, 300, 240, 500, 300, 300);
//设置图形上下文属性
[[UIColor yellowColor]setFill];
[[UIColor redColor]setStroke];
//绘制路径
CGContextDrawPath(self.context, kCGPathFillStroke);
}
(void)drawRect:(CGRect)rect {
// Drawing code
self.context = UIGraphicsGetCurrentContext();//设置线的宽度
CGContextSetLineWidth(self.context, 5);//设置线的颜色
[[UIColor redColor] setStroke];
//CGContextStrokePath(self.context);//[[UIColor blueColor] setFill];
//CGContextFillPath(<#CGContextRef _Nullable c#>)[self drawBea];
}
UIBezierPath环形进度条
ViewController
@implementation ViewController
{
CircleProgress *progressView;
}
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.progressView = [[CircleProgress alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
progressView.backgroundColor = [UIColor lightGrayColor];
//progressView.progress = 0.5;
[self.view addSubview:progressView];
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
slider.minimumValue = 0;
slider.maximumValue = 1;[slider addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
if 0
CAShapeLayer *shapLayer = [[CAShapeLayer alloc] init];
shapLayer.frame = self.view.bounds;
//设置线的颜色 和 宽度
shapLayer.lineWidth = 5;
shapLayer.strokeColor = [UIColor blueColor].CGColor;
//设置填充的颜色
shapLayer.fillColor = [UIColor purpleColor].CGColor;
[self.view.layer addSublayer:shapLayer];
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
// [bezierPath moveToPoint:CGPointMake(100, 100)];
//
// [bezierPath addLineToPoint:CGPointMake(200, 200)];
[bezierPath addArcWithCenter:CGPointMake(100, 100) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
//绘制图形
shapLayer.path = bezierPath.CGPath;
endif
}
-(void)click:(UISlider *)slider
{
progressView.progress = slider.value;
}
@property (nonatomic,strong) CAShapeLayer *backLayer;
@property (nonatomic,strong) CAShapeLayer *foreLayer;
@end
CircleProgress.m
@implementation CircleProgress
(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backLayer = [[CAShapeLayer alloc] init];
self.backLayer.frame = self.bounds;
self.backLayer.lineWidth = 10;
self.backLayer.strokeColor = [UIColor blackColor].CGColor;self.backLayer.fillColor = [UIColor clearColor].CGColor; UIBezierPath *backPath = [UIBezierPath bezierPath]; [backPath addArcWithCenter:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0f) radius:self.bounds.size.width / 2.0f - 10 / 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES]; self.backLayer.path = backPath.CGPath; [self.layer addSublayer:self.backLayer]; self.foreLayer = [[CAShapeLayer alloc] init]; self.foreLayer.frame = self.bounds; self.foreLayer.lineWidth = 10; self.foreLayer.strokeColor = [UIColor orangeColor].CGColor; self.foreLayer.fillColor = [UIColor clearColor].CGColor; // UIBezierPath *forePath = [UIBezierPath bezierPath]; [forePath addArcWithCenter:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0f) radius:self.bounds.size.width / 2.0f - 10 / 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES]; self.foreLayer.path = forePath.CGPath; CABasicAnimation *ann = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; ann.duration = 2.0f; ann.fromValue = @(0); ann.toValue = @(1); [self.foreLayer addAnimation:ann forKey:@""]; [self.layer addSublayer:self.foreLayer];
}
return self;
}
//0 - 1
-(void)setProgress:(CGFloat)progress
{
UIBezierPath *forePath = [UIBezierPath bezierPath];
[forePath addArcWithCenter:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0f) radius:self.bounds.size.width / 2.0f - 10 / 2 startAngle:0 endAngle:M_PI * 2 * progress clockwise:YES];
self.foreLayer.path = forePath.CGPath;
}