From ebc5a9572efcb515fff90ab40350aa1c246c7b7b Mon Sep 17 00:00:00 2001 From: Joan Lluch Date: Wed, 16 Jul 2014 23:37:54 +0200 Subject: [PATCH] state restoration --- .../AppDelegate.m | 13 +- .../ColorViewController.m | 43 ++++-- .../MapViewController.m | 35 +++-- .../en.lproj/MainStoryboard-iPad.storyboard | 2 +- .../en.lproj/MainStoryboard.storyboard | 12 +- .../SWRevealViewController.h | 9 +- .../SWRevealViewController.m | 142 +++++++++++------- 7 files changed, 162 insertions(+), 94 deletions(-) diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m index 55dc092..8e06b17 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m @@ -44,17 +44,20 @@ - (void)applicationWillTerminate:(UIApplication *)application } #pragma mark state preservation / restoration -- (BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder { + +- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder +{ return YES; } -- (BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder { +- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder +{ return YES; } -- (BOOL) application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - - [self.window makeKeyAndVisible]; +- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + //[self.window makeKeyAndVisible]; return YES; } @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m index 0ef9fc1..d364a3d 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m @@ -17,40 +17,57 @@ @implementation ColorViewController - (void)viewDidLoad { [super viewDidLoad]; + [self customSetup]; +} + - [self.revealButtonItem setTarget: self.revealViewController]; - [self.revealButtonItem setAction: @selector( revealToggle: )]; - [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer]; +- (void)customSetup +{ + SWRevealViewController *revealViewController = self.revealViewController; + if ( revealViewController ) + { + [self.revealButtonItem setTarget: revealViewController]; + [self.revealButtonItem setAction: @selector( revealToggle: )]; + [self.navigationController.navigationBar addGestureRecognizer:revealViewController.panGestureRecognizer]; + } _label.text = _text; _label.textColor = _color; } + #pragma mark state preservation / restoration -- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { + +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder +{ NSLog(@"%s", __PRETTY_FUNCTION__); - // TODO save what you need here - [coder encodeObject: _label.text forKey: @"lableText"]; + // Save what you need here + [coder encodeObject: _text forKey: @"text"]; + [coder encodeObject: _color forKey: @"color"]; [super encodeRestorableStateWithCoder:coder]; } -- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder +{ NSLog(@"%s", __PRETTY_FUNCTION__); - // TODO restore what you need here - _label.text = [coder decodeObjectForKey: @"labelText"]; + // Restore what you need here + _color = [coder decodeObjectForKey: @"color"]; + _text = [coder decodeObjectForKey: @"text"]; [super decodeRestorableStateWithCoder:coder]; } -- (void)applicationFinishedRestoringState { + +- (void)applicationFinishedRestoringState +{ NSLog(@"%s", __PRETTY_FUNCTION__); - - // TODO call whatever function you need to visually restore - + // Call whatever function you need to visually restore + [self customSetup]; } @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m index e1bf9c6..ba9f855 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m @@ -17,33 +17,48 @@ @implementation MapViewController - (void)viewDidLoad { [super viewDidLoad]; - - [self.revealButtonItem setTarget: self.revealViewController]; - [self.revealButtonItem setAction: @selector( revealToggle: )]; - [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer]; + [self customSetup]; +} + +- (void)customSetup +{ + SWRevealViewController *revealViewController = self.revealViewController; + if ( revealViewController ) + { + [self.revealButtonItem setTarget: self.revealViewController]; + [self.revealButtonItem setAction: @selector( revealToggle: )]; + [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer]; + } } #pragma mark state preservation / restoration -- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { + +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder +{ NSLog(@"%s", __PRETTY_FUNCTION__); - // TODO save what you need here + // Save what you need here [super encodeRestorableStateWithCoder:coder]; } -- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder +{ NSLog(@"%s", __PRETTY_FUNCTION__); - // TODO restore what you need here + // Restore what you need here [super decodeRestorableStateWithCoder:coder]; } -- (void)applicationFinishedRestoringState { + +- (void)applicationFinishedRestoringState +{ NSLog(@"%s", __PRETTY_FUNCTION__); - // TODO call whatever function you need to visually restore + // Call whatever function you need to visually restore + [self customSetup]; } @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard index c9deb5f..9f45ecb 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard @@ -211,7 +211,7 @@ - + diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard.storyboard b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard.storyboard index aa83630..7224f31 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard.storyboard +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard.storyboard @@ -8,7 +8,7 @@ - + @@ -21,7 +21,7 @@ - + @@ -36,7 +36,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -90,7 +90,7 @@ - + @@ -180,7 +180,7 @@ - + diff --git a/SWRevealViewController/SWRevealViewController.h b/SWRevealViewController/SWRevealViewController.h index 90b7368..2d19468 100755 --- a/SWRevealViewController/SWRevealViewController.h +++ b/SWRevealViewController/SWRevealViewController.h @@ -28,7 +28,12 @@ RELEASE NOTES - - New properties 'toggleAnimationType', 'springDampingRatio' + + Version 2.2.0 + + - State restoration support. + - Reverted panGestureRecognizer implementation to before v2.1.0 (works better). + - New properties 'toggleAnimationType', 'springDampingRatio'. Default reveal animation is 'Spring' Version 2.1.1 (Current Version) @@ -167,7 +172,7 @@ typedef NS_ENUM( NSInteger, FrontViewPosition) typedef NS_ENUM(NSInteger, SWRevealToggleAnimationType) { SWRevealToggleAnimationTypeSpring, // <- produces a spring based animation - SWRevealToggleAnimationTypeEaseOut, // <- produces a simple ease out curve animation + SWRevealToggleAnimationTypeEaseOut, // <- produces an ease out curve animation }; diff --git a/SWRevealViewController/SWRevealViewController.m b/SWRevealViewController/SWRevealViewController.m index 9863940..8f3eeb0 100755 --- a/SWRevealViewController/SWRevealViewController.m +++ b/SWRevealViewController/SWRevealViewController.m @@ -29,8 +29,6 @@ Early code inspired on a similar class by Philip Kluz (Philip.Kluz@zuui.org) #import "SWRevealViewController.h" - - #pragma mark - StatusBar Helper Function // computes the required offset adjustment due to the status bar for the passed in view, @@ -678,13 +676,17 @@ - (UIViewController *)childViewControllerForStatusBarHidden - (void)loadView { // Do not call super, to prevent the apis from unfruitful looking for inexistent xibs! + //[super loadView]; + + // load any defined front/rear controllers from the storyboard before + [self loadStoryboardControllers]; - // This is what Apple tells us to set as the initial frame, which is of course totally irrelevant - // with the modern view controller containment patterns, let's leave it for the sake of it! - //CGRect frame = [[UIScreen mainScreen] applicationFrame]; + // This is what Apple used to tell us to set as the initial frame, which is of course totally irrelevant + // with view controller containment patterns, let's leave it for the sake of it! + // CGRect frame = [[UIScreen mainScreen] applicationFrame]; // On iOS7 the applicationFrame does not return the whole screen. This is possibly a bug. - // As a workaround we use the screen bounds, this still works on iOS6 + // As a workaround we use the screen bounds, this still works on iOS6, any zero based frame would work anyway! CGRect frame = [[UIScreen mainScreen] bounds]; // create a custom content view for the controller @@ -699,9 +701,6 @@ - (void)loadView // set our contentView to the controllers view self.view = _contentView; - // load any defined front/rear controllers from the storyboard - [self loadStoryboardControllers]; - // Apple also tells us to do this: _contentView.backgroundColor = [UIColor blackColor]; @@ -1748,14 +1747,16 @@ - (void)loadStoryboardControllers } } + #pragma mark state preservation / restoration -+ (UIViewController*) viewControllerWithRestorationIdentifierPath:(NSArray*) identifierComponents coder:(NSCoder*)coder { - NSLog(@"%s", __PRETTY_FUNCTION__); - - SWRevealViewController* vc; + ++ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder*)coder +{ + SWRevealViewController* vc = nil; UIStoryboard* sb = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey]; - if (sb) { + if (sb) + { vc = (SWRevealViewController*)[sb instantiateViewControllerWithIdentifier:@"SWRevealViewController"]; vc.restorationIdentifier = [identifierComponents lastObject]; vc.restorationClass = [SWRevealViewController class]; @@ -1763,57 +1764,88 @@ + (UIViewController*) viewControllerWithRestorationIdentifierPath:(NSArray*) ide return vc; } -- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { - // this will include the other view controllers in state encoding and decoding - if (_rearViewController) { - [coder encodeObject: _rearViewController forKey: @"rearViewController"]; - } - - if (_frontViewController) { - [coder encodeObject: _frontViewController forKey: @"frontViewController"]; - } - - if (_rightViewController) { - [coder encodeObject: _rightViewController forKey: @"rightViewController"]; - } - - // ??? What's needed to restore what position the views are in - [coder encodeInt: _frontViewPosition forKey: @"frontViewPosition"]; - [coder encodeInt: _rearViewPosition forKey: @"rearViewPosition"]; - [coder encodeInt: _rightViewPosition forKey: @"rightViewPosition"]; +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder +{ + [coder encodeDouble:_rearViewRevealWidth forKey:@"_rearViewRevealWidth"]; + [coder encodeDouble:_rearViewRevealOverdraw forKey:@"_rearViewRevealOverdraw"]; + [coder encodeDouble:_rearViewRevealDisplacement forKey:@"_rearViewRevealDisplacement"]; + [coder encodeDouble:_rightViewRevealWidth forKey:@"_rightViewRevealWidth"]; + [coder encodeDouble:_rightViewRevealOverdraw forKey:@"_rightViewRevealOverdraw"]; + [coder encodeDouble:_rightViewRevealDisplacement forKey:@"_rightViewRevealDisplacement"]; + [coder encodeBool:_bounceBackOnOverdraw forKey:@"_bounceBackOnOverdraw"]; + [coder encodeBool:_bounceBackOnLeftOverdraw forKey:@"_bounceBackOnLeftOverdraw"]; + [coder encodeBool:_stableDragOnOverdraw forKey:@"_stableDragOnOverdraw"]; + [coder encodeBool:_stableDragOnLeftOverdraw forKey:@"_stableDragOnLeftOverdraw"]; + [coder encodeBool:_presentFrontViewHierarchically forKey:@"_presentFrontViewHierarchically"]; + [coder encodeDouble:_quickFlickVelocity forKey:@"_quickFlickVelocity"]; + [coder encodeDouble:_toggleAnimationDuration forKey:@"_toggleAnimationDuration"]; + [coder encodeInteger:_toggleAnimationType forKey:@"_toggleAnimationType"]; + [coder encodeDouble:_springDampingRatio forKey:@"_springDampingRatio"]; + [coder encodeDouble:_replaceViewAnimationDuration forKey:@"_replaceViewAnimationDuration"]; + [coder encodeDouble:_frontViewShadowRadius forKey:@"_frontViewShadowRadius"]; + [coder encodeCGSize:_frontViewShadowOffset forKey:@"_frontViewShadowOffset"]; + [coder encodeDouble:_frontViewShadowOpacity forKey:@"_frontViewShadowOpacity"]; + [coder encodeObject:_frontViewShadowColor forKey:@"_frontViewShadowColor"]; + [coder encodeBool:_userInteractionStore forKey:@"_userInteractionStore"]; + [coder encodeDouble:_draggableBorderWidth forKey:@"_draggableBorderWidth"]; + [coder encodeBool:_clipsViewsToBounds forKey:@"_clipsViewsToBounds"]; + [coder encodeBool:_extendsPointInsideHit forKey:@"_extendsPointInsideHit"]; + + [coder encodeObject:_rearViewController forKey:@"_rearViewController"]; + [coder encodeObject:_frontViewController forKey:@"_frontViewController"]; + [coder encodeObject:_rightViewController forKey:@"_rightViewController"]; + + [coder encodeInteger:_frontViewPosition forKey:@"_frontViewPosition"]; [super encodeRestorableStateWithCoder:coder]; } -- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder +{ + _rearViewRevealWidth = [coder decodeDoubleForKey:@"_rearViewRevealWidth"]; + _rearViewRevealOverdraw = [coder decodeDoubleForKey:@"_rearViewRevealOverdraw"]; + _rearViewRevealDisplacement = [coder decodeDoubleForKey:@"_rearViewRevealDisplacement"]; + _rightViewRevealWidth = [coder decodeDoubleForKey:@"_rightViewRevealWidth"]; + _rightViewRevealOverdraw = [coder decodeDoubleForKey:@"_rightViewRevealOverdraw"]; + _rightViewRevealDisplacement = [coder decodeDoubleForKey:@"_rightViewRevealDisplacement"]; + _bounceBackOnOverdraw = [coder decodeBoolForKey:@"_bounceBackOnOverdraw"]; + _bounceBackOnLeftOverdraw = [coder decodeBoolForKey:@"_bounceBackOnLeftOverdraw"]; + _stableDragOnOverdraw = [coder decodeBoolForKey:@"_stableDragOnOverdraw"]; + _stableDragOnLeftOverdraw = [coder decodeBoolForKey:@"_stableDragOnLeftOverdraw"]; + _presentFrontViewHierarchically = [coder decodeBoolForKey:@"_presentFrontViewHierarchically"]; + _quickFlickVelocity = [coder decodeDoubleForKey:@"_quickFlickVelocity"]; + _toggleAnimationDuration = [coder decodeDoubleForKey:@"_toggleAnimationDuration"]; + _toggleAnimationType = [coder decodeIntegerForKey:@"_toggleAnimationType"]; + _springDampingRatio = [coder decodeDoubleForKey:@"_springDampingRatio"]; + _replaceViewAnimationDuration = [coder decodeDoubleForKey:@"_replaceViewAnimationDuration"]; + _frontViewShadowRadius = [coder decodeDoubleForKey:@"_frontViewShadowRadius"]; + _frontViewShadowOffset = [coder decodeCGSizeForKey:@"_frontViewShadowOffset"]; + _frontViewShadowOpacity = [coder decodeDoubleForKey:@"_frontViewShadowOpacity"]; + _frontViewShadowColor = [coder decodeObjectForKey:@"_frontViewShadowColor"]; + _userInteractionStore = [coder decodeBoolForKey:@"_userInteractionStore"]; + _animationQueue = [NSMutableArray array]; + _draggableBorderWidth = [coder decodeDoubleForKey:@"_draggableBorderWidth"]; + _clipsViewsToBounds = [coder decodeBoolForKey:@"_clipsViewsToBounds"]; + _extendsPointInsideHit = [coder decodeBoolForKey:@"_extendsPointInsideHit"]; + + [self setRearViewController:[coder decodeObjectForKey:@"_rearViewController"]]; + [self setFrontViewController:[coder decodeObjectForKey:@"_frontViewController"]]; + [self setRightViewController:[coder decodeObjectForKey:@"_rightViewController"]]; - // ??? What's needed to restore what position the views are in - _frontViewPosition = [coder decodeIntForKey: @"frontViewPosition"]; - _rearViewPosition = [coder decodeIntForKey: @"rearViewPosition"]; - _rightViewPosition = [coder decodeIntForKey: @"rightViewPosition"]; + [self setFrontViewPosition:[coder decodeIntForKey: @"_frontViewPosition"]]; [super decodeRestorableStateWithCoder:coder]; } -- (void)applicationFinishedRestoringState { - // ??? Get the views to where they should be - - [self _setFrontViewPosition: _frontViewPosition withDuration:0.0]; - - [self revealToggleAnimated: NO]; - [self revealToggleAnimated: NO]; - -// if (_rearViewPosition != FrontViewPositionNone) { -// [self revealToggleAnimated: NO]; -// } -// -// if (_rightViewPosition >= FrontViewPositionLeft) { -// [self rightRevealToggleAnimated: NO]; -// } +- (void)applicationFinishedRestoringState +{ + // nothing to do at this stage } + @end @@ -1825,11 +1857,7 @@ - (SWRevealViewController*)revealViewController { UIViewController *parent = self; Class revealClass = [SWRevealViewController class]; - - while ( nil != (parent = [parent parentViewController]) && ![parent isKindOfClass:revealClass] ) - { - } - + while ( nil != (parent = [parent parentViewController]) && ![parent isKindOfClass:revealClass] ) {} return (id)parent; }