我用QuartzCode App生成了动画代码。 我有4个文件:
如何在AppDelegate.m中添加此动画,以便在定义启动画面后生成棘手的启动画面动画?
CustomView.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CustomView : UIView
- (void)addJumpAnimation;
- (void)addJumpAnimationCompletionBlock:(void (^)(BOOL finished))completionBlock;
- (void)addJumpHighAnimation;
- (void)addJumpHighAnimationCompletionBlock:(void (^)(BOOL finished))completionBlock;
- (void)removeAnimationsForAnimationId:(NSString *)identifier;
- (void)removeAllAnimations;
@end
CustomView.m
#import "CustomView.h"
#import "QCMethod.h"
@interface CustomView ()
@property (nonatomic, strong) NSMutableDictionary * layers;
@property (nonatomic, strong) NSMapTable * completionBlocks;
@property (nonatomic, assign) BOOL updateLayerValueForCompletedAnimation;
@end
@implementation CustomView
#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupProperties];
[self setupLayers];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setupProperties];
[self setupLayers];
}
return self;
}
- (void)setupProperties{
self.completionBlocks = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsOpaqueMemory valueOptions:NSPointerFunctionsStrongMemory];;
self.layers = [NSMutableDictionary dictionary];
}
- (void)setupLayers{
CALayer * ggk = [CALayer layer];
ggk.frame = CGRectMake(100, 100, 100, 100);
[self.layer addSublayer:ggk];
self.layers[@"ggk"] = ggk;
[self resetLayerPropertiesForLayerIdentifiers:nil];
}
- (void)resetLayerPropertiesForLayerIdentifiers:(NSArray *)layerIds{
[CATransaction begin];
[CATransaction setDisableActions:YES];
if(!layerIds || [layerIds containsObject:@"ggk"]){
CALayer * ggk = self.layers[@"ggk"];
ggk.contents = (id)[UIImage imageNamed:@"ggk"].CGImage;
}
[CATransaction commit];
}
#pragma mark - Animation Setup
- (void)addJumpAnimation{
[self addJumpAnimationCompletionBlock:nil];
}
- (void)addJumpAnimationCompletionBlock:(void (^)(BOOL finished))completionBlock{
if (completionBlock){
CABasicAnimation * completionAnim = [CABasicAnimation animationWithKeyPath:@"completionAnim"];;
completionAnim.duration = 2.5;
completionAnim.delegate = self;
[completionAnim setValue:@"Jump" forKey:@"animId"];
[completionAnim setValue:@(NO) forKey:@"needEndAnim"];
[self.layer addAnimation:completionAnim forKey:@"Jump"];
[self.completionBlocks setObject:completionBlock forKey:[self.layer animationForKey:@"Jump"]];
}
NSString * fillMode = kCAFillModeForwards;
////ggk animation
CAKeyframeAnimation * ggkOpacityAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
ggkOpacityAnim.values = @[@1, @0];
ggkOpacityAnim.keyTimes = @[@0, @1];
ggkOpacityAnim.duration = 0.5;
ggkOpacityAnim.beginTime = 2;
CAKeyframeAnimation * ggkPositionAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
ggkPositionAnim.values = @[[NSValue valueWithCGPoint:CGPointMake(150, 150)], [NSValue valueWithCGPoint:CGPointMake(150, 100)], [NSValue valueWithCGPoint:CGPointMake(150, 150)], [NSValue valueWithCGPoint:CGPointMake(150, 100)], [NSValue valueWithCGPoint:CGPointMake(150, 150)], [NSValue valueWithCGPoint:CGPointMake(150, 100)], [NSValue valueWithCGPoint:CGPointMake(150, 150)], [NSValue valueWithCGPoint:CGPointMake(150, 100)], [NSValue valueWithCGPoint:CGPointMake(150, 150)], [NSValue valueWithCGPoint:CGPointMake(150, 0)]];
ggkPositionAnim.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @1];
ggkPositionAnim.duration = 2.5;
CAAnimationGroup * ggkJumpAnim = [QCMethod groupAnimations:@[ggkOpacityAnim, ggkPositionAnim] fillMode:fillMode];
[self.layers[@"ggk"] addAnimation:ggkJumpAnim forKey:@"ggkJumpAnim"];
}
- (void)addJumpHighAnimation{
[self addJumpHighAnimationCompletionBlock:nil];
}
- (void)addJumpHighAnimationCompletionBlock:(void (^)(BOOL finished))completionBlock{
if (completionBlock){
CABasicAnimation * completionAnim = [CABasicAnimation animationWithKeyPath:@"completionAnim"];;
completionAnim.duration = 3;
completionAnim.delegate = self;
[completionAnim setValue:@"JumpHigh" forKey:@"animId"];
[completionAnim setValue:@(NO) forKey:@"needEndAnim"];
[self.layer addAnimation:completionAnim forKey:@"JumpHigh"];
[self.completionBlocks setObject:completionBlock forKey:[self.layer animationForKey:@"JumpHigh"]];
}
NSString * fillMode = kCAFillModeForwards;
////ggk animation
CAKeyframeAnimation * ggkPositionAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
ggkPositionAnim.values = @[[NSValue valueWithCGPoint:CGPointMake(150, 150)], [NSValue valueWithCGPoint:CGPointMake(150, -300)]];
ggkPositionAnim.keyTimes = @[@0, @1];
ggkPositionAnim.duration = 1;
ggkPositionAnim.beginTime = 2;
CAAnimationGroup * ggkJumpHighAnim = [QCMethod groupAnimations:@[ggkPositionAnim] fillMode:fillMode];
[self.layers[@"ggk"] addAnimation:ggkJumpHighAnim forKey:@"ggkJumpHighAnim"];
}
#pragma mark - Animation Cleanup
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
void (^completionBlock)(BOOL) = [self.completionBlocks objectForKey:anim];;
if (completionBlock){
[self.completionBlocks removeObjectForKey:anim];
if ((flag && self.updateLayerValueForCompletedAnimation) || [[anim valueForKey:@"needEndAnim"] boolValue]){
[self updateLayerValuesForAnimationId:[anim valueForKey:@"animId"]];
[self removeAnimationsForAnimationId:[anim valueForKey:@"animId"]];
}
completionBlock(flag);
}
}
- (void)updateLayerValuesForAnimationId:(NSString *)identifier{
if([identifier isEqualToString:@"Jump"]){
[QCMethod updateValueFromPresentationLayerForAnimation:[self.layers[@"ggk"] animationForKey:@"ggkJumpAnim"] theLayer:self.layers[@"ggk"]];
}
else if([identifier isEqualToString:@"JumpHigh"]){
[QCMethod updateValueFromPresentationLayerForAnimation:[self.layers[@"ggk"] animationForKey:@"ggkJumpHighAnim"] theLayer:self.layers[@"ggk"]];
}
}
- (void)removeAnimationsForAnimationId:(NSString *)identifier{
if([identifier isEqualToString:@"Jump"]){
[self.layers[@"ggk"] removeAnimationForKey:@"ggkJumpAnim"];
}
else if([identifier isEqualToString:@"JumpHigh"]){
[self.layers[@"ggk"] removeAnimationForKey:@"ggkJumpHighAnim"];
}
}
- (void)removeAllAnimations{
[self.layers enumerateKeysAndObjectsUsingBlock:^(id key, CALayer *layer, BOOL *stop) {
[layer removeAllAnimations];
}];
}
@end