CABasicAnimation是基于CALayer的动画,有各种形式的动画,这种动画已经趋于底层了,所以灵活性很强。
添加CoreGraphics.framework,QuartzCore.framework,类库。
延展,可在视频的layer层增加一些动画或者贴纸:
#import <Foundation/Foundation.h> // #if defined(NS_STRING_ENUM) // Available starting in Xcode 8.0. #define SRICKER_STRING_ENUM NS_STRING_ENUM #else #define SRICKER_STRING_ENUM #endif typedef NSString *StickerAnimationType SRICKER_STRING_ENUM; typedef NS_ENUM(NSInteger, StickerTransformLocal) { StickerTransformAll,///x.y StickerTransformX, ///x. StickerTransformY, ///y. StickerTransformZ, ///z. }; @interface StickerDefault : NSObject //透明度 extern StickerAnimationType const StickerAnimationByOpacity; //位置 extern StickerAnimationType const StickerAnimationByPosition; //变形 extern StickerAnimationType const StickerAnimationByTransform; //缩放 extern StickerAnimationType const StickerAnimationByTransformScale; //旋转 extern StickerAnimationType const StickerAnimationByTransformRotation; //大小 extern StickerAnimationType const StickerAnimationByBounds; @end
// // CALayer+Animation.h // MRCTestDemo // // Created by Admin on 2018/4/11. // Copyright © 2018年 LianDongTech. All rights reserved. // #import <QuartzCore/QuartzCore.h> #import <AVFoundation/AVFoundation.h> #import <UIKit/UIKit.h> #import "StickerDefault.h" @interface CALayer (Animation) //透明 - (void)opacityAnimationFromValue:(CGFloat)fromValue toValue:(CGFloat)tovalue repeatCount:(float)repeatCount duration:(float)duration; //旋转 - (void)rotation:(float)duration fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue direction:(StickerTransformLocal)local repeatCount:(float)repeatCount; //缩放 - (void)scale:(float)duration fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue direction:(StickerTransformLocal)local repeatCount:(float)repeatCount; //大小 - (void)boundsFromRect:(CGRect)fromRect toRect:(CGRect)toRect atDuration:(float)duration AtDirectionLocal:(StickerTransformLocal)local repeatCount:(float)repeatCount; //位置 - (void)position:(CGPoint)point degree:(float)degree direction:(StickerTransformLocal)local repeatCount:(float)repeatCount; //变换 - (void)transform:(float)duration fromValue:(CATransform3D)fromValue toValue:(CATransform3D)toValue repeatCount:(float)repeatCount; @end
// // CALayer+Animation.m // MRCTestDemo // // Created by Admin on 2018/4/11. // Copyright © 2018年 LianDongTech. All rights reserved. // #import "CALayer+Animation.h" @implementation CALayer (Animation) - (void)opacityAnimationFromValue:(CGFloat)fromValue toValue:(CGFloat)tovalue repeatCount:(float)repeatCount duration:(float)duration { CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:StickerAnimationByOpacity]; //动画时间 animation.duration=duration; //动画重复次数 animation.repeatCount=repeatCount; //回退动画 //animation.autoreverses=YES; //保存动画后的效果 animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; // animate from fully visible to invisible animation.fromValue=[NSNumber numberWithFloat:fromValue]; animation.toValue=[NSNumber numberWithFloat:tovalue]; animation.beginTime = AVCoreAnimationBeginTimeAtZero; [self addAnimation:animation forKey:@"animateOpacity"]; } - (void)rotation:(float)duration fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue direction:(StickerTransformLocal)local repeatCount:(float)repeatCount { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:[self finalLocalStickerAnimationType:StickerAnimationByTransformRotation stickerTransformLocal:local]]; animation.duration = duration; animation.repeatCount = repeatCount; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; // animation.autoreverses=YES; // rotate from 0 to 360 animation.fromValue=[NSNumber numberWithFloat:fromValue]; //toValue=2.0为360度 animation.toValue=[NSNumber numberWithFloat:(toValue * M_PI)]; animation.beginTime = AVCoreAnimationBeginTimeAtZero; [self addAnimation:animation forKey:@"animateRotation"]; } - (void)scale:(float)duration fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue direction:(StickerTransformLocal)local repeatCount:(float)repeatCount { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:[self finalLocalStickerAnimationType:StickerAnimationByTransformScale stickerTransformLocal:local]]; animation.duration = duration; animation.repeatCount = repeatCount; // animation.autoreverses=YES; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; // animate from half size to full size animation.fromValue=[NSNumber numberWithFloat:fromValue]; animation.toValue=[NSNumber numberWithFloat:toValue]; animation.beginTime = AVCoreAnimationBeginTimeAtZero; [self addAnimation:animation forKey:@"animateScale"]; } - (void)boundsFromRect:(CGRect)fromRect toRect:(CGRect)toRect atDuration:(float)duration AtDirectionLocal:(StickerTransformLocal)local repeatCount:(float)repeatCount { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:[self finalLocalStickerAnimationType:StickerAnimationByBounds stickerTransformLocal:local]]; animation.duration = duration; animation.repeatCount = repeatCount; // animation.autoreverses=YES; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; // animate from half size to full size animation.fromValue=[NSValue valueWithCGRect:fromRect]; animation.toValue=[NSValue valueWithCGRect:toRect]; animation.beginTime = AVCoreAnimationBeginTimeAtZero; [self addAnimation:animation forKey:@"animateBounds"]; } - (void)position:(CGPoint)point degree:(float)degree direction:(StickerTransformLocal)local repeatCount:(float)repeatCount { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:[self finalLocalStickerAnimationType:StickerAnimationByPosition stickerTransformLocal:local]]; animation.duration = degree; animation.repeatCount = repeatCount; // animation.autoreverses=YES; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; // animate from half size to full size animation.fromValue = [NSValue valueWithCGPoint:self.position]; animation.toValue=[NSValue valueWithCGPoint:point]; animation.beginTime = AVCoreAnimationBeginTimeAtZero; [self addAnimation:animation forKey:@"animatePosition"]; } //CATransform3D - (void)transform:(float)duration fromValue:(CATransform3D)fromValue toValue:(CATransform3D)toValue repeatCount:(float)repeatCount { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:StickerAnimationByTransform]; animation.duration = duration; animation.repeatCount = repeatCount; // animation.autoreverses=YES; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; // animate from half size to full size animation.fromValue = [NSValue valueWithCATransform3D:fromValue]; animation.toValue=[NSValue valueWithCATransform3D:toValue]; animation.beginTime = AVCoreAnimationBeginTimeAtZero; [self addAnimation:animation forKey:@"animatePosition"]; } // - (NSString *)finalLocalStickerAnimationType:(StickerAnimationType)type stickerTransformLocal:(StickerTransformLocal)local { NSArray *types = @[type, [NSString stringWithFormat:@"%@.x",type], [NSString stringWithFormat:@"%@.y",type], [NSString stringWithFormat:@"%@.z",type], ]; return (NSString *)types[local]; } @end