自己学习CALayer笔记第三章

  CAShapeLayer的使用
下面画一个简单的火柴人

self.containerView = [[UIView allocinitWithFrame:self.view.bounds];

    [self.view addSubview:self.containerView];

    

    UIBezierPath *path = [[UIBezierPath allocinit];

    [path moveToPoint:CGPointMake(175100)];

    [path addArcWithCenter:CGPointMake(150100radius:25 startAngle:0 endAngle:2*M_PI clockwise:YES];

    [path moveToPoint:CGPointMake(150125)];

    [path addLineToPoint:CGPointMake(150175)];

    [path addLineToPoint:CGPointMake(125225)];

    [path moveToPoint:CGPointMake(150175)];

    [path addLineToPoint:CGPointMake(175225)];

    [path moveToPoint:CGPointMake(100150)];

    [path addLineToPoint:CGPointMake(200150)];

    

    //create shape layer

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    shapeLayer.strokeColor = [UIColor redColor].CGColor;

    shapeLayer.fillColor = [UIColor clearColor].CGColor;

    shapeLayer.lineWidth = 5;

    shapeLayer.lineJoin = kCALineJoinRound;

    shapeLayer.lineCap = kCALineCapRound;

    shapeLayer.path = path.CGPath;

     [ self . containerView . layer   addSublayer :shapeLayer];  
下面一个简单三个角是圆角,一个角是直角的矩形

CGRect rect = CGRectMake(505015050);

    CGSize radii = CGSizeMake(88);

    UIRectCorner corners = UIRectCornerTopRight | UIRectCornerTopLeft;

    path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];

    shapeLayer.path = path.CGPath;

     [ self . containerView . layer   addSublayer :shapeLayer];
textLayer的使用
下面是一个简单的例子

self.lableView = [[UIView allocinitWithFrame:CGRectMake(1010030030)];

    [self.view addSubview:self.lableView];

    

    CATextLayer *textLayer = [CATextLayer layer];

    textLayer.frame = self.lableView.bounds;

    [self.lableView.layer addSublayer:textLayer];

    

    textLayer.foregroundColor = [UIColor blackColor].CGColor;

    textLayer.alignmentMode = kCAAlignmentJustified;

    //设置contentsScale来匹配屏幕

    textLayer.contentsScale = [UIScreen mainScreen].scale;

    textLayer.wrapped = YES;

    

    UIFont *font = [UIFont systemFontOfSize:15];

    

    CFStringRef fontName = (__bridge CFStringRef)font.fontName;

    CGFontRef fontRef = CGFontCreateWithFontName(fontName);

    textLayer.font = fontRef;

    textLayer.fontSize = font.pointSize;

    CGFontRelease(fontRef);

    NSString *text = @"Lorem ipsum dolor sit amet, consectrtur adipiscin";

    textLayer.string = text;

  CATransformLayer的基本使用
下面是一个绘制不同立方体的例子

-(CALayer *)faceWithTransform:(CATransform3D)transform

{

    CALayer *face = [CALayer layer];

    face.frame = CGRectMake(-50, -50100100);

    

    CGFloat red = (rand()/(double)INT_MAX);

    CGFloat green = (rand()/(double)INT_MAX);

    CGFloat blue = (rand()/(double)INT_MAX);

    face.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;

    face.transform = transform;

    return face;

}


-(CALayer *)cubeWithTransform:(CATransform3D)transform

{

    CATransformLayer *cube = [CATransformLayer layer];

    CATransform3D ct = CATransform3DMakeTranslation(0050);

    [cube addSublayer:[self faceWithTransform:ct]];

    ct = CATransform3DMakeTranslation(5000);

    ct = CATransform3DRotate(ct, M_PI_20100);

    [cube addSublayer:[self faceWithTransform:ct]];

    ct = CATransform3DMakeTranslation(0, -500);

    ct = CATransform3DRotate(ct, M_PI_2100);

    [cube addSublayer:[self faceWithTransform:ct]];

    ct = CATransform3DMakeTranslation(0500);

    ct=  CATransform3DRotate(ct, -M_PI_2100);

    [cube addSublayer:[self faceWithTransform:ct]];

    ct = CATransform3DMakeTranslation(-5000);

    ct = CATransform3DRotate(ct, -M_PI_2010);

    [cube addSublayer:[self faceWithTransform:ct]];

    ct = CATransform3DMakeTranslation(00, -50);

    ct = CATransform3DRotate(ct, M_PI010);

    [cube addSublayer:[self faceWithTransform:ct]];

    

    CGSize containerSize = self.containerView.bounds.size;

    cube.position = CGPointMake(containerSize.width/2.0, containerSize.height/2.0);

    cube.transform = transform;

    return cube;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.containerView = [[UIView allocinitWithFrame:self.view.bounds];

    [self.view addSubview:self.containerView];

    

    CATransform3D pt = CATransform3DIdentity;

    pt.m34 = -1.0/500.0;

    self.containerView.layer.sublayerTransform = pt;

    CATransform3D c1t = CATransform3DIdentity;

    c1t = CATransform3DTranslate(c1t, -10000);

    CALayer *cube1 = [self cubeWithTransform:c1t];

    [self.containerView.layer addSublayer:cube1];

    

    CATransform3D c2t = CATransform3DIdentity;

    c2t = CATransform3DTranslate(c2t, 10000);

    c2t = CATransform3DRotate(c2t, -M_PI_4100);

    c2t = CATransform3DRotate(c2t, -M_PI_4010);

    CALayer *cube2 = [self cubeWithTransform:c2t];

    [self.containerView.layer addSublayer:cube2];

}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值