iOS 动画组
其实早在一个多月以前就已经实现了动作组播放的功能,不过当时感觉好像没有什么难度并没有放在心上,今天突然要用到动画组,发现已经忘记了,所以又将原来的代码翻出来看了下。最后决定还是写下来,以备不时之需。动画组播放很简单,使用的是CAAnimationGroup这个类,将不同的动画添加到里面进行播放。通常可以添加的动画有:1、keyPath,它用来绘制路径,直线或者曲线,从理论上讲任意的线都可以绘制;2、旋转动画,它可以绕x、y、z轴进行旋转;3、缩放动画。
废话不多说,直接上代码。
#define pathAnimation @"position"
#define transformX @"transform.rotation.x"
#define transformY @"transform.rotation.y"
#define transformZ @"transform.rotation.z"
#define scaleAnimation @"position scale"
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h> //NSString *const pathAnimation = @"position"; //运动轨迹
//NSString *const transformX = @"transform.rotation.x"; //绕x轴旋转
//NSString *const transformY = @"transform.rotation.y"; //绕y轴旋转
//NSString *const transformZ = @"transform.rotation.z"; //绕z轴旋转
//NSString *const scaleAnimation = @"position scale"; //缩放 typedef enum AnimationType{
KeyPathAnimation, //运动轨迹为抛物线
RotationAnimationX, //绕x轴旋转
RotationAnimationY, //绕y轴旋转
RotationAnimationZ, //绕z轴旋转
ScaleAnimation //缩放
}AnimationType; @protocol LMFThowingLineToolDelegate <NSObject> - (void)animationDidFinish; @end @interface LMFThowingLineTool : NSObject
@property (nonatomic, strong) NSMutableArray *typeArray;//用来存储动画类型
@property (nonatomic, strong) NSArray *pointArray;
@property (nonatomic, strong) CAKeyframeAnimation *keyFrameAnimation;
@property (nonatomic, strong) CABasicAnimation *basicAnimation;
@property (nonatomic, strong) UIView *showView;
@property (nonatomic, assign) CGFloat duration;
@property (nonatomic, assign) id<LMFThowingLineToolDelegate> delegate;
- (void)setPointArray:(NSArray *)pointArray;
- (void)setDuration:(CGFloat)duration;
- (void)setKeyFrameAnimation;
- (void)setBasicAnimationWith:(AnimationType)animationType;
- (void)thowingView:(UIView *)view;
- (void)setTransform:(CGFloat)angle and:(AnimationType)animationType;
@end
实现类:
#import "LMFThowingLineTool.h"
#import "LMFPoint.h" @interface LMFThowingLineTool () @end @implementation LMFThowingLineTool - (void)dealloc
{
self.basicAnimation = nil;
self.keyFrameAnimation = nil;
NSLog(@"%@", self.basicAnimation);
} - (instancetype)init
{
if (self = [super init])
{
self.typeArray = [NSMutableArray array];
}
return self;
}
- (void)setPointArray:(NSArray *)pointArray
{
_pointArray = pointArray;
}
- (void)setDuration:(CGFloat)duration
{
_duration = duration;
}
- (void)setKeyFrameAnimation
{
if ([self.pointArray count] == )
{
return;
}
CGMutablePathRef path = CGPathCreateMutable(); if ([self.pointArray count] == )
{
LMFPoint * startPoint = (LMFPoint *)self.pointArray[];
LMFPoint * centerPoint = (LMFPoint *)self.pointArray[];
LMFPoint * endPoint = (LMFPoint *)self.pointArray[];
CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
//这里用的直线的路径,也可以设置抛物线,获取曲线,可进行自定义
CGPathAddQuadCurveToPoint(path, NULL, centerPoint.x, centerPoint.y, endPoint.x, endPoint.y);
}
else if ([self.pointArray count] > )
{
return;
}
self.keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:pathAnimation];
self.keyFrameAnimation.path = path;
CFRelease(path);
[self.typeArray addObject:self.keyFrameAnimation];
}
- (void)setTransform:(CGFloat)angle and:(AnimationType)animationType
{ }
- (void)setBasicAnimationWith:(AnimationType)animationType
{
self.basicAnimation = [[CABasicAnimation alloc] init];
self.basicAnimation.autoreverses = YES;
self.basicAnimation.repeatCount = MAXFLOAT;
self.basicAnimation.duration = self.duration;
switch (animationType) {
case RotationAnimationX:
self.basicAnimation.keyPath = transformX;
break;
case RotationAnimationY:
self.basicAnimation.keyPath = transformY;
break;
case RotationAnimationZ:
self.basicAnimation.keyPath = transformZ;
break;
case ScaleAnimation:
self.basicAnimation.keyPath = scaleAnimation;
self.basicAnimation.toValue = [NSNumber numberWithFloat:(CGFloat)((arc4random() % ) + ) / 10.0];
[self.typeArray addObject:self.basicAnimation];
break;
default:
break;
}
self.basicAnimation.fromValue = [NSNumber numberWithFloat:0.0];
int intNumber = arc4random()%+;
self.basicAnimation.toValue = [NSNumber numberWithFloat:intNumber*M_PI];
[self.typeArray addObject:self.basicAnimation];
} #pragma mark --开始执行动画
- (void)thowingView:(UIView *)view
{
self.showView = view;
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
// groupAnimation.delegate = self;
groupAnimation.repeatCount = ;
groupAnimation.duration = self.duration;
groupAnimation.removedOnCompletion = NO;
groupAnimation.animations = self.typeArray;
[view.layer addAnimation:groupAnimation forKey:@"position scale"];
} - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[self.showView removeFromSuperview];
self.showView = nil;
} @end
额外需要的类:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface LMFPoint : NSObject
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
- (instancetype)initWith:(CGFloat)x and:(CGFloat)y;
@end #import "LMFPoint.h" @implementation LMFPoint
- (instancetype)initWith:(CGFloat)x and:(CGFloat)y
{
if (self = [super init])
{
self.x = x;
self.y = y;
}
return self;
} - (NSString *)description
{
return [NSString stringWithFormat:@"x:%f y:%f", self.x, self.y];
} @end
iOS 动画组的更多相关文章
- ios基础动画、关键帧动画、动画组、转场动画等
概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...
- IOS第18天(9,核心动画-动画组)
****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...
- iOS:核心动画之动画组CAAnimationGroup
CAAnimationGroup——动画组 动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行 属性说明: ...
- IOS-用动画组制作花瓣掉落效果(另附iOS动画图表)
重要的两个方法:1.动画的数组:animations 2.启动的时间 beginTime 注意:动画组设置了持续时间(duration)可能会导致动画组里面的持续时间不管用 代码如下: #import ...
- IOS开发核心动画六:动画组
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- iOS动画实现总结
在iOS中,动画实现方向有两种,一种是操作UIView的animation方法,另外一种就是核心动画,但到iOS7中,UIView又跟核心动画牵扯在一起. 方式一(利用核心动画添加动画) 核心动画的层 ...
- IOS 动画专题 --iOS核心动画
iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看 ...
- iOS 动画篇 之 Core Animation (一)
iOS中实现动画有两种方式,一种是自己不断的通过drawRect:方法来绘制,另外一种就是使用核心动画(Core Animation). 导语: 核心动画提供高帧速率和流畅的动画,而不会增加CPU的负 ...
- IOS动画(Core Animation)总结 (参考多方文章)
一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...
随机推荐
- 基于MDK的mbed工程建立
个人更喜欢mdk作为IDE来编写代码,而mbed作为一个开源项目,有大量优秀代码可以借鉴使用,今后一段时间都会主要看mbed平台的代码以及国内ebox平台代码 1 首先登陆mbed在 ...
- 学习笔记:MySQL操作初步
对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...
- SQL2008代理作业出现错误: c001f011维护计划创建失败的解决方法
SQL2008数据库总会出现从 IClassFactory 为 CLSID 为 {17BCA6E8-A95D-497E-B2F9-AF6AA475916F} 的 COM 组件创建实例失败,原因是出现以 ...
- (Interface)接口特点
接口是一种规范.只要一个类继承了一个接口,这个类就必须实现这个接口中所有的成员 为了多态. 接口不能被实例化.也就是说,接口不能new(不能创建对象) 接口中的成员不能加"访问修饰符&quo ...
- Java数据结构之回溯算法的递归应用迷宫的路径问题
一.简介 回溯法的基本思想是:对一个包括有很多结点,每个结点有若干个搜索分支的问题,把原问题分解为对若干个子问题求解的算法.当搜索到某个结点.发现无法再继续搜索下去时,就让搜索过程回溯(即退回)到该结 ...
- sqlite嵌入式数据库C语言基本操作(1)
sqlite嵌入式数据库C语言基本操作(1) :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0, ...
- 树上倍增LCA模版
void dfs(int u){ ;i = edge.next){ int to = dege[i].to; ]) continue; d[to] = d[u]+; dis[to] = dis[u]+ ...
- The Same Game-POJ1027模拟
The Same Game Time Limit: 1000MS Memory Limit: 10000K Description The game named "Same" is ...
- 开发工具&环境
远程拷贝:scp cdh4.tar.gz root@10.239.44.111 ~ gerrit for code review: git add . git commit -a git push o ...
- R语言解读多元线性回归模型
转载:http://blog.fens.me/r-multi-linear-regression/ 前言 本文接上一篇R语言解读一元线性回归模型.在许多生活和工作的实际问题中,影响因变量的因素可能不止 ...