核心动画

使用核心动画  需要导入QuartzCore框架(现在 不需要)
 #import <QuartzCore/QuartzCore.h>
 
 CoreAnimation 核心动画  简写CA
 
 CALayer(图层)和UIView的关系:在UIView中有一个layer属性作为根图层,根图层没有隐式动画 根图层上可以放其他子图层, 在UIView中所有能够看到的内容都包含在layer中
 CALayer负责视图中显示的内容和动画
 UIView负责监听和响应事件
 
 CALayer在修改他的属性时都能形成动画效果 叫隐式动画。
 由于CALayer在设计之初就考虑它的动画操作功能,CALayer很多属性在修改时都能形成动画效果,这种属性称为“隐式动画属性”。
 
 CLAyer
 属性    说明      是否支持隐式动画
 anchorPoint    锚点、定位点  锚点的描述是相对于 *自己* x、y位置比例而言的 默认在图像中心点(0.5,0.5)的位置  决定图层的哪一个点 显示在中心点的位置     是
 backgroundColor    图层背景颜色    是
 borderColor    边框颜色    是
 borderWidth    边框宽度    是
 bounds    图层大小    是
 contents    图层显示内容,例如可以将图片作为图层内容显示    是
 contentsRect    图层显示内容的大小和位置     是
 cornerRadius    圆角半径    是
 doubleSided    图层背面是否显示,默认为YES    否
 frame    图层大小和位置,不支持隐式动画,所以CALayer中很少使用frame,通常使用bounds和position代替    否
 hidden    是否隐藏    是
 mask    图层蒙版    是
 maskToBounds    子图层是否剪切图层边界,默认为NO    是
 opacity    透明度 ,类似于UIView的alpha    是
 position    决定图层在父视图的位置 图层位于 *父视图* 中心点位置,类似于UIView的center    是
 shadowColor    阴影颜色    是
 shadowOffset    阴影偏移量    是
 shadowOpacity    阴影透明度,注意默认为0,如果设置阴影必须设置此属性    是
 shadowPath    阴影的形状    是
 shadowRadius    阴影模糊半径    是
 sublayers    子图层    是
 sublayerTransform    子图层形变    是
 transform    图层形变
 
 以上支持隐式动画的属性 本质是这些属性的变动默认隐含了CABasicAnimation动画实现
 
 */

#import "ViewController.h"


@interface ViewController ()
{
    CALayer *myLayer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.191 green:0.945 blue:1.000 alpha:1.000];
    [self layerTest1];

}

- (void)test1
{
    //    CALayer和UIView的关系:
    //    在UIView中有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内容都包含在layer中
    //    CALayer负责视图中显示的内容和动画
    //    UIView负责监听和响应事件
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:view];
    
    //    图层上不可以添加监听和响应事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(testResponse:)];
    [view addGestureRecognizer:tap];
}

- (void)testResponse:(UITapGestureRecognizer *)sender{
    
    sender.view.layer.cornerRadius = sender.view.layer.cornerRadius == 0?50:0;
    
}

#pragma mark------CALayer初体验-------
- (void)layerTest1{
    myLayer = [[CALayer alloc]init];
    myLayer.bounds = CGRectMake(0, 0, 100, 100);
//    设置中心点
    myLayer.position = self.view.center;
    myLayer.backgroundColor = [UIColor redColor].CGColor;
    
    myLayer.cornerRadius = 50;
    myLayer.borderWidth = 2;
    myLayer.borderColor = [UIColor whiteColor].CGColor;
    myLayer.shadowColor = [UIColor yellowColor].CGColor;
//    设置阴影颜色 必须设置shadowOpacity 阴影颜色的透明度 (默认是0 完全透明)
    myLayer.shadowOffset = CGSizeMake(-3, -5);
    myLayer.shadowOpacity = 0.6;
    
//    子图层 是添加到 根图层上面的
    [self.view.layer addSublayer:myLayer];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    myLayer.bounds = CGRectMake(0, 0, 300, 300);
    myLayer.backgroundColor = [UIColor blueColor].CGColor;
    myLayer.cornerRadius = 150;
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    myLayer.position = [touch locationInView:self.view];
    
    CGFloat width = CGRectGetWidth(myLayer.bounds);
    width = width == 300 ? 100 : 300;
    myLayer.bounds = CGRectMake(0, 0, width, width);
    
    myLayer.cornerRadius = width/2;
    
    myLayer.backgroundColor = myLayer.backgroundColor == [UIColor blueColor].CGColor ? [UIColor redColor].CGColor : [UIColor blueColor].CGColor;
}
原文地址:https://www.cnblogs.com/liYongJun0526/p/4874103.html