前言
1.
UIBezierPath: UIBezierPath是在 UIKit 中的一個類,繼承于NSObject,可以創(chuàng)建基于矢量的路徑.此類是Core Graphics框架關(guān)于path的一個OC封裝。使用此類可以定義常見的圓形灯蝴、多邊形等形狀 置侍。我們使用直線愉烙、灰榇俊(arc)來創(chuàng)建復(fù)雜的曲線形狀晚缩。每一個直線段或者曲線段的結(jié)束的地方是下一個的開始的地方笼才。每一個連接的直線或者曲線段的集合成為subpath漱受。一個UIBezierPath對象定義一個完整的路徑包括一個或者多個subpaths。
2.
CAShapeLayer: 繼承于CALayer骡送。 每個CAShapeLayer對象都代表著將要被渲染到屏幕上的一個任意的形狀(shape)昂羡。具體的形狀由其path(類型為CGPathRef)屬性指定。 普通的CALayer是矩形摔踱,所以需要frame屬性虐先。CAShapeLayer它本身沒有形狀,它的形狀來源于其屬性path 派敷。CAShapeLayer有不同于CALayer的屬性蛹批,它從CALayer繼承而來的屬性在繪制時是不起作用的。
具體用法看代碼
僅僅使用UIBezierPath來繪圖的話篮愉,需要在view的drawRect方法里實現(xiàn),詳細(xì)可以看MyView的drawRect方法
//
// MyView.m
// CAShapeLayerAndUIBezierPath
//
// Created by 蔡欣東 on 16/4/21.
// Copyright ? 2016年 蔡欣東. All rights reserved.
//
#import "MyView.h"
@implementation MyView
- (void)drawRect:(CGRect)rect {
[self simpleDraw];
[self drawARCPath];
[self drawTrianglePath];
[self drawSecondBezierPath];
}
//畫圓角矩形
-(void)simpleDraw{
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 100, 100) cornerRadius:20];
path.lineWidth = 5;
//設(shè)置填充顏色
UIColor* fillColor = [UIColor greenColor];
[fillColor set];
[path fill];
//設(shè)置線的顏色般眉,需要在填充顏色之后設(shè)置
UIColor* lineColor = [UIColor redColor];
[lineColor set];
[path stroke];
}
//畫圓弧
-(void)drawARCPath{
UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 150) radius:100 startAngle:0 endAngle:M_PI*90/180 clockwise:YES];
//連接處的樣式
path.lineCapStyle = kCGLineCapRound;
//連接方式
path.lineJoinStyle = kCGLineJoinRound;
path.lineWidth = 5;
UIColor* lineColor = [UIColor redColor];
[lineColor set];
[path stroke];
}
//畫三角形
-(void)drawTrianglePath{
UIBezierPath* path = [UIBezierPath bezierPath];
//設(shè)置起點(diǎn)
[path moveToPoint:CGPointMake(20, 300)];
[path addLineToPoint:CGPointMake(150, 400)];
[path addLineToPoint:CGPointMake(20, 400)];
[path closePath];
path.lineWidth = 5;
UIColor* fillColor = [UIColor greenColor];
[fillColor set];
[path fill];
UIColor* lineColor = [UIColor redColor];
[lineColor set];
[path stroke];
}
//畫二次貝爾曲線
-(void)drawSecondBezierPath{
UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(200, 150)];
[path addQuadCurveToPoint:CGPointMake(200, 300) controlPoint:CGPointMake(50, 50)];
path.lineWidth = 5;
UIColor* lineColor = [UIColor redColor];
[lineColor set];
[path stroke];
}
@end
用CAShapeLayer和UIBezierPath畫圖,以及結(jié)合CAAnimation實現(xiàn)一個繪圖動畫
動畫效果:
//
// ViewController.m
// CAShapeLayerAndUIBezierPath
//
// Created by 蔡欣東 on 16/4/21.
// Copyright ? 2016年 蔡欣東. All rights reserved.
//
#import "ViewController.h"
#import "MyView.h"
@interface ViewController (){
CAShapeLayer *layer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//僅僅使用UIBezierPath的話潜支,需要在view的drawRect方法里實現(xiàn),詳細(xì)可以看MyView的drawRect方法
// CGFloat W = [UIScreen mainScreen].bounds.size.width;
// CGFloat H = [UIScreen mainScreen].bounds.size.height;
// MyView* myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, W, H)];
// myView.backgroundColor = [UIColor whiteColor];
// [self.view addSubview:myView];
//CAShapeLayer和UIBezierPath畫圖
layer = [CAShapeLayer layer];
layer.fillColor = [UIColor clearColor].CGColor;
layer.lineWidth = 20.0f;
layer.lineCap = kCALineCapRound;
layer.lineJoin = kCALineJoinRound;
layer.strokeColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:layer];
// 創(chuàng)建貝塞爾路徑
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:80 startAngle:0 endAngle:M_PI*2 clockwise:NO];
// 關(guān)聯(lián)layer和貝塞爾路徑
layer.path = path.CGPath;
// 創(chuàng)建Animation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @(0.0);
animation.toValue = @(1.0);
layer.autoreverses = NO;
animation.duration = 3.0;
// 設(shè)置layer的animation
[layer addAnimation:animation forKey:nil];
animation.delegate = self;
int count = 16;
for (int i = 0; i<count; i++) {
CAShapeLayer* lineLayer = [CAShapeLayer layer];
lineLayer.fillColor = [UIColor clearColor].CGColor;
lineLayer.strokeColor = [UIColor yellowColor].CGColor;
lineLayer.lineWidth = 15.0f;
lineLayer.lineCap = kCALineCapRound;
lineLayer.lineJoin = kCALineCapRound;
[self.view.layer addSublayer:lineLayer];
UIBezierPath* path2 = [UIBezierPath bezierPath];
int x = 200+100*cos(2*M_PI/count*i);
int y = 200-100*sin(2*M_PI/count*i);
int len = 50;
[path2 moveToPoint:CGPointMake(x, y)];
[path2 addLineToPoint:CGPointMake(x+len*cos(2*M_PI/count*i), y-len*sin(2*M_PI/count*i))];
lineLayer.path = path2.CGPath;
[lineLayer addAnimation:animation forKey:nil];
}
}
//animation結(jié)束回調(diào)
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"yes");
layer.fillColor = [UIColor redColor].CGColor;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end