Skip to content

Commit

Permalink
Added new 'cards' animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinEberhardt committed Sep 22, 2013
1 parent a387009 commit 86c0e0d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
13 changes: 13 additions & 0 deletions AnimationControllers/CECardsAnimationController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CEZoomAnimationController.h
// TransitionsDemo
//
// Created by Colin Eberhardt on 22/09/2013.
// Copyright (c) 2013 Colin Eberhardt. All rights reserved.
//

#import "CEReversibleAnimationController.h"

@interface CECardsAnimationController : CEReversibleAnimationController

@end
123 changes: 123 additions & 0 deletions AnimationControllers/CECardsAnimationController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// CEZoomAnimationController.m
// TransitionsDemo
//
// Created by Colin Eberhardt on 22/09/2013.
// Copyright (c) 2013 Colin Eberhardt. All rights reserved.
//

#import "CECardsAnimationController.h"

@implementation CECardsAnimationController



- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {

if(self.reverse){
[self executeReverseAnimation:transitionContext fromVC:fromVC toVC:toVC fromView:fromView toView:toView];
} else {
[self executeForwardsAnimation:transitionContext fromVC:fromVC toVC:toVC fromView:fromView toView:toView];
}

}

-(void)executeForwardsAnimation:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {

UIView* containerView = [transitionContext containerView];

// positions the to- view off the bottom of the sceen
CGRect offScreenFrame = containerView.frame;
offScreenFrame.origin.y = containerView.frame.size.height;
toView.frame = offScreenFrame;

[containerView insertSubview:toView aboveSubview:fromView];

CATransform3D t1 = [self firstTransform];
CATransform3D t2 = [self secondTransformWithView:fromView];

[UIView animateKeyframesWithDuration:self.duration delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{

// push the from- view to the back
[UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.4f animations:^{
fromView.layer.transform = t1;
fromView.alpha = 0.6;
}];
[UIView addKeyframeWithRelativeStartTime:0.2f relativeDuration:0.4f animations:^{
fromView.layer.transform = t2;
}];

// slide the to- view upwards. In his original implementation Tope used a 'spring' animation, however
// this does not work with keyframes, so we siulate it by overshooting the final location in
// the first keyframe
[UIView addKeyframeWithRelativeStartTime:0.6f relativeDuration:0.2f animations:^{
toView.frame = CGRectOffset(containerView.frame, 0.0, -30.0);
}];
[UIView addKeyframeWithRelativeStartTime:0.8f relativeDuration:0.2f animations:^{
toView.frame = containerView.frame;
}];

} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];


}

-(void)executeReverseAnimation:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {

UIView* containerView = [transitionContext containerView];

// positions the to- view behind the from- view
toView.frame = containerView.frame;
CATransform3D scale = CATransform3DIdentity;
toView.layer.transform = CATransform3DScale(scale, 0.6, 0.6, 1);
toView.alpha = 0.6;

[containerView insertSubview:toView belowSubview:fromView];

CGRect frameOffScreen = containerView.frame;
frameOffScreen.origin.y = containerView.frame.size.height;

CATransform3D t1 = [self firstTransform];

[UIView animateKeyframesWithDuration:self.duration delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{

// push the from- view off the bottom of the screen
[UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.5f animations:^{
fromView.frame = frameOffScreen;
}];

// animate the to- view into place
[UIView addKeyframeWithRelativeStartTime:0.3f relativeDuration:0.45f animations:^{
toView.layer.transform = t1;
toView.alpha = 1.0;
}];
[UIView addKeyframeWithRelativeStartTime:0.75f relativeDuration:0.25f animations:^{
toView.layer.transform = CATransform3DIdentity;
}];
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}

-(CATransform3D)firstTransform{
CATransform3D t1 = CATransform3DIdentity;
t1.m34 = 1.0/-900;
t1 = CATransform3DScale(t1, 0.95, 0.95, 1);
t1 = CATransform3DRotate(t1, 15.0f * M_PI/180.0f, 1, 0, 0);
return t1;

}

-(CATransform3D)secondTransformWithView:(UIView*)view{

CATransform3D t2 = CATransform3DIdentity;
t2.m34 = [self firstTransform].m34;
t2 = CATransform3DTranslate(t2, 0, view.frame.size.height*-0.08, 0);
t2 = CATransform3DScale(t2, 0.8, 0.8, 1);

return t2;
}

@end
6 changes: 6 additions & 0 deletions TransitionsDemo/TransitionsDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
72C15E1417E10E7B0056B3F9 /* CEExplodeAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72C15E1317E10E7B0056B3F9 /* CEExplodeAnimationController.m */; };
72C15E1717E19A940056B3F9 /* CEFoldAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72C15E1617E19A940056B3F9 /* CEFoldAnimationController.m */; };
72EC05A917E7B1BE00DCB9A3 /* CEPinchInteractionController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EC05A817E7B1BE00DCB9A3 /* CEPinchInteractionController.m */; };
72EE96D817EF47A60097DF82 /* CECardsAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -81,6 +82,8 @@
72C15E1617E19A940056B3F9 /* CEFoldAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CEFoldAnimationController.m; sourceTree = "<group>"; };
72EC05A717E7B1BE00DCB9A3 /* CEPinchInteractionController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CEPinchInteractionController.h; sourceTree = "<group>"; };
72EC05A817E7B1BE00DCB9A3 /* CEPinchInteractionController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CEPinchInteractionController.m; sourceTree = "<group>"; };
72EE96D617EF47A60097DF82 /* CECardsAnimationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CECardsAnimationController.h; sourceTree = "<group>"; };
72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CECardsAnimationController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -182,6 +185,8 @@
72C15E1317E10E7B0056B3F9 /* CEExplodeAnimationController.m */,
72C15E1517E19A940056B3F9 /* CEFoldAnimationController.h */,
72C15E1617E19A940056B3F9 /* CEFoldAnimationController.m */,
72EE96D617EF47A60097DF82 /* CECardsAnimationController.h */,
72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */,
);
name = AnimationControllers;
path = ../AnimationControllers;
Expand Down Expand Up @@ -311,6 +316,7 @@
7273237017DFC4710072C7FD /* SettingsViewController.m in Sources */,
7273235F17DFBEBF0072C7FD /* CEFlipAnimationController.m in Sources */,
7273236317DFBEFE0072C7FD /* CEReversibleAnimationController.m in Sources */,
72EE96D817EF47A60097DF82 /* CECardsAnimationController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion TransitionsDemo/TransitionsDemo/SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ @implementation SettingsViewController {

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
_animationControllers = @[@"None", @"Fold", @"Explode", @"Flip", @"Turn", @"Crossfade"];
_animationControllers = @[@"None", @"Cards", @"Fold", @"Explode", @"Flip", @"Turn", @"Crossfade"];
_interactionControllers = @[@"None", @"Swipe", @"Pinch"];
}
return self;
Expand Down

0 comments on commit 86c0e0d

Please sign in to comment.