CAAnimation动画/CAAnimation Group

简介: <p style="margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:24px"> <a target="_blank"><span style="text-decoration:underline"><strong>动画</strong

动画分隐式动画和显式动画


CAAnimation采用了CAMediaTiming协议,可以调整时间,包括持续时间,速度,重复次数;采用了CAAction协议,可以通过响应动作的方式来显示动画.

CAAnimation的一些派生类:
CATransition 提供渐变效果:(推拉push效果,消退fade效果,揭开reveal效果)
CAAnimationGroup 允许多个动画同时播放
CABasicAnimation 提供了对单一动画的实现
CAKeyframeAnimation 关键桢动画,可以定义行动路线
CAConstraint 约束类,在布局管理器类中用它来设置属性
CAConstraintLayoutManager 约束布局管理器,是用来将多个CALayer进行布局的.各个CALayer是通过名称来区分,而布局属性是通过CAConstraint来设置的.
CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务.


事务管理(Transactions)
事务分两种:
1.隐式事务(implicit transaction)
除显式事务外,任何对于CALayer属性的修改,都是隐式事务.这样的事务会在run-loop中被提交.
如:
theLayer.opacity = 0.0;
theLayer.zPosition = -200;
theLayer.position = CGPointMake(0.0, 0.0);

2.显式事务(explicit transaction)
a. 通过明确的调用begin,commit来提交动画.优点是可以同时修改多个Layer的属性.
如:
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                               forKey:kCATransactionDisableActions];
[aLayer removeFromSuperlayer];
[CATransaction commit];

b.可以重置持续时间
可以在begin,commit对中临时修改动画持续时间.
[CATransaction begin]
[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
                               forKey:kCATransactionAnimationDuration];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit];

c.事务可以嵌套.
如:
//第一层嵌套
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                             forKey:kCATransactionAnimationDuration];
theLayer.position = CGPointMake(0.0, 0.0);
//第二层嵌套
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:5.0f]
                               forKey:kCATransactionAnimationDuration];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit];
[CATransaction commit];



布局管理器示例如下:
//创建和设置一个布局管理器
theLayer.layoutManager = [CAConstraintLayoutManager layoutManager];

//创建layerA
CALayer *layerA = [CALayer layer];
layerA.name = @"layerA";

//设置layerA的中点位置等于超类的中点位置
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY
                                                                                       relativeTo:@"superLayer"
                                                                                       attribute:kCAConstraintMidY]];

[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX
                                                                                       relativeTo:@"superLayer"
                                                                                       attribute:kCAConstraintMidX]];
[theLayer addSublayer:layerA];

//创建layerB
CALayer *layerB = [CALayer layer];
layerB.name = @"layerB";

//设置layerB的宽度等于layerA的宽度
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth
                                                                                       relativeTo:@"LayerA"
                                                                                       attribute:kCAConstraintWidth]];

[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX
                                                                                       relativeTo:@"layerA"
                                                                                       attribute:kCAConstraintMidX]];

[theLayer addSublayer:layerB];

 

转自‘http://hi.baidu.com/iamgleaf/blog/item/38520d949d025107d31b7023.html

 

 

 

 

复制代码
//  AnimationView.m

//  AnimationTestV2

//

//  Created by jtone  on 11-8-9.

//  Copyright 2011年__MyCompanyName__. All rights reserved.

//

#import"AnimationView.h"

#import<QuartzCore/QuartzCore.h>

#define kDuaitionOfTimer00.2

#define kDuaitionOfTimer10.2

#define kDuaitionOfTimer20.2

#define kDuaitionOfTimer30.2

#define kDuaitionOfTimer41.1

#define kDisplacementOfTimer110

#define kDisplacementOfTimer210

@implementationAnimationView

CGPoint leftPhoneCenter;

CGPoint contactCenter;

CGPoint rightPhoneCenter;

CGPoint picCenter;



//位置变化动画

- (CAAnimation *)animationMoveFrom:(CGPoint) from To:(CGPoint) to Duration:(CGFloat) duration BeginTime:(CGFloat)beginTime 

{

CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

CGFloat animationDuration = duration;

CGMutablePathRef thePath = CGPathCreateMutable();

CGPathMoveToPoint(thePath, NULL, from.x, from.y);

CGPathAddLineToPoint(thePath, NULL, to.x, to.y);

bounceAnimation.path = thePath;

bounceAnimation.duration = animationDuration;

    bounceAnimation.beginTime = beginTime;

bounceAnimation.repeatCount=0;

bounceAnimation.removedOnCompletion=NO;

bounceAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

CGPathRelease(thePath);

return bounceAnimation;

}


//透明度变化动画

-(CAAnimation *)animationWithOpacityFrom:(CGFloat) from To:(CGFloat) to Duration:(CGFloat) duration BeginTime:(CGFloat)beginTime 

{    

    CABasicAnimation *theAnimation;    

    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];    

    theAnimation.duration=duration; 

    theAnimation.beginTime = beginTime;    

    theAnimation.repeatCount=0;    

    theAnimation.autoreverses=NO;    

    theAnimation.fromValue=[NSNumber numberWithFloat:from];    

    theAnimation.toValue=[NSNumber numberWithFloat:to];

    

    return theAnimation;

}



-(NSArray *)AnimWithPhone:(CGPoint)phoneCenter Option:(int)option//手机壳动画设置

{

    NSArray *arr = [NSArray arrayWithObjects:

                    [self   animationMoveFrom:phoneCenter  To:CGPointMake(phoneCenter.x+kDisplacementOfTimer1*option,phoneCenter.y)    Duration:kDuaitionOfTimer0 BeginTime:1.5],

                    [self   animationMoveFrom:CGPointMake(phoneCenter.x+kDisplacementOfTimer1*option,phoneCenter.y)    To:phoneCenter  Duration:kDuaitionOfTimer1 BeginTime:1.7],

                    [self   animationMoveFrom:phoneCenter    To:CGPointMake(phoneCenter.x+kDisplacementOfTimer2*option,phoneCenter.y)  Duration:kDuaitionOfTimer2 BeginTime:1.9],

                    [self  animationMoveFrom:CGPointMake(phoneCenter.x+kDisplacementOfTimer2*option,phoneCenter.y)    To:phoneCenter  Duration:kDuaitionOfTimer3 BeginTime:2.1],

                   nil];

   returnarr;

}



-(NSArray *)AnimFromObj:(CGPoint)obj1 ToObj:(CGPoint)obj2 Option:(int)option//图片,联系人动画设置

{

    NSArray *arr = [NSArray arrayWithObjects:

                    [self  animationWithOpacityFrom:0.0To:0.0Duration:1.0BeginTime:0.0],

                    [self  animationWithOpacityFrom:0.0To:1.0Duration:0.3BeginTime:1.0],

                    [self  animationMoveFrom:obj1  To:CGPointMake(obj1.x+kDisplacementOfTimer1*option, obj1.y)    Duration:kDuaitionOfTimer0 BeginTime:1.5],

                    [self  animationMoveFrom:CGPointMake(obj1.x+kDisplacementOfTimer1*option, obj1.y)    To:obj1  Duration:kDuaitionOfTimer1 BeginTime:1.7],

                    [self  animationMoveFrom:obj1    To:CGPointMake(obj1.x+kDisplacementOfTimer2*option,obj1.y)   Duration:kDuaitionOfTimer2 BeginTime:1.9],

                    [self  animationMoveFrom:CGPointMake(obj1.x+kDisplacementOfTimer2*option,obj1.y)     To:obj1  Duration:kDuaitionOfTimer3 BeginTime:2.1],

                    [self  animationMoveFrom:obj1 To:obj2 Duration:kDuaitionOfTimer4 BeginTime:2.4],

                    [self  animationMoveFrom:obj2 To:obj2 Duration:kDuaitionOfTimer4 BeginTime:3.5],

                    [self  animationWithOpacityFrom:1.0To:0.0Duration:0.3BeginTime:3.7],

                   nil];                    

   returnarr;

}




-(CAAnimationGroup *)getAnimGroup //创建动画组

{

    CAAnimationGroup * animGroup  = [CAAnimationGroup animation];

animGroup.delegate            = self;

animGroup.removedOnCompletion = NO;

animGroup.duration  = 4.0;

animGroup.repeatCount  = 1;

animGroup.fillMode  = kCAFillModeForwards;

    return animGroup;

}




-(void)setup//开始动画

{

    leftPhoneCenter    = CGPointMake(leftPhone.frame.origin.x+35, leftPhone.frame.origin.y+55);

    contactCenter         = CGPointMake(contact.frame.origin.x+25, contact.frame.origin.y+25);

    rightPhoneCenter = CGPointMake(rightPhone.frame.origin.x+35, rightPhone.frame.origin.y+55);

    picCenter                 = CGPointMake(picture.frame.origin.x+25,picture.frame.origin.y+25);

    

    contact.hidden = NO;

    picture.hidden = NO;

    

    CAAnimationGroup * mp1 = [self getAnimGroup];

    mp1.animations           = [self AnimWithPhone:leftPhoneCenter Option:-1];

    

    [leftPhone.layer addAnimation:mp1 forKey:@"jtone"];

    [leftPhoneScreen.layer addAnimation:mp1 forKey:@"jtone"];

    

    CAAnimationGroup * mp2 = [self getAnimGroup];

    mp2.animations           = [self AnimWithPhone:rightPhoneCenter Option:1];

    

    [rightPhone.layer addAnimation:mp2 forKey:@"jtone"];

    [rightPhoneScreen.layer addAnimation:mp2 forKey:@"jtone"];

    

    CAAnimationGroup * mp3 = [self getAnimGroup];

    mp3.animations           = [self AnimFromObj:contactCenter ToObj:picCenter Option:-1];

    [contact.layer addAnimation:mp3 forKey:@"jtone"];    

    

    CAAnimationGroup * mp4 = [self getAnimGroup];

    mp4.animations           = [self AnimFromObj:picCenter ToObj:contactCenter Option:1];

    [picture.layer addAnimation:mp4 forKey:@"jtone"];

}



- (void)dealloc

{

    [superdealloc];

}

@end
复制代码
目录
相关文章
|
安全 数据安全/隐私保护 iOS开发
基于iOS的动态权限管理实现
【4月更文挑战第9天】 随着移动互联网的快速发展,用户对应用程序的隐私安全要求越来越高。在iOS平台中,如何实现动态权限管理成为了开发者关注的焦点。本文将详细介绍一种基于iOS的动态权限管理实现方法,通过使用Core Motion框架和Notification Center,实现对用户位置信息的实时监控和动态权限申请。
|
10月前
|
人工智能 IDE API
AI驱动的开发者工具:打造沉浸式API集成体验
本文介绍了阿里云在过去十年中为开发者提供的API服务演变。内容分为两大部分:一是从零开始使用API的用户旅程,涵盖API的发现、调试与集成;二是回顾阿里云过去十年为开发者提供的服务及发展历程。文中详细描述了API从最初的手写SDK到自动化生成SDK的变化,以及通过API Explorer、IDE插件和AI助手等工具提升开发者体验的过程。这些工具和服务旨在帮助开发者更高效地使用API,减少配置和调试的复杂性,提供一站式的解决方案。
|
Linux 开发工具
Linux配置软件仓库
Linux配置软件仓库。配置光盘内容为yum/dnf命令的软件仓库。
1549 0
|
6月前
|
数据采集 机器学习/深度学习 人工智能
代理IP:企业AI应用的隐形加速器与合规绞索
代理IP作为企业AI应用的重要基础设施,既是效率提升的加速器,也可能成为合规风险的来源。它通过技术演进重塑数据采集、模型训练与安全防护等核心环节,如智能路由、量子加密和边缘计算等创新方案显著优化性能。然而,全球法规(如GDPR)对数据流动提出严格要求,促使企业开发自动化合规审计系统应对挑战。未来,代理IP将向智能路由3.0、PaaS服务及量子网络方向发展,成为连接物理与数字世界的神经网络。企业在享受其带来的效率增益同时,需构建技术、法律与伦理三位一体的防护体系以规避风险。
186 0
|
6月前
|
人工智能 运维 自然语言处理
【CodeBuddy】今天520,我只教你一遍。
在520这个充满爱意的日子,作者仅用5分钟就完成了一个包含时空胶囊、动态情书、记忆时间轴等功能的浪漫网页应用。借助CodeBuddy的AI编程能力,从动态UI生成到交互逻辑构建,再到数据结构设计,AI将创意快速转化为可运行的代码艺术。文章详细解析了开发过程中的技术突破与AI贡献,如动画协调、状态保持及移动端适配等,并探讨了AI编程对创意突破、效率提升和学习范式的深远影响。最终,通过粒子特效与时间轴流动,展现了AI编程如何开启新纪元,让开发者专注于创意与架构,实现人类智慧与AI执行力的完美协奏。
155 0
【CodeBuddy】今天520,我只教你一遍。
|
开发者 人工智能 自然语言处理
欢迎使用通义灵码
灵码使用指南!一键收藏。
141523 31
|
SQL 数据库
|
机器学习/深度学习 JSON 自然语言处理
LLM2Vec介绍和将Llama 3转换为嵌入模型代码示例
通过LLM2Vec,我们可以使用LLM作为文本嵌入模型。但是简单地从llm中提取的嵌入模型往往表现不如常规嵌入模型。
655 5
|
小程序 JavaScript
微信小程序学习之数据绑定,事件绑定,事件传参与数据同步的学习记录
本文介绍了微信小程序中的数据绑定、事件绑定、事件传参与数据同步的基本概念和使用方法,包括如何在data对象中定义数据、使用mustache语法在wxml中渲染数据、绑定和处理事件、事件对象属性、事件传参以及实现输入框与data数据的同步。
微信小程序学习之数据绑定,事件绑定,事件传参与数据同步的学习记录

热门文章

最新文章