IOS开发入门之核心动画编程指南Core Animation Programming Guide - iOS
白羽 2018-11-23 来源 :网络 阅读 977 评论 0

摘要:本文将带你了解IOS开发入门核心动画编程指南Core Animation Programming Guide - iOS,希望本文对大家学IOS有所帮助。

    本文将带你了解IOS开发入门核心动画编程指南Core Animation Programming Guide - iOS,希望本文对大家学IOS有所帮助。


        

1 有关核心动画


 

1.1 概览

1.1.1 Core Animation 管理应用的内容

核心是 layer objects,

1.1.2 更改 layer 触发动画

Like views, layer objects have a bounds rectangle, a position onscreen, an opacity, a transform, and many other visually-oriented properties that can be modified. 更改这些属性的时候有隐式动画,可以明确指定动画行为。

1.1.3 layer可以组织成层级结构

1.1.4 action 让你更改图层的默认行为

隐式layer动画是使用 action objects。可以创建自己的 action objects ,实现自定义动画,或者也能实现其他类型的行为。

1.2 如果使用这个文档

1.3 预备工作

先熟悉 View Programming Guide for iOS.

核心动画基础

layer 提供绘图和动画的基础

Layer objects 对核心。layer只是管理内容和可视化属性,所以可以将layer想成 model 对象

基于 layer 的绘图模型

 


对于 view 中调用 drawRect: 方法,非常的损耗,因为这在线程上使用CPU。核心动画通过在硬件上缓存位图来达到同样或相似的效果。

基于 layer 的动画

 


layer 对象定义自己的几何

layer 也有像view 有的一些属性,frame bounds,并且也有类似 anchor poing(锚点)这种 view 没有的属性。

layer 使用两种类型的坐标系

point-based coordinate systems 和 unit coordinate systems

通常使用 point-based 坐标系指定 layer 的大小和位置,


 

unit coordinate system (单位坐标系)是用来表示当 layer 的大小变化的时候哪些属性可能改变。可以认为单位坐标系指定所有可能变化值的百分比。


 

锚点影响几何操作

 

 
 <img style="display: none;" src="" alt="How the anchor point affects the layer’s position prZ喎?" https:="" www.2cto.com="" kf="" ware="" vc="" "="" target="_blank" class="keylink">vcGVydHk=" src="https://www.2cto.com/uploadfile/Collfiles/20160908/20160908091844351.jpg"  /> 


 

layer 可以在三维空间中操作

 


Core Animation Functions Reference.

layer 树反映了动画状态的不同方面

有3个 layer 对象集合:

model layer tree 中的对象是模型对象,存储动画的目标值 presentation tree包含动画运行中的值。你应该从来都不要去修改这个 tree 中的对象。使用这些对象读取当前动画的值,或者使用这些值开始新的动画。 render tree 中的对象执行实际的动画,这是核心动画私有的 

 


  

 

layer 和 view 之间的关系

layer 不是 view 的替代品,不能单独靠layer 对象创建一个可视化的界面。layer 提供了 view 的基础。layer 使得view内容的绘制和动画更加有效,并且动画有较高的帧率,就是更流畅。layer不能处理事件,不能绘制内容,不能参与到响应链等等。所以应用必须有一个或多个 view 来处理这种交互

设置 layer 对象

激活核心动画支持

更改与视图相关的 layer 对象

改变 UIView 使用的 layer class

重写 layerClass 类方法 返回想要替换的 类对象。

指定 view 的 layer 类


+ (Class) layerClass {

   return [CAMetalLayer class];

}

   

Changing the Layer Class Used By NSView

不同的 layer 类提供专属行为

CALayer子类及其用法

                                                

  Class

  Usage

给 layer 提供内容

3种方式给layer的位图提供数据:

直接给layer对象的 contents 属性赋值一个 image 对象。这是最好的方法 给layer赋值一个代理对象,并让代理绘制 layer 的内容。(适合内容周期性的变化,比如随着 view变化) 定义一个 layer 子类,并重写一个它的绘制方法 

给 layer 内容设置图像

将image 赋值给 layer必须是 CGImageRef 类型。

使用代理给layer提供内容

代理实现 displayLayer: 方法



   

- (void)displayLayer:(CALayer *)theLayer {

    // Check the value of some state property

    if (self.displayYesImage) {

        // Display the Yes image

        theLayer.contents = [someHelperObject loadStateYesImage];

    }

    else {

        // Display the No image

        theLayer.contents = [someHelperObject loadStateNoImage];

    }

}

   

代理实现 drawLayer:inContext: 方法




   

- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext {

    CGMutablePathRef thePath = CGPathCreateMutable();

 

    CGPathMoveToPoint(thePath,NULL,15.0f,15.f);

    CGPathAddCurveToPoint(thePath,

                          NULL,

                          15.f,250.0f,

                          295.0f,250.0f,

                          295.0f,15.0f);

 

    CGContextBeginPath(theContext);

    CGContextAddPath(theContext, thePath);

 

    CGContextSetLineWidth(theContext, 5);

    CGContextStrokePath(theContext);

 

    // Release the path

    CFRelease(thePath);

}

   

通过子类给 layer 提供内容

重写 layer 的 display 方法,并使用它直接设置contents属性 重写 layer 的 drawInContext: 方法,并使用它来绘制图像内容 

调整提供的内容

 

 
  

 

layer 的 contentsGravity 属性模式的设置是 kCAGravityResize 常量。

 

 
  

 

高分辨率图像

调整图层的视觉风格和外观

layer 有自己的背景和边界

 

 
  

 

layer 支持半径圆角

 

 
  

 

layer 支持内建阴影

 

 
  

 

OS X 的view 添加过滤效果

iOS 不能给 layer 对象添加过滤器

对于 OS X 性能影响的 layer 重回策略

给 layer 添加自定义属性

Animating Layer Content

修改 layer 属性,达到简单动画效果

精确的动画,需要创建一个 CABasicAnimation 对象,使用这个对象来配置参数。


   

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];

fadeAnim.toValue = [NSNumber numberWithFloat:0.0];

fadeAnim.duration = 1.0;

[theLayer addAnimation:fadeAnim forKey:@"opacity"];

 

// Change the actual data value in the layer to the final value.

theLayer.opacity = 0.0;

   

使用关键帧动画来改变 layer 的属性


   

// create a CGPath that implements two arcs (a bounce)

CGMutablePathRef thePath = CGPathCreateMutable();

CGPathMoveToPoint(thePath,NULL,74.0,74.0);

CGPathAddCurveToPoint(thePath,NULL,74.0,500.0,

                                   320.0,500.0,

                                   320.0,74.0);

CGPathAddCurveToPoint(thePath,NULL,320.0,500.0,

                                   566.0,500.0,

                                   566.0,74.0);

 

CAKeyframeAnimation * theAnimation;

 

// Create the animation object, specifying the position property as the key path.

theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];

theAnimation.path=thePath;

theAnimation.duration=5.0;

 

// Add the animation to the layer.

[theLayer addAnimation:theAnimation forKey:@"position"];

   

指定 keyframe 的值

在动画运行的时候,停止动画

removeAnimationForKey: removeAllAnimations 

只能移除显示动画,不能移除隐式动画。

多个动画一起变化


   

// Animation 1

CAKeyframeAnimation* widthAnim = [CAKeyframeAnimation animationWithKeyPath:@"borderWidth"];

NSArray* widthValues = [NSArray arrayWithObjects:@1.0, @10.0, @5.0, @30.0, @0.5, @15.0, @2.0, @50.0, @0.0, nil];

widthAnim.values = widthValues;

widthAnim.calculationMode = kCAAnimationPaced;

 

// Animation 2

CAKeyframeAnimation* colorAnim = [CAKeyframeAnimation animationWithKeyPath:@"borderColor"];

NSArray* colorValues = [NSArray arrayWithObjects:(id)[UIColor greenColor].CGColor,

            (id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor,  nil];

colorAnim.values = colorValues;

colorAnim.calculationMode = kCAAnimationPaced;

 

// Animation group

CAAnimationGroup* group = [CAAnimationGroup animation];

group.animations = [NSArray arrayWithObjects:colorAnim, widthAnim, nil];

group.duration = 5.0;

 

[myLayer addAnimation:group forKey:@"BorderChanges"];

   

检测动画的结束

There are two different ways to be notified about the state of an animation:

Add a completion block to the current transaction using the setCompletionBlock: method. When all of the animations in the transaction finish, the transaction executes your completion block. Assign a delegate to your CAAnimation object and implement the animationDidStart: and animationDidStop:finished: delegate methods. 

基于 layer 的view 怎么做动画

iOS 中修改 layer 的规则


   

[UIView animateWithDuration:1.0 animations:^{

   // Change the opacity implicitly.

   myView.layer.opacity = 0.0;

 

   // Change the position explicitly.

   CABasicAnimation* theAnim = [CABasicAnimation animationWithKeyPath:@"position"];

   theAnim.fromValue = [NSValue valueWithCGPoint:myView.layer.position];

   theAnim.toValue = [NSValue valueWithCGPoint:myNewPosition];

   theAnim.duration = 3.0;

   [myView.layer addAnimation:theAnim forKey:@"AnimateFrame"];

}];

   

建立 layer 层级

将 layer 放入 layer层级中

添加、插入、移除 sublayer

 

 

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程