iOS Core Animation学习总结(3)--动画的基本类型
一. CABasicAnimation (基础动画)
移位:
CABasicAnimation *animation = [CABasicAnimation animation];
//keyPath指定动画类别,position表示移位
animation.keyPath = @"position";
//移动到x=200,y=200的位置
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(, )];
animation.duration = ;
//动画执行完毕后不删除动画
animation.removedOnCompletion = NO;
//保持最新的状态
animation.fillMode = @"forwards";
//添加动画
[self.layer addAnimation:animation forKey:nil];
缩放:
CABasicAnimation *animation = [CABasicAnimation animation];
//keyPath指定动画类别,bounds表示缩放
animation.keyPath = @"bounds";
//缩放到width=50,height=50
animation.toValue = [NSValue valueWithCGRect:CGRectMake(, , , )];
animation.duration = ;
//动画完成不删除动画
animation.removedOnCompletion = NO;
//保持最新的状态
animation.fillMode = @"forwards"; [self.layer addAnimation:animation forKey:nil];
旋转:
CABasicAnimation *animation = [CABasicAnimation animation];
//keyPath指定动画类别,transform表示旋转
animation.keyPath = @"transform";
//沿x,y轴顺时针旋转45度
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, , , )];
animation.duration = ;
animation.removedOnCompletion = NO;
animation.fillMode = @"forwards"; [self.layer addAnimation:animation forKey:nil];
二. CAKeyframeAnimation (关键帧动画)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"position";
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2.0; //设置圆形轨迹,并绕圆形轨迹移动
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(, , , ));
anim.path = path;
CGPathRelease(path); // 设置动画的执行节奏
// kCAMediaTimingFunctionEaseInEaseOut : 一开始比较慢, 中间会加速, 临近结束的时候, 会变慢
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.delegate = self;
[anim setValue:@"aaa" forKey:@"TAG"];
[self.layer addAnimation:anim forKey:nil];
设置代理的回调方法,让动画结束后弹出提示
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSString *strTag = [anim valueForKey:@"TAG"];
if ([strTag isEqualToString:@"aaa"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Animation Done" message:@"动作完成" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
}
三. CATransition(转场动画)
(1)视图跳转
_newView = [[UIView alloc] init];
_newView.frame = CGRectMake(, , , );
[self.view addSubview:_newView]; UIView *view1 = [[UIView alloc] init];
view1.frame = CGRectMake(, , , );
view1.backgroundColor = [UIColor yellowColor];
[_newView addSubview:view1]; UIView *view2 = [[UIView alloc] init];
view2.frame = CGRectMake(, , , );
view2.backgroundColor = [UIColor greenColor];
[_newView addSubview:view2];
添加转场按钮事件处理:
- (IBAction)exchangeView {
// 转场动画
CATransition *transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = @"pageCurl";
transition.subtype = kCATransitionFromRight;
transition.duration = ;
[_newView exchangeSubviewAtIndex: withSubviewAtIndex:];
[_newView.layer addAnimation:transition forKey:@"myAnimation"];
}
(2)控制器跳转
- (IBAction)pushView {
CATransition *transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//立体动画效果
transition.type = @"cube";
[self.navigationController.view.layer addAnimation:transition forKey:@"navAnimation"];
TestViewController *testVC = [[TestViewController alloc] init];
[self.navigationController showViewController:testVC sender:nil];
}
四. CAAnimationGroup (组合动画)
//添加图片
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"curry.jpg"]];
imgView.frame = CGRectMake(, , imgView.frame.size.width, imgView.frame.size.height);
[self.view addSubview:imgView]; //贝塞尔曲线路径
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:CGPointMake(10.0, 10.0)];
[movePath addQuadCurveToPoint:CGPointMake(, ) controlPoint:CGPointMake(, )]; //以下必须导入QuartzCore包
//关键帧动画(位置)
CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
posAnim.path = movePath.CGPath;
posAnim.removedOnCompletion = YES; //缩放动画
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnim.removedOnCompletion = YES; //透明动画
CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
opacityAnim.removedOnCompletion = YES; //动画组
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:posAnim, scaleAnim, opacityAnim, nil];
animGroup.duration = ; [imgView.layer addAnimation:animGroup forKey:nil];
iOS Core Animation学习总结(3)--动画的基本类型的更多相关文章
- iOS Core Animation学习总结(1)--CALayer常用属性
图层是core animation的基础, UIView之所以能显示在屏幕上,靠的是其内部的这个图层,即每个UIView 都有 CALayer,可通过UIView.layer或者[UIView lay ...
- iOS Core Animation学习总结(2)--实现自定义图层
一. 创建图层继承于CALayer,并在子类实现drawInContext方法 @interface CTLayer : CALayer @end @implementation CTLayer -( ...
- 转 iOS Core Animation 动画 入门学习(一)基础
iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...
- iOS - Core Animation 核心动画
1.UIView 动画 具体讲解见 iOS - UIView 动画 2.UIImageView 动画 具体讲解见 iOS - UIImageView 动画 3.CADisplayLink 定时器 具体 ...
- Core Animation学习总结
文件夹: The Layer Beneath The Layer Tree(图层树) The Backing Image(寄宿层) Layer Geometry(图层几何学) Visual Effec ...
- iOS Core Animation 简明系列教程
iOS Core Animation 简明系列教程 看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽 ...
- 使用Core Animation对象来实现动画
转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...
- iOS Core Animation 动画 入门学习(一)基础
reference:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide ...
- IOS Core Animation Advanced Techniques的学习笔记(五)
第六章:Specialized Layers 类别 用途 CAEmitterLayer 用于实现基于Core Animation粒子发射系统.发射器层对象控制粒子的生成和起源 CAGradient ...
随机推荐
- nyoj 115 城市平乱
城市平乱 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...
- Struts2中DMI(动态方法调用)的错误问题(There is no Action mapped for namespace [/xxx] and action name [xxx!yyy] a)
默认的Struts.xml中是这样的 <constant name="struts.enable.DynamicMethodInvocation" value="f ...
- 幻灯片(响应式设计)(jquery实现)
Html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- ECSHOP在线手册布局参考图--文章列表页 article_cat.dwt
A.购物车 1,设置方法 程序自动读取购物车的商品数量 2,代码相关 cart.lbi 中 {insert_scripts files='transport.js'} <div clas ...
- <meta 标签的详细使用
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:na ...
- 【转】安装Ubuntu 15.10后要做的事
Ubuntu 15.10发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- MyEclipse与Mysql数据库的连接
1.载入MySql驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 载入MySql驱动程序 2.建立Connection连接对象 ...
- C# Redis Server分布式缓存编程(二)
在Redis编程中, 实体和集合类型则更加有趣和实用 namespace Zeus.Cache.Redis.Demo { public class Person { public int Id { g ...
- android 删除文件以及递归删除文件夹
private void deleteDirectory(File file) { if (file.isFile()) { file.delete(); return; } if(file.isDi ...
- Linux系统常用命令 __转载的
1.登录linux系统命令:login 用户名 密码: 2.注销linux系统命令:logout ; 3.在linux系统中进入windows系统(图形界面)命令:Start x; 4.关闭lin ...