iOS Core Animation学习总结(1)--CALayer常用属性

图层是core animation的基础, UIView之所以能显示在屏幕上,靠的是其内部的这个图层,即每个UIView 都有 CALayer,可通过UIView.layer或者[UIView layer]访问到这个图层。UIView可以响应事件,而CALayer只负责显示。
 
一. 基本属性设置
    
1. 设置阴影和透明度
    //创建image view
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"curry.jpg"]];
    imgView.frame = CGRectMake(100, 100, 200, 150);
    [self.view addSubview:imgView];
    
    //设置阴影的颜色为灰色
    imgView.layer.shadowColor = [UIColor grayColor].CGColor;
    //阴影的偏移大小,右下角偏移
    imgView.layer.shadowOffset = CGSizeMake(10, 10);
    //0.5表示半透明
    imgView.layer.shadowOpacity = 0.5;
效果:
2.设置圆角效果
    //设置圆角半径为10
    imgView.layer.cornerRadius = 10;
    imgView.layer.masksToBounds = YES;
效果:

3.设置边框

    //设置黄色边框大小为5像素
    imgView.layer.borderWidth = 5;
    imgView.layer.borderColor = [UIColor yellowColor].CGColor;

效果:

4.旋转

    //设置旋转效果
    imgView.layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 1, 1);//顺时针旋转45°

效果:

5. 缩放
    //设置缩放效果
    imgView.layer.transform = CATransform3DMakeScale(0.5, 1, 0);//x轴缩小0.5倍

  

效果:

 
 二.新建图层
 
引用QuartzCore.framework
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor blackColor].CGColor;
    layer.bounds = CGRectMake(0, 0, 100, 100);

    layer.position = CGPointMake(200, 200);
    layer.anchorPoint = CGPointMake(0.5, 0.5);
    layer.cornerRadius = 10;
    layer.masksToBounds = YES;
    layer.contents = CFBridgingRelease([UIImage imageNamed:@"curry.jpg"].CGImage);

    [self.view.layer addSublayer:layer];

  

 
 
posted @ 2015-10-16 10:33  finley0066  阅读(195)  评论(0)    收藏  举报