IOS--CALayer的介绍及使用技巧

  首先对CALayer进行简单的介绍:

1.在UIView中,CALayer只是一个类的声明,因此需要添加 QuartzCore框架

2.UIKit框架只能应用到ios中,但是Quartz2D是跨平台的,因此在使用颜色时应该将UIColor转换成CGColor

3.修改图层相当于修改了UIView属性,即修改了界面属性

4.形变属性既可以使用形变函数制定,也可以使用keypath制定

创建imageView并设置边框属性(基础)

1.bounds:宽度和高度,x y设置为0;

2.position:位置(默认指中心点,具体由anchorPoint决定

3.anchorPoint:锚点,(x,y的范围都是0~1),决定了position的含义

4.backgroundColor:背景颜色(CGColorRef 类型)

5.borderColor:边框颜色

6.borderWidth:边框宽度

7.borderRadius:圆角半径

8.contents:内容

   UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 90, 90, 90)];
    imgView.backgroundColor = [UIColor redColor];
    imgView.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:imgView];
    
    //1.获取layer设置边框
    imgView.layer.borderWidth = 1;
    imgView.layer.borderColor = [UIColor darkGrayColor].CGColor;
   // 2.设置弧度
    imgView.layer.cornerRadius = 45;
    imgView.layer.masksToBounds = YES;
    imgView.clipsToBounds = YES;
    
    //3.设置阴影
    //如果设置imgView.layer.masksToBounds = YES;  则不会出现阴影
    imgView.layer.shadowColor = [UIColor redColor].CGColor;
    imgView.layer.shadowOffset = CGSizeMake(5, 5);
    imgView.layer.shadowOpacity = 0.5;

    //对imgView的layer层的transform 属性进行操作(第一种方法)
        //平移
    imgView.layer.transform = CATransform3DMakeTranslation(0, 200, 0);
    //旋转
     imgView.layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 0, 0);
    //缩放
      imgView.layer.transform = CATransform3DMakeScale(2, 2, 1);
    
   //对imgView的layer层的transform 属性进行操作(第二种方法)通过kvc的方法
        
           //平移
   [imgView.layer setValue:@100 forKeyPath:@"transform.translation.x"];
    //旋转
    
    [imgView.layer setValue:@M_PI_2 forKeyPath:@"transform.rotation.z"];
    //缩放
    [imgView.layer setValue:@0.5 forKeyPath:@"transform.scale.x"];

  CALayer的基本属性

//创建图层
    CALayer *subLayer = [[CALayer alloc] init];
    //将图层添加到view的根图层上
    [self.view.layer addSublayer:subLayer];
    
    //----------常用的属性----------
    //设置图层的大小
    subLayer.bounds = CGRectMake(0, 0, 200, 200);
    //设置图层的显示位置
    subLayer.position = CGPointMake(100, 100);
    //设置背景颜色
    subLayer.backgroundColor = [UIColor redColor].CGColor;
    
    //设置锚点
    //决定了position的含义,x和y的范围是0~1
    subLayer.anchorPoint = CGPointMake(0.5, 0.5);
    
    //设置图层显示的内容
    UIImage *img = [UIImage imageNamed:@"1.jpg"];
    subLayer.contents = (id)img.CGImage;

  CALayer中的隐式动画(隐式动画的默认时间为0.25秒)

   每一个UIView内部都默认关联着一个CALayer,称这个layer为Root Layer;

所有的非Root Layer都存在着隐式动画,默认时长为1/4秒

bounds:缩放动画

position:平移动画

opacity:淡入淡出的动画(改变透明度)

更多的隐式动画属性可在文档中搜索

使用CALayer绘图,会调用自己的drawRect方法

//点击界面触触发的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    //隐式动画的时间:0.25秒
    
    //如果不想存在动画
    
    //关闭动画
//    [CATransaction begin];
//    [CATransaction setDisableActions:YES];
    
    //取得点击的坐标
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    
    //1.修改layer的位置
    _layer.position = point;
    
    //2、改变layer的大小
    CGFloat num = arc4random_uniform(50) + 30;
    _layer.bounds = CGRectMake(0, 0, num, num);
    
    //3、修改layer的透明度
    _layer.opacity = arc4random_uniform(10)*0.1;
    
    //4、背景颜色
    _layer.backgroundColor = [UIColor colorWithRed:arc4random_uniform(10)*0.1 green:arc4random_uniform(10)*0.1 blue:arc4random_uniform(10)*0.1 alpha:1].CGColor;
    
    //5、设置边框的弧度
    _layer.cornerRadius = arc4random_uniform(20);
    
    //6、transform
    _layer.transform = CATransform3DMakeScale(2, 1, 1);
    
    //提交
    [CATransaction commit];
    
}








转载于:https://my.oschina.net/zhangqy/blog/507833

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值