动画解释

Core Animation 的 CALayer,这是动画产生作用的地方。

基本动画

让红色button的 x-position 从  150.0  变为  320.0 ,刚好超过它的 parent view 的边

使用 CABasicAnimation,可以如下实现这个动画

    CABasicAnimation *animation = [CABasicAnimationanimation];

    animation.keyPath =@"position.x";

    animation.fromValue =@150;

    animation.toValue =@320;

    animation.duration =1;

    [self.buttonTest.layeraddAnimation:animationforKey:@"basic"];

请注意我们要动画的键路径,也就是 position.x,实际上包含一个存储在 position 属性中的 CGPoint 结构体成员。这是 Core Animation 一个非常方便的特性。请务必查看https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/Key-ValueCodingExtensions/Key-ValueCodingExtensions.html

然而,当我们运行该代码时,我们意识到火箭在完成动画后马上回到了初始位置。这是因为在默认情况下,动画不会在超出其持续时间后还修改 presentation layer。实际上,在结束时它甚至会被彻底移除。

一旦动画被移除,presentation layer 将回到 model layer 的值,并且因为我们从未修改该 layer 的 position 属性,所以我们的飞船将重新出现在它开始的地方。

改变动画位置使其在留在动画完成后的位置

    CABasicAnimation *animation = [CABasicAnimationanimation];

    animation.keyPath =@"position.x";

    animation.fromValue =@150;

    animation.toValue =@320;

    animation.duration =1;

    [self.buttonTest.layeraddAnimation:animationforKey:@"basic"];

    self.buttonTest.layer.position =CGPointMake(320,61);

值得指出的是,实际上我们创建的动画对象在被添加到 layer 时立刻就复制了一份。这个特性在多个 view 中重用动画时这非常有用。比方说我们想要第二个button在第一个button移动不久后起飞:

CABasicAnimation *animation = [CABasicAnimationanimation];

    animation.keyPath =@"position.x";

    animation.byValue =@320;//移动的距离

    animation.duration =1;

    

    [self.buttonTest.layeraddAnimation:animationforKey:@"basic"];

   self.buttonTest.layer.position =CGPointMake(150,61);

    

    animation.beginTime =CACurrentMediaTime() +0.5;//在第一动画开始后的0.5秒开始

    

    [self.buttonBlueTest.layeraddAnimation:animationforKey:@"basic"];

    self.buttonBlueTest.layer.position =CGPointMake(150,111);

多步动画

CAKeyframeAnimation *animation = [CAKeyframeAnimationanimation];

    animation.keyPath =@"position.x";

    animation.values =@[@0,@10,@-10,@10,@0];

    animation.keyTimes =@[@0,@(1 / 6.0),@(3 / 6.0),@(5 / 6.0),@1 ];

    animation.duration =0.4;

    animation.additive =YES;

    [self.textFieldText.layeraddAnimation:animationforKey:@"shake"];

values 数组定义了表单应该到哪些位置。

设置 keyTimes 属性让我们能够指定关键帧动画发生的时间。它们被指定为关键帧动画总持续时间的一个分数。

设置  additive  属性为  YES  使 Core Animation 在更新 presentation layer 之前将动画的值添加到 model layer 中去。这使我们能够对所有形式的需要更新的元素重用相同的动画,且无需提前知道它们的位置。因为这个属性从  CAPropertyAnimation  继承,所以你也可以在使用  CABasicAnimation  时使用它。

RBBAnimation项目---学习

1.enable strict checking of objc_msgSend Calls 需要设为no,便于编写iOS,os通用静态类库。

2.在Xcode的Build Settings 里的User-Defined里用户可以自定义全局变量,并为其赋值

3.#import <libextobjc/EXTSynthesize.h>,自定义文件库引入,Xcode的Build Settings 里的Header Search Paths 设置文件路径




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值