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
21 changes: 21 additions & 0 deletions ChangingBackground.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
001809EC171C6764002D3E93 /* blue@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 001809E8171C6764002D3E93 /* blue@2x.png */; };
001809ED171C6764002D3E93 /* green.png in Resources */ = {isa = PBXBuildFile; fileRef = 001809E9171C6764002D3E93 /* green.png */; };
001809EE171C6764002D3E93 /* green@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 001809EA171C6764002D3E93 /* green@2x.png */; };
DF1528581941206000770B11 /* TransitioningDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DF1528571941206000770B11 /* TransitioningDelegate.m */; };
DF15285B194120A100770B11 /* AnimatedTransitioning.m in Sources */ = {isa = PBXBuildFile; fileRef = DF15285A194120A100770B11 /* AnimatedTransitioning.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -50,6 +52,10 @@
001809E8171C6764002D3E93 /* blue@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "blue@2x.png"; sourceTree = "<group>"; };
001809E9171C6764002D3E93 /* green.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = green.png; sourceTree = "<group>"; };
001809EA171C6764002D3E93 /* green@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "green@2x.png"; sourceTree = "<group>"; };
DF1528561941206000770B11 /* TransitioningDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TransitioningDelegate.h; sourceTree = "<group>"; };
DF1528571941206000770B11 /* TransitioningDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TransitioningDelegate.m; sourceTree = "<group>"; };
DF152859194120A100770B11 /* AnimatedTransitioning.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AnimatedTransitioning.h; sourceTree = "<group>"; };
DF15285A194120A100770B11 /* AnimatedTransitioning.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnimatedTransitioning.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -98,6 +104,7 @@
children = (
001809EF171C6768002D3E93 /* BackgroundImages */,
001809D5171C63FC002D3E93 /* AppDelegate */,
DF1528551941201C00770B11 /* Transitioning */,
001809DC171C6453002D3E93 /* ViewControllers */,
001809BF171C637E002D3E93 /* Supporting Files */,
);
Expand Down Expand Up @@ -167,6 +174,17 @@
name = BackgroundImages;
sourceTree = "<group>";
};
DF1528551941201C00770B11 /* Transitioning */ = {
isa = PBXGroup;
children = (
DF1528561941206000770B11 /* TransitioningDelegate.h */,
DF1528571941206000770B11 /* TransitioningDelegate.m */,
DF152859194120A100770B11 /* AnimatedTransitioning.h */,
DF15285A194120A100770B11 /* AnimatedTransitioning.m */,
);
name = Transitioning;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -238,9 +256,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DF1528581941206000770B11 /* TransitioningDelegate.m in Sources */,
001809C5171C637E002D3E93 /* main.m in Sources */,
001809C9171C637E002D3E93 /* AppDelegate.m in Sources */,
001809D9171C6448002D3E93 /* FirstViewController.m in Sources */,
DF15285B194120A100770B11 /* AnimatedTransitioning.m in Sources */,
001809E0171C64E4002D3E93 /* SecondViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -356,6 +376,7 @@
001809D4171C637E002D3E93 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
15 changes: 15 additions & 0 deletions ChangingBackground/AnimatedTransitioning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AnimatedTransitioning.h
// ChangingBackground
//
// Created by Christopher Gu on 6/5/14.
// Copyright (c) 2014 Ora Interactive. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface AnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>

@property (nonatomic) BOOL reverse;

@end
64 changes: 64 additions & 0 deletions ChangingBackground/AnimatedTransitioning.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// AnimatedTransitioning.m
// ChangingBackground
//
// Created by Christopher Gu on 6/5/14.
// Copyright (c) 2014 Ora Interactive. All rights reserved.
//

#import "AnimatedTransitioning.h"
#import "AppDelegate.h"

static NSTimeInterval const AnimatedTransitionDuration = 0.5f;

@implementation AnimatedTransitioning


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *container = [transitionContext containerView];

[container addSubview:toViewController.view];

if (self.reverse) {
[UIView animateWithDuration:AnimatedTransitionDuration animations:^{
toViewController.view.frame = CGRectMake(-320, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
fromViewController.view.frame = CGRectMake(320, 0, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
} completion:^(BOOL finished) {
[transitionContext completeTransition:finished];

[UIView transitionWithView:appDelegate.window duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
appDelegate.window.backgroundColor = [UIColor colorWithPatternImage:appDelegate.blueBackground];
} completion:^(BOOL finished) {

}];
}];
}
else {
[UIView animateWithDuration:AnimatedTransitionDuration animations:^{
toViewController.view.frame = CGRectMake(320, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
fromViewController.view.frame = CGRectMake(-320, 0, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
} completion:^(BOOL finished) {
[transitionContext completeTransition:finished];

[UIView transitionWithView:appDelegate.window duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
appDelegate.window.backgroundColor = [UIColor colorWithPatternImage:appDelegate.greenBackground];
} completion:^(BOOL finished) {

}];
}];
}
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return AnimatedTransitionDuration;
}

@end
3 changes: 3 additions & 0 deletions ChangingBackground/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIImage *blueBackground;
@property (strong, nonatomic) UIImage *greenBackground;

@end
34 changes: 27 additions & 7 deletions ChangingBackground/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,47 @@
//

#import "AppDelegate.h"
#import "TransitioningDelegate.h"
#import "FirstViewController.h"

@interface AppDelegate () {
UIWindow *window;
}
@property (nonatomic, strong) TransitioningDelegate *transitioningDelegate;

@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [UIWindow.alloc initWithFrame:UIScreen.mainScreen.bounds];
[window makeKeyAndVisible];
self.window = [UIWindow.alloc initWithFrame:UIScreen.mainScreen.bounds];
[self.window makeKeyAndVisible];

FirstViewController *firstViewController = FirstViewController.new;
UINavigationController *navigationController = [UINavigationController.alloc initWithRootViewController:firstViewController];
[navigationController setNavigationBarHidden:YES];

window.rootViewController = navigationController;

self.transitioningDelegate = [TransitioningDelegate new];
firstViewController.transitioningDelegate = self.transitioningDelegate;
self.window.rootViewController = firstViewController;

self.blueBackground = [self imageWithImage:[UIImage imageNamed:@"blue"] scaledToSize:CGSizeMake(320, 568)];
self.greenBackground = [self imageWithImage:[UIImage imageNamed:@"green"] scaledToSize:CGSizeMake(320, 568)];

self.window.backgroundColor = [UIColor colorWithPatternImage:self.blueBackground];

return YES;
}

// the image sizes are too short for 4-inch
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

@end

13 changes: 11 additions & 2 deletions ChangingBackground/FirstViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@
#import "SecondViewController.h"

@interface FirstViewController ()

{
IBOutlet UIButton *goForwardButton;
}
@end

@implementation FirstViewController

-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor clearColor];
goForwardButton.layer.cornerRadius = 5.0;
}

- (IBAction)goForwardButtonPressed {
SecondViewController *secondViewController = SecondViewController.new;
[self.navigationController pushViewController:secondViewController animated:YES];
secondViewController.transitioningDelegate = self.transitioningDelegate;
[self presentViewController:secondViewController animated:YES completion:nil];
}

@end
Loading