ios动画 calyer-旋转,缩放,裁剪,阴影,平移等

本文详细阐述了CALayer与UIView的关系及其核心功能,包括CALayer如何实现UIView的显示,以及关键属性如position和anchorPoint的作用。同时介绍了CALayer的一些基本属性和隐式动画的概念。
CALayerUIView的关系
(1)CALayer负责视图中显示的内容动画
(2)UIView负责监听和响应事件
positionanchorPoint的作用

CALayer:
iOS,你能看得见摸得着的东西基本上都UIView比如一个按钮、一个文本标签、一个文本输入框、一个图标等等,这些都UIView
其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层
在创建UIView对象时,UIView内部会自动创建一个图层(CALayer对象),通过UIViewlayer属性可以访问这个层

@property(nonatomic,readonly,retain)CALayer *layer;

UIView需要显示到屏幕上时,会调用drawRect:方法进行绘图,并且会将所有内容绘制在自己的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示
换句话说,UIView本身不具备显示的功能,是它内部的层才有显示功能
CALayer的属性:
宽度和高度 @propertyCGRectbounds;
位置(默认指中点,具体由anchorPoint决定) : @propertyCGPointposition;
锚点(x,y的范围都是0-1),决定了position的含义 :@propertyCGPointanchorPoint;
背景颜(CGColorRef类型) :@propertyCGColorRefbackgroundColor;
形变属性:@property CATransform3Dtransform;
边框颜(CGColorRef类型) @propertyCGColorRefborderColor;
边框宽度 @propertyCGFloatborderWidth;
半径  @propertyCGFloatcornerRadius;
内容(比如设置为图片CGImageRef)  @property(retain)idcontents;
关于CALayer的疑惑
首先
CALayer是定义在QuartzCore框架中的(Core Animation)
CGImageRefCGColorRef两种数据类型是定义在CoreGraphics框架中的
UIColorUIImage是定义在UIKit框架中的
QuartzCore框架和CoreGraphics框架是可以跨平台使用的,在iOSMac OS X上都能使用
UIKit只能在iOS使用
为了保证可移植性,QuartzCore不能使用UIImageUIColor只能使用CGImageRefCGColorRef

CALayer2个非常重要的属性positionanchorPoint
@propertyCGPointposition;
  用来设置CALayer在父层中的位置
   以父层的左上角为原点(0, 0)
@propertyCGPointanchorPoint;
   称为定位点、“锚点”
 定着CALayer身上的哪个点会在position属性所指的位置
 以自己的左上角为原点(0, 0)
 它的xy取值范围都是0~1默认值为0.5,0.5,意味着锚点在layer中间
什么是隐式动画?
   当对RootLayer部分属性进行修改时默认会自动产生一些动画效果
   而这些属性称为AnimatableProperties(可动画属性)
列举几个常见的Animatable Properties
 bounds用于设置CALayer的宽度和高度。修改这个属性会产生缩放动画
 backgroundColor用于设置CALayer背景色。修改这个属性会产生背景色的渐变动画
 position用于设置CALayer位置。修改这个属性会产生平移动画


可以通过动画事务(CATransaction)关闭默认的隐式动画效果

[CATransactionbegin];

[CATransactionsetDisableActions:YES];

self.myview.layer.position = CGPointMake(10,10);

[CATransactioncommit];


demo:

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation HMViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:1 animations:^{
        [self imageLayer];
        [self viewLayer];
        
        // 缩放
       // _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"];
 
    }];
}
/**
 利用layer将图片裁剪成圆形图片
 **/
- (void)imageLayer
{
    // 圆形裁剪
    _imageView.layer.cornerRadius = 50;
    // 超出layer边框的全部裁剪掉
    _imageView.layer.masksToBounds = YES;
    _imageView.layer.borderColor = [UIColor grayColor].CGColor;
    _imageView.layer.borderWidth = 1;
}
/**
 利用layer将view变成圆形
 **/
- (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;
}



自定义图层:
- (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:@"阿狸头像"].CGImage;
    [self.view.layer addSublayer:layer];
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值