现在的位置: 首页 > 综合 > 正文

UIBerzierPath类 创建路径

2013年03月06日 ⁄ 综合 ⁄ 共 2150字 ⁄ 字号 评论关闭

UIBerzierPath类可以创建矢量路径,此类为Core
Grapice框架关于path的一个封装

1、Berzier Path基础
UIBezierPath对象是CGPathRef数据类型封装,一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths,包括直线和曲线。
创建步骤:
  1. 创建一个path对象
  2. 使用方法moveToPoint:设置初始线段的起点
  3. 添加line或者curve定义一个或者多个subpaths
  4. 添加UIBezierPath对象绘图的属性

简单代码如下:

    UIBezierPath *path=[UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(0, 0)];

    [path addLineToPoint:CGPointMake(60, 60)];

    [path addLineToPoint:CGPointMake(20, 20)];

    [path closePath];//关闭path,这个方法会自动连接最后点和起始点

画圆弧

 

    UIBezierPath *path=[UIBezierPath bezierPath];

    [path addArcWithCenter:CGPointMake(100, 100) radius:60 startAngle:0 endAngle:360clockwise:YES];

立方和二次贝塞尔曲线


例子:画一个圆圈,并且动画围绕圆圈绕一圈

#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

抱歉!评论已关闭.