Skip to content

Commit

Permalink
Fix animation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Dec 2, 2015
1 parent e569c50 commit a2c9422
Showing 1 changed file with 63 additions and 64 deletions.
127 changes: 63 additions & 64 deletions Source/AMWaveTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -373,81 +373,80 @@ - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionC
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
// And kick it aside
toVC.view.transform = CGAffineTransformMakeTranslation(delta, 0);

[transitionContext containerView].backgroundColor = fromVC.view.backgroundColor;

// First step is required to trigger the load of the visible cells.
[UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{} completion:^(BOOL finished) {

// Plain animation that moves the destination controller in place. Once it's done it will notify the transition context
if (self.operation == UINavigationControllerOperationPush) {
[toVC.view setTransform:CGAffineTransformMakeTranslation(1, 0)];
[UIView animateWithDuration:self.duration + self.maxDelay delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[toVC.view setTransform:CGAffineTransformIdentity];
} completion:^(BOOL finished2) {
[transitionContext completeTransition:YES];
}];
} else {
[fromVC.view setTransform:CGAffineTransformMakeTranslation(1, 0)];

// Trigger the layout of the new cells
[[transitionContext containerView] layoutIfNeeded];

// Plain animation that moves the destination controller in place. Once it's done it will notify the transition context
if (self.operation == UINavigationControllerOperationPush) {
[toVC.view setTransform:CGAffineTransformMakeTranslation(1, 0)];
[UIView animateWithDuration:self.duration + self.maxDelay delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[toVC.view setTransform:CGAffineTransformIdentity];
[UIView animateWithDuration:self.duration + self.maxDelay delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[fromVC.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
} completion:^(BOOL finished2) {
[fromVC.view removeFromSuperview];
[transitionContext completeTransition:YES];
}];
}

NSArray *fromViews = [self visibleCellsForViewController:fromVC];
NSArray *toViews = [self visibleCellsForViewController:toVC];
} completion:^(BOOL finished2) {
[transitionContext completeTransition:YES];
}];
} else {
[fromVC.view setTransform:CGAffineTransformMakeTranslation(1, 0)];
[toVC.view setTransform:CGAffineTransformIdentity];
[UIView animateWithDuration:self.duration + self.maxDelay delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[fromVC.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
} completion:^(BOOL finished2) {
[fromVC.view removeFromSuperview];
[transitionContext completeTransition:YES];
}];
}

__block NSArray *currentViews;
__block NSUInteger currentVisibleViewsCount;

void (^cellAnimation)(id, NSUInteger, BOOL*) = ^(UIView *view, NSUInteger idx, BOOL *stop){
BOOL fromMode = currentViews == fromViews;
NSTimeInterval delay = ((float)idx / (float)currentVisibleViewsCount) * self.maxDelay;
if (!fromMode) {
[view setTransform:CGAffineTransformMakeTranslation(delta, 0)];
NSArray *fromViews = [self visibleCellsForViewController:fromVC];
NSArray *toViews = [self visibleCellsForViewController:toVC];

__block NSArray *currentViews;
__block NSUInteger currentVisibleViewsCount;

void (^cellAnimation)(id, NSUInteger, BOOL*) = ^(UIView *view, NSUInteger idx, BOOL *stop){
BOOL fromMode = currentViews == fromViews;
NSTimeInterval delay = ((float)idx / (float)currentVisibleViewsCount) * self.maxDelay;
if (!fromMode) {
[view setTransform:CGAffineTransformMakeTranslation(delta, 0)];
}
void (^animation)() = ^{
if (fromMode) {
view.transform = CGAffineTransformMakeTranslation(-delta, 0);
view.alpha = 0;
} else {
view.transform = CGAffineTransformIdentity;
view.alpha = 1;
}
void (^animation)() = ^{
if (fromMode) {
view.transform = CGAffineTransformMakeTranslation(-delta, 0);
view.alpha = 0;
} else {
view.transform = CGAffineTransformIdentity;
view.alpha = 1;
}
};
void (^completion)(BOOL) = ^(BOOL finished2){
if (fromMode) {
[view setTransform:CGAffineTransformIdentity];
}
};
if (self.transitionType == AMWaveTransitionTypeSubtle) {
[UIView animateWithDuration:self.duration delay:delay options:UIViewAnimationOptionCurveEaseIn animations:animation completion:completion];
} else if (self.transitionType == AMWaveTransitionTypeNervous) {
[UIView animateWithDuration:self.duration delay:delay usingSpringWithDamping:0.75 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseIn animations:animation completion:completion];
} else if (self.transitionType == AMWaveTransitionTypeBounce){
[UIView animateWithDuration:self.duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:animation completion:completion];
};
void (^completion)(BOOL) = ^(BOOL finished2){
if (fromMode) {
[view setTransform:CGAffineTransformIdentity];
}
};


currentViews = fromViews;
NSArray *viewsArrays = @[fromViews, toViews];

for (currentViews in viewsArrays) {
// Animates all views
currentVisibleViewsCount = currentViews.count;
[currentViews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:cellAnimation];
if (self.transitionType == AMWaveTransitionTypeSubtle) {
[UIView animateWithDuration:self.duration delay:delay options:UIViewAnimationOptionCurveEaseIn animations:animation completion:completion];
} else if (self.transitionType == AMWaveTransitionTypeNervous) {
[UIView animateWithDuration:self.duration delay:delay usingSpringWithDamping:0.75 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseIn animations:animation completion:completion];
} else if (self.transitionType == AMWaveTransitionTypeBounce){
[UIView animateWithDuration:self.duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:animation completion:completion];
}
}];
};


currentViews = fromViews;
NSArray *viewsArrays = @[fromViews, toViews];

for (currentViews in viewsArrays) {
// Animates all views
currentVisibleViewsCount = currentViews.count;
[currentViews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:cellAnimation];
}
}

- (NSArray *)visibleCellsForViewController:(UIViewController*)viewController {
NSArray *visibleCells = nil;

if ([viewController respondsToSelector:@selector(visibleCells)]) {
visibleCells = ((UIViewController<AMWaveTransitioning>*)viewController).visibleCells;
} else if ([viewController respondsToSelector:@selector(tableView)]) {
Expand Down

0 comments on commit a2c9422

Please sign in to comment.