zoukankan      html  css  js  c++  java
  • iOS开发

    CALayer的基本使用

    在iOS中。你能看得见摸得着的东西基本上都是UIView。比方一个button、一个文本标签、一个文本输入框、一个图标等等。这些都是UIView

    事实上UIView之所以能显示在屏幕上,全然是由于它内部的一个图层

    在创建UIView对象时,UIView内部会自己主动创建一个图层(即CALayer对象)。通过UIView的layer属性能够訪问这个层
    @property(nonatomic,readonly,retain) CALayer *layer;

    当UIView须要显示到屏幕上时,会调用drawRect:方法进行画图,而且会将全部内容绘制在自己的图层上,画图完毕后,系统会将图层复制到屏幕上,于是就完毕了UIView的显示

    换句话说,UIView本身不具备显示的功能,是它内部的层才有显示功能

    通过操作CALayer对象,能够非常方便地调整UIView的一些外观属性,比方:
    阴影
    圆角大小
    边框宽度和颜色
    … …

    还能够给图层加入动画,来实现一些比較炫酷的效果

    CALayer的属性

    //宽度和高度
    @property CGRect bounds;
    //位置(默认指中点。详细由anchorPoint决定)
    @property CGPoint position;
    //锚点(x,y的范围都是0-1)。决定了position的含义
    @property CGPoint anchorPoint;
    //背景颜色(CGColorRef类型)
    @property CGColorRef backgroundColor;
    //形变属性
    @property CATransform3D transform;
    
    //边框颜色(CGColorRef类型)
    @property CGColorRef borderColor;
    
    //边框宽度
    @property CGFloat borderWidth;
    
    //圆角半径
    @property CGFloat cornerRadius;
    
    //内容(比方设置为图片CGImageRef)
    @property(retain) id contents;
    

    关于CALayer的疑惑

    首先
    CALayer是定义在QuartzCore框架中的(Core Animation)
    CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的
    UIColor、UIImage是定义在UIKit框架中的

    其次
    QuartzCore框架和CoreGraphics框架是能够跨平台使用的,在iOS和Mac OS X上都能使用
    可是UIKit仅仅能在iOS中使用

    为了保证可移植性,QuartzCore不能使用UIImage、UIColor,仅仅能使用CGImageRef、CGColorRef

    UIView和CALayer的选择

    通过CALayer,就能做出跟UIView一样的界面效果

    既然CALayer和UIView都能实现同样的显示效果,那到底该选择谁好呢?
    事实上,对照CALayer,UIView多了一个事件处理的功能。

    也就是说。CALayer不能处理用户的触摸事件。而UIView能够
    所以,假设显示出来的东西须要跟用户进行交互的话,用UIView;假设不须要跟用户进行交互,用UIView或者CALayer都能够
    当然。CALayer的性能会高一些,由于它少了事件处理的功能,更加轻量级

    position和anchorPoint介绍

    //CALayer有2个非常重要的属性:position和anchorPoint
    
    @property CGPoint position;
    用来设置CALayer在父层中的位置
    以父层的左上角为原点(0, 0)
    
    @property CGPoint anchorPoint;
    称为“定位点”、“锚点”
    决定着CALayer身上的哪个点会在position属性所指的位置
    以自己的左上角为原点(0, 0)
    它的x、y取值范围都是0~1,默认值为(0.5, 0.5),意味着锚点在layer的中间
    

    anchorPoint(示意图)

    这里写图片描写叙述

    position和anchorPoint

    这里写图片描写叙述

    隐式动画

    每个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层)

    全部的非Root Layer。也就是手动创建的CALayer对象。都存在着隐式动画

    什么是隐式动画?
    当对非Root Layer的部分属性进行改动时。默认会自己主动产生一些动画效果
    而这些属性称为Animatable Properties(可动画属性)

    列举几个常见的Animatable Properties:
    bounds:用于设置CALayer的宽度和高度。改动这个属性会产生缩放动画
    backgroundColor:用于设置CALayer的背景色。改动这个属性会产生背景色的渐变动画
    position:用于设置CALayer的位置。改动这个属性会产生平移动画

    //能够通过动画事务(CATransaction)关闭默认的隐式动画效果
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.myview.layer.position = CGPointMake(10, 10);
    [CATransaction commit];

    CALayer图层实例

    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *redView;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [UIView animateWithDuration:1 animations:^{
    
             //缩放
            _imageView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
    
             //平移
            _imageView.layer.transform = CATransform3DMakeTranslation(200, 200, 0);
    
             //缩放
            _imageView.layer.transform = CATransform3DMakeScale(1, 0.5, 1);
    
         //利用KVC改变形变
         NSValue *rotation = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)];
    
            [_imageView.layer setValue:rotation forKeyPath:@"transform"];
    
            [_imageView.layer setValue:@M_PI forKeyPath:@"transform.rotation"];
    
            [_imageView.layer setValue:@0.5 forKeyPath:@"transform.scale"];
    
            // 平移x轴
            [_imageView.layer setValue:@200 forKeyPath:@"transform.translation.x"];
    
    
        }];
    }
    
    - (void)imageLayer
    {
        // 圆形裁剪
        _imageView.layer.cornerRadius = 50;
    
        // 超出layer边框的全部裁剪掉
        _imageView.layer.masksToBounds = YES;
    
        _imageView.layer.borderColor = [UIColor whiteColor].CGColor;
        _imageView.layer.borderWidth = 2;
    }
    
    - (void)viewLayer
    {
        // 设置阴影透明度
        _redView.layer.shadowOpacity = 1;
    
        // 设置阴影颜色
        _redView.layer.shadowColor = [UIColor yellowColor].CGColor;
    
        // 设置阴影圆角半径
        _redView.layer.shadowRadius = 10;
    
        // 设置圆角半径
        _redView.layer.cornerRadius = 50;
    
        // 设置边框半径
        _redView.layer.borderColor = [UIColor whiteColor].CGColor;
    
        // 设置边框半径
        _redView.layer.borderWidth = 2;
    }
    
    @end

    自己定义涂层

    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 创建一个图层
        CALayer *layer = [CALayer layer];
    
        // 设置尺寸
        layer.bounds = CGRectMake(0, 0, 100, 100);
    
        // 设置位置
        layer.position = CGPointMake(100, 100);
    
        // 设置颜色
        layer.backgroundColor = [UIColor redColor].CGColor;
    
        // 设置内容
        layer.contents = (__bridge id)[UIImage imageNamed:@"picture.png"].CGImage;
    
    
        [self.view.layer addSublayer:layer];
    
    }
    @end
  • 相关阅读:
    -bash: fork: Cannot allocate memory 问题的处理
    Docker top 命令
    docker常见问题修复方法
    The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
    What's the difference between encoding and charset?
    hexcode of é î Latin-1 Supplement
    炉石Advanced rulebook
    炉石bug反馈
    Sidecar pattern
    SQL JOIN
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7290552.html
Copyright © 2011-2022 走看看