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平台上负责图形渲染与动画的 ...
随机推荐
- win2012,oracle11g,sqlplus切换实例的方法
问题环境:windows 2012 r2 64位 ,oracle 11.2.0.4,多个实例. 在这种情况下, sqlplus "/as sysdba" 默认登录的是系统后面安装 ...
- (二)sql入门 管理数据库对象
在数据库里,有各种各样的对象,除了最常见的表之外,还有诸如视图.索引等数据库对象. 这些对象,在数据库里需要有人来管理,那么谁来管理呢?当然是数据库的使用者了.每个使用者相对于数据库里有一片区域,称为 ...
- OO Design
什么是设计原则? 设计原则是基本的工具,应用这些规则可以使你的代码更加灵活.更容易维护.更容易扩展.基本原则:封装变化Encapsulate what varies.面向接口变成而不是实现 Code ...
- javascript的escape()方法
escape() 方法:采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码 ...
- Android数字签名
描述你对Android数字签名的理解? 数字签名就是为程序打上一个标记来作为自己的标识,是Android系统要求的,若一个Android程序没有经过数字签名,没办法安装到系统: 签名也是对apk的一种 ...
- Spring学习笔记之五----Spring MVC
Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...
- [T-SQL]INSERT INTO SELECT 与 SELECT INTO FROM
1.INSERT INTO SELECT 语法:INSERT INTO Table2(field1,field2,...) select value1,value2,... from Table1 要 ...
- Configure Visual Studio 2013 for debugging .NET framework
https://referencesource.microsoft.com/ In order to configure Visual Studio 2013 do the following in ...
- 使用dom4j操作XML
DOM4J介绍 DOM4J是使用Java语言编写的,用于读写及操作XML的一套组件,DOM4J同时具有DOM修改文件的优点和SAX读取快速的优点. DOM4J的使用 首先下载dom4j的JAR包,我用 ...
- C语言-两个库函数
两个库函数 --1-- printf函数 1.1 printf 函数的介绍 1.2 格式控制字符串 1.3 %f输出精度的问题 1.4 printf 函数使用注意事项 --2-- scanf函数 2. ...