diff --git a/ChangingBackground.xcodeproj/project.pbxproj b/ChangingBackground.xcodeproj/project.pbxproj index 2e16be1..9368cda 100644 --- a/ChangingBackground.xcodeproj/project.pbxproj +++ b/ChangingBackground.xcodeproj/project.pbxproj @@ -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 */; }; + E4152A9B1831256600603A26 /* UIViewController+CBCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = E4152A9A1831256600603A26 /* UIViewController+CBCategory.m */; }; + E4AA9050182E68FC00525168 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4AA904F182E68FC00525168 /* QuartzCore.framework */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -50,6 +52,9 @@ 001809E8171C6764002D3E93 /* blue@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "blue@2x.png"; sourceTree = ""; }; 001809E9171C6764002D3E93 /* green.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = green.png; sourceTree = ""; }; 001809EA171C6764002D3E93 /* green@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "green@2x.png"; sourceTree = ""; }; + E4152A991831256600603A26 /* UIViewController+CBCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+CBCategory.h"; sourceTree = ""; }; + E4152A9A1831256600603A26 /* UIViewController+CBCategory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+CBCategory.m"; sourceTree = ""; }; + E4AA904F182E68FC00525168 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,6 +62,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + E4AA9050182E68FC00525168 /* QuartzCore.framework in Frameworks */, 001809B9171C637E002D3E93 /* UIKit.framework in Frameworks */, 001809BB171C637E002D3E93 /* Foundation.framework in Frameworks */, 001809BD171C637E002D3E93 /* CoreGraphics.framework in Frameworks */, @@ -69,6 +75,8 @@ 001809AC171C637E002D3E93 = { isa = PBXGroup; children = ( + E4152A991831256600603A26 /* UIViewController+CBCategory.h */, + E4152A9A1831256600603A26 /* UIViewController+CBCategory.m */, 001809BE171C637E002D3E93 /* ChangingBackground */, 001809B7171C637E002D3E93 /* Frameworks */, 001809B6171C637E002D3E93 /* Products */, @@ -86,6 +94,7 @@ 001809B7171C637E002D3E93 /* Frameworks */ = { isa = PBXGroup; children = ( + E4AA904F182E68FC00525168 /* QuartzCore.framework */, 001809B8171C637E002D3E93 /* UIKit.framework */, 001809BA171C637E002D3E93 /* Foundation.framework */, 001809BC171C637E002D3E93 /* CoreGraphics.framework */, @@ -241,6 +250,7 @@ 001809C5171C637E002D3E93 /* main.m in Sources */, 001809C9171C637E002D3E93 /* AppDelegate.m in Sources */, 001809D9171C6448002D3E93 /* FirstViewController.m in Sources */, + E4152A9B1831256600603A26 /* UIViewController+CBCategory.m in Sources */, 001809E0171C64E4002D3E93 /* SecondViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -321,6 +331,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "ChangingBackground/ChangingBackground-Prefix.pch"; INFOPLIST_FILE = "ChangingBackground/ChangingBackground-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -332,6 +343,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "ChangingBackground/ChangingBackground-Prefix.pch"; INFOPLIST_FILE = "ChangingBackground/ChangingBackground-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -356,6 +368,7 @@ 001809D4171C637E002D3E93 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ChangingBackground/AppDelegate.m b/ChangingBackground/AppDelegate.m index 9538cc1..7ee44fc 100644 --- a/ChangingBackground/AppDelegate.m +++ b/ChangingBackground/AppDelegate.m @@ -20,7 +20,7 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { window = [UIWindow.alloc initWithFrame:UIScreen.mainScreen.bounds]; [window makeKeyAndVisible]; - + FirstViewController *firstViewController = FirstViewController.new; UINavigationController *navigationController = [UINavigationController.alloc initWithRootViewController:firstViewController]; [navigationController setNavigationBarHidden:YES]; diff --git a/ChangingBackground/FirstViewController.h b/ChangingBackground/FirstViewController.h index a2fae46..fa41d9e 100644 --- a/ChangingBackground/FirstViewController.h +++ b/ChangingBackground/FirstViewController.h @@ -7,6 +7,7 @@ // #import +#import "SecondViewController.h" @interface FirstViewController : UIViewController diff --git a/ChangingBackground/FirstViewController.m b/ChangingBackground/FirstViewController.m index 8aedb68..dc203f4 100644 --- a/ChangingBackground/FirstViewController.m +++ b/ChangingBackground/FirstViewController.m @@ -10,14 +10,79 @@ #import "SecondViewController.h" @interface FirstViewController () +@property (weak, nonatomic) IBOutlet UILabel *homeVCLabel; +@property (weak, nonatomic) IBOutlet UIButton *goForwardButton; @end @implementation FirstViewController +@synthesize goForwardButton; +@synthesize homeVCLabel; + +CGRect labelRect; +CGRect buttonRect; +id observer; +UIImage *transitionImage; +UIImage *originalImage; + + +- (void)simulatePush { + [UIView animateWithDuration:0.2f animations:^{ + goForwardButton.center = CGPointMake(buttonRect.origin.x - 300, buttonRect.origin.y); + homeVCLabel.center = CGPointMake(labelRect.origin.x - 300, labelRect.origin.y); + } completion:^(BOOL finished) { + SecondViewController *secondViewController = SecondViewController.new; + secondViewController.view.backgroundColor = [UIColor clearColor]; + secondViewController.backgroundImage = originalImage; + [self.navigationController pushViewController:secondViewController animated:NO]; + }]; +} + +- (void)preserveUIElementPositions { + // Additional UIElements would need to be added here + buttonRect = goForwardButton.frame; + labelRect = homeVCLabel.frame; +} - (IBAction)goForwardButtonPressed { - SecondViewController *secondViewController = SecondViewController.new; - [self.navigationController pushViewController:secondViewController animated:YES]; + [self simulatePush]; +} + +- (void)changeBackground { + for (UIView *subview in [self.view subviews]) { + if ([subview isKindOfClass:[UIImageView class]]) { + [subview removeFromSuperview]; + } + } + UIImageView *oldImageView = [[UIImageView alloc] initWithImage:transitionImage]; + UIImageView *newImageView = [[UIImageView alloc] initWithImage:originalImage]; + [self.view insertSubview:oldImageView atIndex:0]; + [UIView animateWithDuration:2.0f animations:^{ + oldImageView.alpha = 0.0; + newImageView.alpha = 1.0; + [self.view insertSubview:newImageView atIndex:0]; + }]; +} + +- (void)viewDidAppear:(BOOL)animated { + [UIView animateWithDuration:0.2f animations:^{ + goForwardButton.frame = CGRectMake(buttonRect.origin.x, buttonRect.origin.y, buttonRect.size.width, buttonRect.size.height); + homeVCLabel.frame = CGRectMake(labelRect.origin.x, labelRect.origin.y, labelRect.size.width, labelRect.size.height); + }]; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + originalImage = [UIImage imageNamed:@"blue@2x.png"]; + UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:originalImage]; + [self.view insertSubview:backgroundImageView atIndex:0]; + [self preserveUIElementPositions]; + + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + observer = [nc addObserverForName:@"Go To VC1" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) { + transitionImage = note.object; + [self changeBackground]; + }]; } @end diff --git a/ChangingBackground/FirstViewController.xib b/ChangingBackground/FirstViewController.xib index 0dd58c2..eceb3c2 100644 --- a/ChangingBackground/FirstViewController.xib +++ b/ChangingBackground/FirstViewController.xib @@ -1,217 +1,43 @@ - - - - 1552 - 12C3006 - 3084 - 1187.34 - 625.00 - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 2083 - - - IBProxyObject - IBUIButton - IBUILabel - IBUIView - - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - PluginDependencyRecalculationVersion - - - - - IBFilesOwner - IBCocoaTouchFramework - - - IBFirstResponder - IBCocoaTouchFramework - - - - 274 - - - - 292 - {{77, 115}, {166, 21}} - - - _NS:9 - NO - YES - 7 - NO - IBCocoaTouchFramework - Home View Controller - - 1 - MCAwIDAAA - darkTextColor - - - 0 - - 1 - 17 - - - Helvetica - 17 - 16 - - NO - - - - 292 - {{102, 180}, {109, 44}} - - _NS:9 - NO - IBCocoaTouchFramework - 0 - 0 - 1 - Go Forward - - 3 - MQA - - - 1 - MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA - - - 3 - MC41AA - - - 2 - 15 - - - Helvetica-Bold - 15 - 16 - - - - {{0, 20}, {320, 548}} - - - 3 - MQA - - 2 - - - - - IBUIScreenMetrics - - YES - - - - - - {320, 568} - {568, 320} - - - IBCocoaTouchFramework - Retina 4 Full Screen - 2 - - IBCocoaTouchFramework - - - - - - - view - - - - 3 - - - - goForwardButtonPressed - - - 7 - - 8 - - - - - - 0 - - - - - - 1 - - - - - - - - - -1 - - - File's Owner - - - -2 - - - - - 4 - - - - - 7 - - - - - - - FirstViewController - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - UIResponder - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - - - - 8 - - - 0 - IBCocoaTouchFramework - YES - 3 - 2083 - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ChangingBackground/SecondViewController.h b/ChangingBackground/SecondViewController.h index 9ded32e..bd5ee19 100644 --- a/ChangingBackground/SecondViewController.h +++ b/ChangingBackground/SecondViewController.h @@ -10,6 +10,6 @@ @interface SecondViewController : UIViewController -- (IBAction)goBackButtonPressed; +@property (weak, nonatomic) UIImage *backgroundImage; @end diff --git a/ChangingBackground/SecondViewController.m b/ChangingBackground/SecondViewController.m index 028f811..0b6e654 100644 --- a/ChangingBackground/SecondViewController.m +++ b/ChangingBackground/SecondViewController.m @@ -8,11 +8,58 @@ #import "SecondViewController.h" +@interface SecondViewController () + +@property (weak, nonatomic) IBOutlet UILabel *secondVCLabel; +@property (weak, nonatomic) IBOutlet UIButton *goBackButton; + +@end @implementation SecondViewController +@synthesize goBackButton; +@synthesize secondVCLabel; + +CGRect labelRect; +CGRect buttonRect; - (IBAction)goBackButtonPressed { - [self.navigationController popViewControllerAnimated:YES]; + [self simulatePop]; +} + +- (void)simulatePop { + [UIView animateWithDuration:0.2f animations:^{ + goBackButton.center = CGPointMake(buttonRect.origin.x + 300, buttonRect.origin.y); + secondVCLabel.center = CGPointMake(labelRect.origin.x + 300, labelRect.origin.y); + } completion:^(BOOL finished) { + [[NSNotificationCenter defaultCenter] postNotificationName:@"Go To VC1" object:[UIImage imageNamed:@"green@2x.png"]]; + [self.navigationController popViewControllerAnimated:NO]; + }]; +} + +- (void)viewWillAppear:(BOOL)animated { + buttonRect = goBackButton.frame; + labelRect = secondVCLabel.frame; + goBackButton.frame = CGRectMake(buttonRect.origin.x + 300, buttonRect.origin.y, buttonRect.size.width, buttonRect.size.height); + secondVCLabel.frame = CGRectMake(labelRect.origin.x + 300, labelRect.origin.y, labelRect.size.width, labelRect.size.height); +} + +- (void)changeBackground { + UIImageView *oldImageView = [[UIImageView alloc] initWithImage:self.backgroundImage]; + UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green@2x.png"]]; + [self.view insertSubview:oldImageView atIndex:0]; + [UIView animateWithDuration:2.0f animations:^{ + oldImageView.alpha = 0.0; + newImageView.alpha = 1.0; + [self.view insertSubview:newImageView atIndex:0]; + }]; +} + +- (void)viewDidAppear:(BOOL)animated { + [UIView animateWithDuration:0.2f animations:^{ + goBackButton.frame = buttonRect; + secondVCLabel.frame = labelRect; + }]; + [self changeBackground]; } @end diff --git a/ChangingBackground/SecondViewController.xib b/ChangingBackground/SecondViewController.xib index d603949..21e33d1 100644 --- a/ChangingBackground/SecondViewController.xib +++ b/ChangingBackground/SecondViewController.xib @@ -1,243 +1,43 @@ - - - - 1552 - 12C3006 - 3084 - 1187.34 - 625.00 - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 2083 - - - IBProxyObject - IBUIButton - IBUILabel - IBUIView - - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - PluginDependencyRecalculationVersion - - - - - IBFilesOwner - IBCocoaTouchFramework - - - IBFirstResponder - IBCocoaTouchFramework - - - - 274 - - - - 292 - {{71, 118}, {209, 21}} - - - - _NS:9 - NO - YES - 7 - NO - IBCocoaTouchFramework - Second View Controller - - 1 - MCAwIDAAA - darkTextColor - - - 0 - - 1 - 17 - - - Helvetica - 17 - 16 - - NO - - - - 292 - {{118, 178}, {85, 44}} - - - _NS:9 - NO - IBCocoaTouchFramework - 0 - 0 - 1 - Go Back - - 3 - MQA - - - 1 - MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA - - - 3 - MC41AA - - - 2 - 15 - - - Helvetica-Bold - 15 - 16 - - - - {{0, 20}, {320, 548}} - - - - - 3 - MQA - - 2 - - - - - IBUIScreenMetrics - - YES - - - - - - {320, 568} - {568, 320} - - - IBCocoaTouchFramework - Retina 4 Full Screen - 2 - - IBCocoaTouchFramework - - - - - - - view - - - - 3 - - - - goBackButtonPressed - - - 7 - - 10 - - - - - - 0 - - - - - - 1 - - - - - - - - - -1 - - - File's Owner - - - -2 - - - - - 4 - - - - - 7 - - - - - - - SecondViewController - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - UIResponder - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - - - - 10 - - - - - SecondViewController - UIViewController - - goBackButtonPressed - id - - - goBackButtonPressed - - goBackButtonPressed - id - - - - IBProjectSource - ./Classes/SecondViewController.h - - - - - 0 - IBCocoaTouchFramework - YES - 3 - 2083 - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UIViewController+CBCategory.h b/UIViewController+CBCategory.h new file mode 100644 index 0000000..1b46764 --- /dev/null +++ b/UIViewController+CBCategory.h @@ -0,0 +1,15 @@ +// +// UIViewController+CBCategory.h +// ChangingBackground +// +// Created by Jhaybie on 11/11/13. +// Copyright (c) 2013 Ora Interactive. All rights reserved. +// + +#import + +@interface UIViewController (CBCategory) + ++ (void)changeBackgroundImageOfViewController:(UIViewController *)viewController FromOldImage:(UIImage *)oldBackgroundImage toNewImage:(UIImage *)newBackgroundImage; + +@end diff --git a/UIViewController+CBCategory.m b/UIViewController+CBCategory.m new file mode 100644 index 0000000..c07a2c0 --- /dev/null +++ b/UIViewController+CBCategory.m @@ -0,0 +1,33 @@ +// +// UIViewController+CBCategory.m +// ChangingBackground +// +// Created by Jhaybie on 11/11/13. +// Copyright (c) 2013 Ora Interactive. All rights reserved. +// + +#import "UIViewController+CBCategory.h" + +@implementation UIViewController (CBCategory) + + ++ (void)changeBackgroundImageOfViewController:(UIViewController *)viewController FromOldImage:(UIImage *)oldBackgroundImage toNewImage:(UIImage *)newBackgroundImage { + //THIS LOOP ONLY WORKS WITH THE ASSUMPTION THAT THE VIEWCONTROLLER ONLY USES UIIMAGEVIEW TO CONTAIN THE BACKGROUND IMAGES; + //ALL OTHER UIIMAGEVIEWS IN THE VIEWCONTROLLER WILL BE REMOVED! + for (UIView *subview in [viewController.view subviews]) { + if ([subview isKindOfClass:[UIImageView class]]) { + [subview removeFromSuperview]; + } + } + UIImageView *oldImageView = [[UIImageView alloc] initWithImage:oldBackgroundImage]; + UIImageView *newImageView = [[UIImageView alloc] initWithImage:newBackgroundImage]; + [viewController.view insertSubview:oldImageView atIndex:0]; + [UIView animateWithDuration:2.0f animations:^{ + oldImageView.alpha = 0.0; + newImageView.alpha = 1.0; + [viewController.view insertSubview:newImageView atIndex:0]; + }]; +} + + +@end