Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Component/CPAnimationSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions Component/CPAnimationStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 24 additions & 1 deletion Component/private/CPAnimationSequence.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand All @@ -43,6 +58,10 @@ -(void) cancel
}
}

- (void) runAnimated:(BOOL)animated {
[self runAnimated:animated factor:self.factor];
}

#pragma mark - property override

- (void) setDelay:(NSTimeInterval)delay {
Expand All @@ -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 {
Expand Down
22 changes: 17 additions & 5 deletions Component/private/CPAnimationStep.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand All @@ -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) {
Expand All @@ -95,15 +103,19 @@ - (void) runAnimated:(BOOL)animated {
};

if (animated && currentStep.delay) {
[CPAnimationStep runBlock:execution afterDelay:currentStep.delay];
[CPAnimationStep runBlock:execution afterDelay:currentStep.delay * factor];
} else {
execution();
}
}
}

- (void) run {
[self runAnimated:YES];
[self runAnimated:YES];
}

- (void) run:(CGFloat)factor {
[self runAnimated:YES factor:factor];
}

-(void) cancel {
Expand Down