From 6bde19900983ef9636706b7c7e1d955b2c58fc9c Mon Sep 17 00:00:00 2001 From: Aguacero Date: Fri, 12 Sep 2014 18:14:43 +0400 Subject: [PATCH 1/3] Add initialise sequence by array --- Component/CPAnimationSequence.h | 1 + Component/private/CPAnimationSequence.m | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/Component/CPAnimationSequence.h b/Component/CPAnimationSequence.h index aacf295..fa761b4 100644 --- a/Component/CPAnimationSequence.h +++ b/Component/CPAnimationSequence.h @@ -19,6 +19,7 @@ #pragma mark - constructors + (id) sequenceWithSteps:(CPAnimationStep*)first, ... NS_REQUIRES_NIL_TERMINATION; ++ (id) sequenceWithStepsByArray:(NSArray *)steps; #pragma mark - properties diff --git a/Component/private/CPAnimationSequence.m b/Component/private/CPAnimationSequence.m index ea34638..54a3aac 100644 --- a/Component/private/CPAnimationSequence.m +++ b/Component/private/CPAnimationSequence.m @@ -34,6 +34,15 @@ + (id) sequenceWithSteps:(CPAnimationStep*)first, ... { return instance; } ++ (id) sequenceWithStepsByArray:(NSArray *)steps +{ + CPAnimationSequence* instance = [[self alloc] init]; + if (instance) { + instance.steps = [NSArray arrayWithArray:steps]; + } + return instance; +} + -(void) cancel { [ super cancel ]; From 69cb2ce4865896740e9621b7cd9d468ec35d1bee Mon Sep 17 00:00:00 2001 From: Aguacero Date: Fri, 12 Sep 2014 18:33:26 +0400 Subject: [PATCH 2/3] Reverse steps array --- Component/private/CPAnimationSequence.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Component/private/CPAnimationSequence.m b/Component/private/CPAnimationSequence.m index 54a3aac..5a23cd4 100644 --- a/Component/private/CPAnimationSequence.m +++ b/Component/private/CPAnimationSequence.m @@ -38,7 +38,7 @@ + (id) sequenceWithStepsByArray:(NSArray *)steps { CPAnimationSequence* instance = [[self alloc] init]; if (instance) { - instance.steps = [NSArray arrayWithArray:steps]; + instance.steps = [NSArray arrayWithArray:[[steps reverseObjectEnumerator] allObjects]]; } return instance; } From 9183a7d5fd60e99548a0f3d27162ceb1417851c0 Mon Sep 17 00:00:00 2001 From: Aguacero Date: Thu, 18 Sep 2014 11:41:25 +0400 Subject: [PATCH 3/3] Add animation group factor --- Component/CPAnimationSequence.h | 2 ++ Component/CPAnimationStep.h | 2 ++ Component/private/CPAnimationSequence.m | 16 +++++++++++++++- Component/private/CPAnimationStep.m | 22 +++++++++++++++++----- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Component/CPAnimationSequence.h b/Component/CPAnimationSequence.h index fa761b4..4441346 100644 --- a/Component/CPAnimationSequence.h +++ b/Component/CPAnimationSequence.h @@ -20,10 +20,12 @@ + (id) sequenceWithSteps:(CPAnimationStep*)first, ... NS_REQUIRES_NIL_TERMINATION; + (id) sequenceWithStepsByArray:(NSArray *)steps; ++ (id) sequenceWithStepsByArray:(NSArray *)steps factor:(CGFloat)factor; #pragma mark - properties /** Animations steps, from first to last. */ @property (nonatomic, strong, readonly) NSArray* steps; +@property (nonatomic, assign) CGFloat factor; @end diff --git a/Component/CPAnimationStep.h b/Component/CPAnimationStep.h index 4215b05..4426d86 100644 --- a/Component/CPAnimationStep.h +++ b/Component/CPAnimationStep.h @@ -43,8 +43,10 @@ typedef CPAnimationStepBlock AnimationStep __deprecated; /** Starts the step execution. */ - (void) runAnimated:(BOOL)animated; +- (void) runAnimated:(BOOL)animated factor:(CGFloat)factor; /** Shortcut for [step runAnimated:YES] */ - (void) run; +- (void) run:(CGFloat)factor; -(void) cancel; diff --git a/Component/private/CPAnimationSequence.m b/Component/private/CPAnimationSequence.m index 5a23cd4..7bfd435 100644 --- a/Component/private/CPAnimationSequence.m +++ b/Component/private/CPAnimationSequence.m @@ -35,10 +35,16 @@ + (id) sequenceWithSteps:(CPAnimationStep*)first, ... { } + (id) sequenceWithStepsByArray:(NSArray *)steps +{ + return [self sequenceWithStepsByArray:steps factor:1.f]; +} + ++ (id) sequenceWithStepsByArray:(NSArray *)steps factor:(CGFloat)factor { CPAnimationSequence* instance = [[self alloc] init]; if (instance) { instance.steps = [NSArray arrayWithArray:[[steps reverseObjectEnumerator] allObjects]]; + instance.factor = factor; } return instance; } @@ -52,6 +58,10 @@ -(void) cancel } } +- (void) runAnimated:(BOOL)animated { + [self runAnimated:animated factor:self.factor]; +} + #pragma mark - property override - (void) setDelay:(NSTimeInterval)delay { @@ -62,13 +72,17 @@ - (void) setDuration:(NSTimeInterval)duration { NSAssert(NO, @"Setting a duration on a sequence is undefined and therefore disallowed!"); } +- (CGFloat)factor { + return _factor > 0.f ? _factor : 1.f; +} + - (NSTimeInterval) duration { NSTimeInterval fullDuration = 0; for (CPAnimationStep* current in self.animationStepArray) { fullDuration += current.delay; fullDuration += current.duration; } - return fullDuration+self.delay; + return (fullDuration+self.delay) * self.factor; } - (void) setOptions:(UIViewAnimationOptions)options { diff --git a/Component/private/CPAnimationStep.m b/Component/private/CPAnimationStep.m index 0ca5511..911efd6 100644 --- a/Component/private/CPAnimationStep.m +++ b/Component/private/CPAnimationStep.m @@ -60,10 +60,18 @@ - (CPAnimationStepBlock) animationStep:(BOOL)animated { } - (void) runAnimated:(BOOL)animated { + [self runAnimated:animated factor:1.f]; +} + +- (void) runAnimated:(BOOL)animated factor:(CGFloat)factor { if (self.cancelRequested) { return; } + + if (factor <= 0.f) { + factor = 1.f; + } if (!self.consumableSteps) { self.consumableSteps = [[NSMutableArray alloc] initWithArray:[self animationStepArray]]; @@ -78,9 +86,9 @@ - (void) runAnimated:(BOOL)animated { }; CPAnimationStep* currentStep = [self.consumableSteps lastObject]; // Note: do not animate to short steps - if (animated && currentStep.duration >= 0.02) { - [UIView animateWithDuration:currentStep.duration - delay:currentStep.delay + if (animated && currentStep.duration * factor >= 0.02) { + [UIView animateWithDuration:currentStep.duration * factor + delay:currentStep.delay * factor options:currentStep.options animations:[currentStep animationStep:animated] completion:^(BOOL finished) { @@ -95,7 +103,7 @@ - (void) runAnimated:(BOOL)animated { }; if (animated && currentStep.delay) { - [CPAnimationStep runBlock:execution afterDelay:currentStep.delay]; + [CPAnimationStep runBlock:execution afterDelay:currentStep.delay * factor]; } else { execution(); } @@ -103,7 +111,11 @@ - (void) runAnimated:(BOOL)animated { } - (void) run { - [self runAnimated:YES]; + [self runAnimated:YES]; +} + +- (void) run:(CGFloat)factor { + [self runAnimated:YES factor:factor]; } -(void) cancel {