UIBerzierPath类可以创建矢量路径,此类为Core
Grapice框架关于path的一个封装
- 创建一个path对象
- 使用方法moveToPoint:设置初始线段的起点
- 添加line或者curve定义一个或者多个subpaths
- 添加UIBezierPath对象绘图的属性
简单代码如下:
画圆弧
立方和二次贝塞尔曲线
例子:画一个圆圈,并且动画围绕圆圈绕一圈
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
{
CAShapeLayer *arcLayer;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self intiUIOfView];
self.view.backgroundColor = [UIColor grayColor];
NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(dosometing:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)dosometing:(id)sender
{
NSLog(@"dosometing");
arcLayer.strokeColor = nil;
[self drawLineAnimation:arcLayer];
arcLayer.strokeColor=[UIColor colorWithWhite:1 alpha:0.7].CGColor;
}
-(void)intiUIOfView
{
UIBezierPath *path=[UIBezierPath bezierPath];
CGRect rect=[UIScreen mainScreen].applicationFrame;
NSLog(@"width: %f ,height: %f",CGRectGetWidth(rect),CGRectGetHeight(rect));
[path addArcWithCenter:CGPointMake(100,100) radius:100 startAngle:0 endAngle:2*M_PI clockwise:YES];
arcLayer=[CAShapeLayer layer];
arcLayer.path=path.CGPath;
arcLayer.fillColor=[UIColor redColor].CGColor;
arcLayer.strokeColor=[UIColor colorWithWhite:1 alpha:0.7].CGColor;
arcLayer.lineWidth=3;
arcLayer.frame=self.view.frame;
[self.view.layer addSublayer:arcLayer];
[self drawLineAnimation:arcLayer];
}
-(void)drawLineAnimation:(CALayer*)layer
{
CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
bas.duration=3;
bas.delegate=self;
bas.fromValue=[NSNumber numberWithInteger:0];
bas.toValue=[NSNumber numberWithInteger:1];
[layer addAnimation:bas forKey:@"key"];
}
@end