diff --git a/Component/CPAnimationSequence.h b/Component/CPAnimationSequence.h index aacf295..4441346 100644 --- a/Component/CPAnimationSequence.h +++ b/Component/CPAnimationSequence.h @@ -19,10 +19,13 @@ #pragma mark - constructors + (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 ea34638..7bfd435 100644 --- a/Component/private/CPAnimationSequence.m +++ b/Component/private/CPAnimationSequence.m @@ -34,6 +34,21 @@ + (id) sequenceWithSteps:(CPAnimationStep*)first, ... { return instance; } ++ (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; +} + -(void) cancel { [ super cancel ]; @@ -43,6 +58,10 @@ -(void) cancel } } +- (void) runAnimated:(BOOL)animated { + [self runAnimated:animated factor:self.factor]; +} + #pragma mark - property override - (void) setDelay:(NSTimeInterval)delay { @@ -53,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 {