iOS 动画组

时间:2021-07-24 18:09:13

  其实早在一个多月以前就已经实现了动作组播放的功能,不过当时感觉好像没有什么难度并没有放在心上,今天突然要用到动画组,发现已经忘记了,所以又将原来的代码翻出来看了下。最后决定还是写下来,以备不时之需。动画组播放很简单,使用的是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