Skip to content

Commit 010da80

Browse files
committed
Add NatGeo-inspited transition
1 parent fa17dfe commit 010da80

File tree

4 files changed

+169
-1
lines changed

4 files changed

+169
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// CENatGeoAnimationController.h
3+
// TransitionsDemo
4+
//
5+
// Created by Paweł Wrzosek on 22.10.2013.
6+
// Copyright (c) 2013 Colin Eberhardt. All rights reserved.
7+
//
8+
9+
#import "CEReversibleAnimationController.h"
10+
11+
/**
12+
NatGeo-inspired transition adopted to iOS7 API.
13+
@see https://github.com/michaelhenry/MHNatGeoViewControllerTransition
14+
*/
15+
@interface CENatGeoAnimationController : CEReversibleAnimationController
16+
17+
@end
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//
2+
// CENatGeoAnimationController.m
3+
// TransitionsDemo
4+
//
5+
// Created by Paweł Wrzosek on 22.10.2013.
6+
// Copyright (c) 2013 Colin Eberhardt. All rights reserved.
7+
//
8+
9+
#import "CENatGeoAnimationController.h"
10+
11+
static const CGFloat kAnimationFirstPartRatio = 0.8f;
12+
13+
@implementation CENatGeoAnimationController
14+
15+
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
16+
// Grab the from and to view controllers from the context
17+
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
18+
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
19+
20+
[transitionContext.containerView addSubview:fromViewController.view];
21+
[transitionContext.containerView addSubview:toViewController.view];
22+
23+
CALayer *fromLayer;
24+
CALayer *toLayer;
25+
26+
if (self.reverse) {
27+
toViewController.view.userInteractionEnabled = YES;
28+
29+
fromLayer = toViewController.view.layer;
30+
toLayer = fromViewController.view.layer;
31+
32+
// Reset to initial transform
33+
sourceLastTransform(fromLayer);
34+
destinationLastTransform(toLayer);
35+
36+
37+
//Perform animation
38+
[UIView animateWithDuration:kAnimationFirstPartRatio * self.duration
39+
delay:(1.0f - kAnimationFirstPartRatio) * self.duration
40+
options:UIViewAnimationOptionCurveLinear animations:^{
41+
destinationFirstTransform(toLayer);
42+
} completion:nil];
43+
44+
[UIView animateWithDuration:self.duration
45+
delay:0.0f
46+
options:0
47+
animations:^{
48+
sourceFirstTransform(fromLayer);
49+
} completion:^(BOOL finished) {
50+
CGRect oldFrame = [fromLayer frame];
51+
[fromLayer setAnchorPoint:CGPointMake(0.5f, 0.5f)];
52+
[fromLayer setFrame:oldFrame];
53+
54+
[transitionContext completeTransition:YES];
55+
}];
56+
57+
} else {
58+
fromViewController.view.userInteractionEnabled = NO;
59+
60+
fromLayer = fromViewController.view.layer;
61+
toLayer = toViewController.view.layer;
62+
63+
// Change anchor point and reposition it.
64+
CGRect oldFrame = fromLayer.frame;
65+
[fromLayer setAnchorPoint:CGPointMake(0.0f, 0.5f)];
66+
[fromLayer setFrame:oldFrame];
67+
68+
// Reset to initial transform
69+
sourceFirstTransform(fromLayer);
70+
destinationFirstTransform(toLayer);
71+
72+
//Perform animation
73+
[UIView animateWithDuration:self.duration
74+
delay:0.0f
75+
options:0
76+
animations:^{
77+
destinationLastTransform(toLayer);
78+
} completion:nil];
79+
80+
[UIView animateWithDuration:kAnimationFirstPartRatio * self.duration
81+
delay:(1.0f - kAnimationFirstPartRatio) * self.duration
82+
options:0
83+
animations:^{
84+
sourceLastTransform(fromLayer);
85+
}
86+
completion:^(BOOL finished) {
87+
[transitionContext completeTransition:YES];
88+
}];
89+
}
90+
91+
}
92+
93+
#pragma mark -
94+
95+
#pragma mark - Required 3d Transform
96+
static void sourceFirstTransform(CALayer *layer) {
97+
CATransform3D t = CATransform3DIdentity;
98+
t.m34 = 1.0 / -500;
99+
t = CATransform3DTranslate(t, 0.0f, 0.0f, 0.0f);
100+
layer.transform = t;
101+
}
102+
103+
static void sourceLastTransform(CALayer *layer) {
104+
CATransform3D t = CATransform3DIdentity;
105+
t.m34 = 1.0 / -500.0f;
106+
t = CATransform3DRotate(t, radianFromDegree(80), 0.0f, 1.0f, 0.0f);
107+
t = CATransform3DTranslate(t, 0.0f, 0.0f, -30.0f);
108+
t = CATransform3DTranslate(t, 170.0f, 0.0f, 0.0f);
109+
layer.transform = t;
110+
}
111+
112+
static void destinationFirstTransform(CALayer * layer) {
113+
CATransform3D t = CATransform3DIdentity;
114+
t.m34 = 1.0f / -500.0f;
115+
// Rotate 5 degrees within the axis of z axis
116+
t = CATransform3DRotate(t, radianFromDegree(5.0f), 0.0f, 0.0f, 1.0f);
117+
// Reposition toward to the left where it initialized
118+
t = CATransform3DTranslate(t, 320.0f, -40.0f, 150.0f);
119+
// Rotate it -45 degrees within the y axis
120+
t = CATransform3DRotate(t, radianFromDegree(-45), 0.0f, 1.0f, 0.0f);
121+
// Rotate it 10 degrees within thee x axis
122+
t = CATransform3DRotate(t, radianFromDegree(10), 1.0f, 0.0f, 0.0f);
123+
layer.transform = t;
124+
}
125+
126+
static void destinationLastTransform(CALayer * layer) {
127+
CATransform3D t = CATransform3DIdentity;
128+
t.m34 = 1.0/ -500;
129+
// Rotate to 0 degrees within z axis
130+
t = CATransform3DRotate(t, radianFromDegree(0), 0.0f, 0.0f, 1.0f);
131+
// Bring back to the final position
132+
t = CATransform3DTranslate(t, 0.0f, 0.0f, 0.0f);
133+
// Rotate 0 degrees within y axis
134+
t = CATransform3DRotate(t, radianFromDegree(0), 0.0f, 1.0f, 0.0f);
135+
// Rotate 0 degrees within x axis
136+
t = CATransform3DRotate(t, radianFromDegree(0), 1.0f, 0.0f, 0.0f);
137+
layer.transform = t;
138+
}
139+
140+
#pragma mark - Convert Degrees to Radian
141+
static double radianFromDegree(float degrees) {
142+
return (degrees / 180) * M_PI;
143+
}
144+
145+
@end

TransitionsDemo/TransitionsDemo.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
72EC05A917E7B1BE00DCB9A3 /* CEPinchInteractionController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EC05A817E7B1BE00DCB9A3 /* CEPinchInteractionController.m */; };
3333
72EE96D817EF47A60097DF82 /* CECardsAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */; };
3434
72EE96DB17EF66040097DF82 /* CEVerticalSwipeInteractionController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EE96DA17EF66040097DF82 /* CEVerticalSwipeInteractionController.m */; };
35+
7D0D046C1816DBA700F289A6 /* CENatGeoAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D0D046B1816DBA700F289A6 /* CENatGeoAnimationController.m */; };
3536
/* End PBXBuildFile section */
3637

3738
/* Begin PBXContainerItemProxy section */
@@ -87,6 +88,8 @@
8788
72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CECardsAnimationController.m; sourceTree = "<group>"; };
8889
72EE96D917EF66040097DF82 /* CEVerticalSwipeInteractionController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CEVerticalSwipeInteractionController.h; sourceTree = "<group>"; };
8990
72EE96DA17EF66040097DF82 /* CEVerticalSwipeInteractionController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CEVerticalSwipeInteractionController.m; sourceTree = "<group>"; };
91+
7D0D046A1816DBA700F289A6 /* CENatGeoAnimationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CENatGeoAnimationController.h; sourceTree = "<group>"; };
92+
7D0D046B1816DBA700F289A6 /* CENatGeoAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CENatGeoAnimationController.m; sourceTree = "<group>"; };
9093
/* End PBXFileReference section */
9194

9295
/* Begin PBXFrameworksBuildPhase section */
@@ -190,6 +193,8 @@
190193
72C15E1617E19A940056B3F9 /* CEFoldAnimationController.m */,
191194
72EE96D617EF47A60097DF82 /* CECardsAnimationController.h */,
192195
72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */,
196+
7D0D046A1816DBA700F289A6 /* CENatGeoAnimationController.h */,
197+
7D0D046B1816DBA700F289A6 /* CENatGeoAnimationController.m */,
193198
);
194199
name = AnimationControllers;
195200
path = ../AnimationControllers;
@@ -317,6 +322,7 @@
317322
7273232C17DFBD4D0072C7FD /* main.m in Sources */,
318323
7273236017DFBEBF0072C7FD /* CETurnAnimationController.m in Sources */,
319324
7273236A17DFBF140072C7FD /* CEHorizontalSwipeInteractionController.m in Sources */,
325+
7D0D046C1816DBA700F289A6 /* CENatGeoAnimationController.m in Sources */,
320326
7273236D17DFC0690072C7FD /* NavigationController.m in Sources */,
321327
7273237017DFC4710072C7FD /* SettingsViewController.m in Sources */,
322328
72EE96DB17EF66040097DF82 /* CEVerticalSwipeInteractionController.m in Sources */,

TransitionsDemo/TransitionsDemo/SettingsViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ @implementation SettingsViewController {
2323

2424
- (id)initWithCoder:(NSCoder *)aDecoder {
2525
if (self = [super initWithCoder:aDecoder]) {
26-
_animationControllers = @[@"None", @"Cards", @"Fold", @"Explode", @"Flip", @"Turn", @"Crossfade"];
26+
_animationControllers = @[@"None", @"Cards", @"Fold", @"Explode", @"Flip", @"Turn", @"Crossfade", @"NatGeo"];
2727
_interactionControllers = @[@"None", @"HorizontalSwipe", @"VerticalSwipe", @"Pinch"];
2828
}
2929
return self;

0 commit comments

Comments
 (0)