核心动画1

/*

 使用核心动画 需导入QuarzCore 框架(现在 不需要)

 #import <QuartzCore/QuartzCore.h>

 

 

 

 CALayer和UIView的关系:

 1、在UIView中有一个layer属性作为根图层,根图层没有隐式动画,根图层上可以放其他子图层,在UIview中所有能够看到的内容都包含在layer中

 2、CALayer负责视图中显示的内容和动画

 3、UIView负责监听和响应事件

 

 4、CALayer:包含在QuarzCore框架中,QuarzCore既可以用在iOS中又可以用在Mac OS X中

 

 -------------------------------------------------------------------------

 CALayer的存在意义:

 1、在iOS中CALayer的设计主要是为了内容展示和动画操作,CALayer本身并不包含在UIKit中,它不能响应事件

 2、由于CALayer在设计之初就考虑它的动画操作功能,CALayer很多属性在修改时都能形成动画效果,这种属性被称为“隐式动画属性”

 

 -------------------------------------------------------------------------

CALayer的常用属性:

 定义一个图层的位置:

 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 图层形变

 

 -------------------------------------------------------------------------

注意:1、图层上不能添加手势

     2、以上支持隐式动画的属性 本质是这些属性的变动默认隐含了CABasicAnimation动画实现

 */

 

 

#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>

 

@interface ViewController ()

{

    CALayer *myLayer;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

//    [self test1];

    [self layerTest];

    self.view.backgroundColor = [UIColor colorWithRed:0.074 green:0.854 blue:0.988 alpha:1.000];

}

 

- (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)layerTest {

    myLayer = [[CALayer alloc] init];

    myLayer.bounds = CGRectMake(0, 0, 200, 200);

//    设置中心点

    myLayer.position = self.view.center;

    myLayer.backgroundColor = [UIColor orangeColor].CGColor;

    myLayer.cornerRadius = 100;

    myLayer.borderWidth = 2;

    myLayer.borderColor = [UIColor whiteColor].CGColor;

    myLayer.shadowColor = [UIColor yellowColor].CGColor;

    myLayer.shadowOffset = CGSizeMake(-1, -5);

//    设置阴影颜色必须先设置 shadowOpacity(阴影颜色的透明度 默认是0 完全透明)

    myLayer.shadowOpacity = 1.0;

//    子图层是添加到 根图层上的

    [self.view.layer addSublayer:myLayer];

    

    myLayer.delegate = self;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

 

    myLayer.bounds = CGRectMake(0, 0, 300, 300);

    myLayer.backgroundColor = [UIColor greenColor].CGColor;

    myLayer.cornerRadius = 150;

 

}

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    myLayer.position = [touch locationInView:self.view];

    

//    得到layer的宽度

    CGFloat width = CGRectGetWidth(myLayer.bounds);

    width = width == 300 ? 200:300;

    myLayer.bounds = CGRectMake(0, 0, width, width);

    myLayer.cornerRadius = width/2;

    myLayer.backgroundColor = myLayer.backgroundColor == [UIColor orangeColor].CGColor ? [UIColor greenColor].CGColor : [UIColor orangeColor].CGColor;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

原文地址:https://www.cnblogs.com/wukun16/p/4884162.html