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平台上负责图形渲染与动画的 ...
随机推荐
- iOS 推送小记
ios做推送功能时,最烦得就是各种证书的问题,以前自己做的时候经常要反复搞那些证书搞好几遍才能成功,现在发现归根到底都是appid这个东西搞错了,做个笔记记下来,以免忘了. 首先是程序里面注册推送的变 ...
- The constructor BASE64Encoder() is not accessible due to restriction on required library
在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示:Access restriction : T ...
- 获取浏览器信息 c#
Request.Browser.MajorVersion.ToString();//获取客户端浏览器的版本号 Request.Browser.Version.ToString();//获取客户端浏览器 ...
- web app响应式字体设置!rem之我见
之前做沙漠教育的时候,直接以设计图为准,然后强暴式,缩放处理.简单.直接,粗暴!但是,开发快.……一劳永逸! 但那是,现在开发,作为业界良心:是不能那么做的!(那个是被逼的啊 首先看代码: @medi ...
- 安卓版App开发心得
从2016年4月到6月主要做的工作是网站的开发,而6月到现在2016年8月初,主要做的工作是Android和IOS两种App的开发,又以Android为主. 将这段时间的Android开发心得记录如下 ...
- DataGrid新增行数据
本文将介绍一下,如何通过Jquery MiniUI来添加Datagrid一行. 1.效果展示: ↓ 2.具体代码: <script type="text/javascript" ...
- 005_重写 Standard Delete Button
以后会用JS直接删除,但是在加载.js时候出现问题,会在以后进一步追踪完善: <apex:page standardController="Opportunity" > ...
- GDB的深入研究
GDB的深入研究 一.GDB代码调试 (一)GDB调试实例 在终端中编译一个示例C语言小程序,保存为文件 gdblianxi.c 中,用GCC编译. 在上面的命令行中,使用-o参数指定了编译生成的可执 ...
- EditText光标颜色设置
又一次做应用,发现EditText没有显示光标,借鉴了网上的方法,发现是因为光标是白色的,与背景一样造成的,这里记录一下如何设置EditText等的光标颜色: 需要在布局文件中指定androd:tex ...
- Bootstrap相关总结
1.工具提示 Tooltips js调用显示 $('#example').tooltip( { title:'这个是一个提示', }); $('#example').tooltip('show');