动画—Core Animation初步学习四:动画组,一系列动画的组合,能构建出复杂的动画效果
来源:互联网 发布:数据不可信的 英文 编辑:程序博客网 时间:2024/05/18 02:53
在此贴上我学习的博客
http://www.cnblogs.com/kenshincui/p/3972100.html#autoid-3-2-0
动画组
实际开发中一个物体的运动往往是复合运动,单一属性的运动情况比较少,但恰恰属性动画每次进行动画设置时一次只能设置一个属性进行动画控制(不管是基础动画还是关键帧动画都是如此),这样一来要做一个复合运动的动画就必须创建多个属性动画进行组合。对于一两种动画的组合或许处理起来还比较容易,但是对于更多动画的组合控制往往会变得很麻烦,动画组的产生就是基于这样一种情况而产生的。动画组是一系列动画的组合,凡是添加到动画组中的动画都受控于动画组,这样一来各类动画公共的行为就可以统一进行控制而不必单独设置,而且放到动画组中的各个动画可以并发执行,共同构建出复杂的动画效果。
动画组使用起来并不复杂,首先单独创建单个动画(可以是基础动画也可以是关键帧动画),然后将基础动画添加到动画组,最后将动画组添加到图层即可。
前面关键帧动画部分,路径动画看起来效果虽然很流畅,但是落花本身的旋转运动没有了,这里不妨将基础动画部分的旋转动画和路径关键帧动画进行组合使得整个动画看起来更加的和谐、顺畅。
//// 动画组// Animation//// Created by Kenshin Cui on 14-3-22.// Copyright (c) 2014年 Kenshin Cui. All rights reserved.//#import "KCMainViewController.h"@interface KCMainViewController (){ CALayer *_layer;}@end@implementation KCMainViewController- (void)viewDidLoad { [super viewDidLoad]; //设置背景(注意这个图片其实在根图层) UIImage *backgroundImage=[UIImage imageNamed:@"background.jpg"]; self.view.backgroundColor=[UIColor colorWithPatternImage:backgroundImage]; //自定义一个图层 _layer=[[CALayer alloc]init]; _layer.bounds=CGRectMake(0, 0, 10, 20); _layer.position=CGPointMake(50, 150); _layer.contents=(id)[UIImage imageNamed:@"petal.png"].CGImage; [self.view.layer addSublayer:_layer]; //创建动画 [self groupAnimation];}#pragma mark 基础旋转动画-(CABasicAnimation *)rotationAnimation{ CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; CGFloat toValue=M_PI_2*3; basicAnimation.toValue=[NSNumber numberWithFloat:M_PI_2*3];// basicAnimation.duration=6.0; basicAnimation.autoreverses=true; basicAnimation.repeatCount=HUGE_VALF; basicAnimation.removedOnCompletion=NO; [basicAnimation setValue:[NSNumber numberWithFloat:toValue] forKey:@"KCBasicAnimationProperty_ToValue"]; return basicAnimation;}#pragma mark 关键帧移动动画-(CAKeyframeAnimation *)translationAnimation{ CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"]; CGPoint endPoint= CGPointMake(55, 400); CGPathRef path=CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, _layer.position.x, _layer.position.y); CGPathAddCurveToPoint(path, NULL, 160, 280, -30, 300, endPoint.x, endPoint.y); keyframeAnimation.path=path; CGPathRelease(path); [keyframeAnimation setValue:[NSValue valueWithCGPoint:endPoint] forKey:@"KCKeyframeAnimationProperty_EndPosition"]; return keyframeAnimation;}#pragma mark 创建动画组-(void)groupAnimation{ //1.创建动画组 CAAnimationGroup *animationGroup=[CAAnimationGroup animation]; //2.设置组中的动画和其他属性 CABasicAnimation *basicAnimation=[self rotationAnimation]; CAKeyframeAnimation *keyframeAnimation=[self translationAnimation]; animationGroup.animations=@[basicAnimation,keyframeAnimation]; animationGroup.delegate=self; animationGroup.duration=10.0;//设置动画时间,如果动画组中动画已经设置过动画属性则不再生效 animationGroup.beginTime=CACurrentMediaTime()+5;//延迟五秒执行 //3.给图层添加动画 [_layer addAnimation:animationGroup forKey:nil];}#pragma mark - 代理方法#pragma mark 动画完成-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ CAAnimationGroup *animationGroup=(CAAnimationGroup *)anim; CABasicAnimation *basicAnimation=animationGroup.animations[0]; CAKeyframeAnimation *keyframeAnimation=animationGroup.animations[1]; CGFloat toValue=[[basicAnimation valueForKey:@"KCBasicAnimationProperty_ToValue"] floatValue]; CGPoint endPoint=[[keyframeAnimation valueForKey:@"KCKeyframeAnimationProperty_EndPosition"] CGPointValue]; [CATransaction begin]; [CATransaction setDisableActions:YES]; //设置动画最终状态 _layer.position=endPoint; _layer.transform=CATransform3DMakeRotation(toValue, 0, 0, 1); [CATransaction commit];}@end
运行效果:
0 0
- 动画—Core Animation初步学习四:动画组,一系列动画的组合,能构建出复杂的动画效果
- 动画—Core Animation初步学习二: 基础动画
- 动画—Core Animation初步学习三:关键帧动画
- 动画—Core Animation初步学习一:Core Animation动画介绍
- Android 动画效果(二):四种基础动画的 **动态设置、动画监听、组合动画
- 动画—Core Animation初步学习五:转场动画_从一个场景以动画的形式过渡到另一个场景
- iOS中的动画:核心动画Core Animation, UIView动画, Block动画, UIImageView的帧动画.
- Core Animation动画学习3——基本动画、关键帧动画、转场动画 和 动画组
- Animation动画效果的实现
- Animation动画效果的实现
- Animation动画效果的实现
- Animation动画效果的实现
- Animation动画效果的开发
- Animation动画效果的实现
- Animation动画效果的实现
- android的动画效果Animation
- Core Animation动画学习2——自定义CALayer的动画
- CALayer 的简介 和Core Animation动画效果 A
- 记录下读取applicationContext.xml路径的问题
- Node.js的expressjs框架对response的处理流程实验
- 视频如何添加字幕
- 微博 https 请求协议
- iOS开发 - FMDatabase 使用
- 动画—Core Animation初步学习四:动画组,一系列动画的组合,能构建出复杂的动画效果
- Cocoa 新的依赖管理工具:Carthage
- string及vector核心相关知识
- 创还原表空间
- 音乐播放
- 解决内网远程连接MySQL速度慢
- HDU 1495非常可乐(BFS)
- iOS开发 - StoryBoard
- hadoop2.7.1配置备忘