隐式动画

一、前言

  • 每一个UIView内部都默认关联着一个CALayer对象,我们可用称这个Layer为Root Layer(根层)
  • 所有的非RootLayer根层,也就是手动创建的CALayer对象,都存在着隐式动画

二、什么是隐式动画?

  • 当对非RootLayer的部分属性进行修改时,默认会自动产生一些动画效果
  • 而这些属性称为AnimatableProperties(可动画属性)
  • 也就是说,对非根层的layer可动画属性进行修改产生的动画,就称为隐式动画

三、可动画属性 Animatable Properties

  • 如上所述:我们可以修改可动画属性,来进行隐式动画,现在介绍可动画属性

  • 查看CALayer头文件:查看部分可动画属性

  • 哪些才可以称着,可动画属性呢? -> 属性,注释中包含 Animatable

  • 常见可动画属性

    • bounds:用于设置CALayer的宽度和高度。修改这个属性会产生缩放动画
    • backgroundColor:用于设置CALayer的背景色。修改这个属性会产生背景色的渐变动画
    • position:用于设置CALayer的位置。修改这个属性会产生平移动画
    • ............

// 查看部分头文件,还有其他的可动画属性不在此

/* The bounds of the layer. Defaults to CGRectZero. Animatable. */
@property CGRect bounds;

/* The position in the superlayer that the anchor point of the layer's
 * bounds rect is aligned to. Defaults to the zero point. Animatable. */
@property CGPoint position;

/* The Z component of the layer's position in its superlayer. Defaults
 * to zero. Animatable. */
@property CGFloat zPosition;

/* Defines the anchor point of the layer's bounds rect, as a point in
 * normalized layer coordinates - '(0, 0)' is the bottom left corner of
 * the bounds rect, '(1, 1)' is the top right corner. Defaults to
 * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */
@property CGPoint anchorPoint;

/* The Z component of the layer's anchor point (i.e. reference point for
 * position and transform). Defaults to zero. Animatable. */
@property CGFloat anchorPointZ;

/* A transform applied to the layer relative to the anchor point of its
 * bounds rect. Defaults to the identity transform. Animatable. */
@property CATransform3D transform;

/* Convenience methods for accessing the `transform' property as an
 * affine transform. */
- (CGAffineTransform)affineTransform;
- (void)setAffineTransform:(CGAffineTransform)m;

/* Unlike NSView, each Layer in the hierarchy has an implicit frame
 * rectangle, a function of the `position', `bounds', `anchorPoint',
 * and `transform' properties. When setting the frame the `position'
 * and `bounds.size' are changed to match the given frame. */
@property CGRect frame;

/* When true the layer and its sublayers are not displayed. Defaults to
 * NO. Animatable. */
@property(getter=isHidden) BOOL hidden;

四 隐式动画 实现 : CATransaction(动画事务)

  • 可以通过动画事务(CATransaction)关闭默认的隐式动画效果
[CATransactionbegin];
[CATransactionsetDisableActions:YES];
self.myview.layer.position= CGPointMake(10,10);
[CATransactioncommit];

五、实例

  • 实例:


    隐式动画.gif
    • 点击屏幕,让控制器上的view视图上的一个矩形子控件,不断地进行改变:位置,颜色,与圆角
  • 思路:其实很简单,在touchBegin:方法中 -> 就是不断修改layer的可动画属性实现的

  • 代码实现:如下

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) CALayer *greenLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加 一个自定义的layer到控制器的view的layer上
    CALayer *layer = [CALayer layer];
    
    _greenLayer = layer;
    
    layer.position = CGPointMake(0, 0);
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.anchorPoint = CGPointMake(0, 0);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    
    [self.view.layer addSublayer:layer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 开启事务
    [CATransaction begin];
    
    
//    [CATransaction setDisableActions:YES];
//    [CATransaction setAnimationDuration:2];
    _greenLayer.position = CGPointMake(arc4random_uniform(250), arc4random_uniform(300));
    
    _greenLayer.backgroundColor = [self randomColor].CGColor;
    _greenLayer.cornerRadius = arc4random_uniform(50);
    
    _greenLayer.borderColor = [self randomColor].CGColor;
    _greenLayer.borderWidth = arc4random_uniform(5);
    
    // 提交事务
    [CATransaction commit];
    
}

/**
 *  返回 随机色
 */
- (UIColor *)randomColor
{
    CGFloat r = arc4random_uniform(256) / 255.0;
    CGFloat g = arc4random_uniform(256) / 255.0;
    CGFloat b = arc4random_uniform(256) / 255.0;
    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

@end
     ```



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,083评论 5 13
  • 问题: 1.什么是隐式动画,为什么CALayer设置可动画属性时会触发隐式动画?2.UIView设置属性,为什么没...
    人生看淡不服就干阅读 10,552评论 1 15
  • 在前面的学习中,我们讨论了CoreAnimation除了动画外可以做到的任何事情。但是动画是CoreAni...
    小猫仔阅读 1,510评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,439评论 6 30
  • Core Animation Core Animation除了动画之外可以做到的任何事情。但是动画是Core An...
    清风沐沐阅读 630评论 1 3