mirror of
https://github.com/zhigang1992/ECSlidingViewController.git
synced 2026-03-29 00:18:36 +08:00
Connect menu items to replace top view
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
{
|
||||
ECSlidingViewController *slidingViewController = (ECSlidingViewController *)self.window.rootViewController;
|
||||
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
|
||||
slidingViewController.underLeftViewController = [storyboard instantiateViewControllerWithIdentifier:@"Menu"];
|
||||
slidingViewController.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"FirstTop"];
|
||||
slidingViewController.underLeftViewController = [storyboard instantiateViewControllerWithIdentifier:@"Menu"];
|
||||
slidingViewController.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"FirstTop"];
|
||||
|
||||
slidingViewController.anchorRightRevealAmount = 280;
|
||||
slidingViewController.anchorLeftRevealAmount = 280;
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
|
||||
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
|
||||
self.slidingViewController.underRightViewController = [storyboard instantiateViewControllerWithIdentifier:@"UnderRight"];
|
||||
}
|
||||
|
||||
- (IBAction)revealMenu:(id)sender
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "ECSlidingViewController.h"
|
||||
|
||||
@interface MenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate>
|
||||
|
||||
|
||||
@@ -40,7 +40,10 @@
|
||||
|
||||
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
NSLog(@"sup: %@", [self.menuItems objectAtIndex:indexPath.row]);
|
||||
NSString *identifier = [NSString stringWithFormat:@"%@Top", [self.menuItems objectAtIndex:indexPath.row]];
|
||||
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
|
||||
UIViewController *newTopViewController = [storyboard instantiateViewControllerWithIdentifier:identifier];
|
||||
[self.slidingViewController replaceTopViewController:newTopViewController];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "ECSlidingViewController.h"
|
||||
|
||||
@interface SecondTopViewController : UIViewController
|
||||
|
||||
- (IBAction)revealMenu:(id)sender;
|
||||
@end
|
||||
|
||||
@@ -10,4 +10,16 @@
|
||||
|
||||
@implementation SecondTopViewController
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
|
||||
self.slidingViewController.underRightViewController = nil;
|
||||
}
|
||||
|
||||
- (IBAction)revealMenu:(id)sender
|
||||
{
|
||||
[self.slidingViewController anchorToRight];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -47,7 +47,22 @@
|
||||
<view key="view" contentMode="scaleToFill" id="UCM-3d-Qlw">
|
||||
<rect key="frame" x="0.0" y="20" width="320" height="460"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
|
||||
<subviews>
|
||||
<navigationBar contentMode="scaleToFill" id="DZQ-RF-UaR">
|
||||
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
|
||||
<items>
|
||||
<navigationItem title="Third" id="aqx-6e-2rn">
|
||||
<barButtonItem key="leftBarButtonItem" title="Menu" id="jqE-oN-67Z">
|
||||
<connections>
|
||||
<action selector="revealMenu:" destination="LsE-e5-Jbi" id="Dtd-tB-t74"/>
|
||||
</connections>
|
||||
</barButtonItem>
|
||||
</navigationItem>
|
||||
</items>
|
||||
</navigationBar>
|
||||
</subviews>
|
||||
<color key="backgroundColor" red="0.31581848979999999" green="0.63389155529999996" blue="1" alpha="1" colorSpace="calibratedRGB"/>
|
||||
</view>
|
||||
</viewController>
|
||||
</objects>
|
||||
@@ -60,7 +75,22 @@
|
||||
<view key="view" contentMode="scaleToFill" id="Z4T-qv-OS6">
|
||||
<rect key="frame" x="0.0" y="20" width="320" height="460"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
|
||||
<subviews>
|
||||
<navigationBar contentMode="scaleToFill" id="rTR-El-PZX">
|
||||
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
|
||||
<items>
|
||||
<navigationItem title="Second" id="pNC-6r-dql">
|
||||
<barButtonItem key="leftBarButtonItem" title="Menu" id="N80-y2-Ta5">
|
||||
<connections>
|
||||
<action selector="revealMenu:" destination="XYc-ji-qQz" id="tFN-2R-5Go"/>
|
||||
</connections>
|
||||
</barButtonItem>
|
||||
</navigationItem>
|
||||
</items>
|
||||
</navigationBar>
|
||||
</subviews>
|
||||
<color key="backgroundColor" red="0.0" green="0.83163265310000001" blue="0.68719590809999997" alpha="1" colorSpace="calibratedRGB"/>
|
||||
</view>
|
||||
</viewController>
|
||||
</objects>
|
||||
@@ -93,7 +123,7 @@
|
||||
</items>
|
||||
</navigationBar>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
|
||||
<color key="backgroundColor" red="1" green="0.5" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
|
||||
</view>
|
||||
</viewController>
|
||||
</objects>
|
||||
@@ -106,7 +136,7 @@
|
||||
<view key="view" contentMode="scaleToFill" id="XRA-pX-V7S">
|
||||
<rect key="frame" x="0.0" y="20" width="320" height="460"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
|
||||
<color key="backgroundColor" name="controlHighlightColor" catalog="System" colorSpace="catalog"/>
|
||||
</view>
|
||||
</viewController>
|
||||
</objects>
|
||||
@@ -142,9 +172,15 @@
|
||||
</class>
|
||||
<class className="SecondTopViewController" superclassName="UIViewController">
|
||||
<source key="sourceIdentifier" type="project" relativePath="./Classes/SecondTopViewController.h"/>
|
||||
<relationships>
|
||||
<relationship kind="action" name="revealMenu:"/>
|
||||
</relationships>
|
||||
</class>
|
||||
<class className="ThirdTopViewController" superclassName="UIViewController">
|
||||
<source key="sourceIdentifier" type="project" relativePath="./Classes/ThirdTopViewController.h"/>
|
||||
<relationships>
|
||||
<relationship kind="action" name="revealMenu:"/>
|
||||
</relationships>
|
||||
</class>
|
||||
<class className="UnderRightViewController" superclassName="UIViewController">
|
||||
<source key="sourceIdentifier" type="project" relativePath="./Classes/UnderRightViewController.h"/>
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "ECSlidingViewController.h"
|
||||
|
||||
@interface ThirdTopViewController : UIViewController
|
||||
|
||||
- (IBAction)revealMenu:(id)sender;
|
||||
@end
|
||||
|
||||
@@ -10,4 +10,16 @@
|
||||
|
||||
@implementation ThirdTopViewController
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
|
||||
self.slidingViewController.underRightViewController = nil;
|
||||
}
|
||||
|
||||
- (IBAction)revealMenu:(id)sender
|
||||
{
|
||||
[self.slidingViewController anchorToRight];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
@interface ECSlidingViewController : UIViewController
|
||||
|
||||
@property (nonatomic, strong) UIViewController *underLeftViewController;
|
||||
@property (nonatomic, strong) UIViewController *underRightViewController;
|
||||
@property (nonatomic, strong) UIViewController *topViewController;
|
||||
@property (nonatomic, unsafe_unretained) CGFloat anchorRightRevealAmount;
|
||||
@property (nonatomic, unsafe_unretained) CGFloat anchorLeftRevealAmount;
|
||||
@@ -20,6 +21,7 @@
|
||||
- (void)anchorToRight;
|
||||
- (void)anchorToLeft;
|
||||
- (void)reset;
|
||||
- (void)replaceTopViewController:(UIViewController *)newTopViewController;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
- (NSUInteger)autoResizeToFillScreen;
|
||||
- (UIView *)topView;
|
||||
- (UIView *)underLeftView;
|
||||
- (UIView *)underRightView;
|
||||
- (void)updateTopViewLeftEdgePosition:(CGFloat)position;
|
||||
- (void)addTopViewSnapshot;
|
||||
- (void)removeTopViewSnapshot;
|
||||
@@ -26,6 +27,10 @@
|
||||
- (BOOL)underLeftShowing;
|
||||
- (BOOL)underRightShowing;
|
||||
|
||||
- (void)underLeftWillAppear;
|
||||
- (void)underRightWillAppear;
|
||||
- (void)topDidReset;
|
||||
|
||||
@end
|
||||
|
||||
@implementation UIViewController(SlidingViewExtension)
|
||||
@@ -43,8 +48,9 @@
|
||||
@end
|
||||
|
||||
@implementation ECSlidingViewController
|
||||
@synthesize underLeftViewController = _underLeftViewController;
|
||||
@synthesize topViewController = _topViewController;
|
||||
@synthesize underLeftViewController = _underLeftViewController;
|
||||
@synthesize underRightViewController = _underRightViewController;
|
||||
@synthesize topViewController = _topViewController;
|
||||
@synthesize anchorRightRevealAmount;
|
||||
@synthesize anchorLeftRevealAmount;
|
||||
@synthesize topViewSnapshot;
|
||||
@@ -55,18 +61,54 @@
|
||||
|
||||
- (void)setTopViewController:(UIViewController *)theTopViewController
|
||||
{
|
||||
[self removeTopViewSnapshot];
|
||||
[_topViewController.view removeFromSuperview];
|
||||
[_topViewController removeFromParentViewController];
|
||||
|
||||
_topViewController = theTopViewController;
|
||||
[_topViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
|
||||
[_topViewController.view setFrame:self.view.bounds];
|
||||
|
||||
[self addChildViewController:self.topViewController];
|
||||
[self.topViewController didMoveToParentViewController:self];
|
||||
|
||||
[self.view addSubview:_topViewController.view];
|
||||
}
|
||||
|
||||
- (void)setUnderLeftViewController:(UIViewController *)theUnderLeftViewController
|
||||
{
|
||||
[_underLeftViewController.view removeFromSuperview];
|
||||
[_underLeftViewController removeFromParentViewController];
|
||||
|
||||
_underLeftViewController = theUnderLeftViewController;
|
||||
[_underLeftViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
|
||||
[_underLeftViewController.view setFrame:self.view.bounds];
|
||||
[self.view insertSubview:_underLeftViewController.view atIndex:0];
|
||||
|
||||
if (_underLeftViewController) {
|
||||
[_underLeftViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
|
||||
[_underLeftViewController.view setFrame:self.view.bounds];
|
||||
|
||||
[self addChildViewController:self.underLeftViewController];
|
||||
[self.underLeftViewController didMoveToParentViewController:self];
|
||||
|
||||
[self.view insertSubview:_underLeftViewController.view atIndex:0];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setUnderRightViewController:(UIViewController *)theUnderRightViewController
|
||||
{
|
||||
[_underRightViewController.view removeFromSuperview];
|
||||
[_underRightViewController removeFromParentViewController];
|
||||
|
||||
_underRightViewController = theUnderRightViewController;
|
||||
|
||||
if (_underRightViewController) {
|
||||
[_underRightViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
|
||||
[_underRightViewController.view setFrame:self.view.bounds];
|
||||
|
||||
[self addChildViewController:self.underRightViewController];
|
||||
[self.underRightViewController didMoveToParentViewController:self];
|
||||
|
||||
[self.view insertSubview:_underRightViewController.view atIndex:0];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewDidLoad
|
||||
@@ -78,12 +120,6 @@
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
|
||||
[self addChildViewController:self.underLeftViewController];
|
||||
[self.underLeftViewController didMoveToParentViewController:self];
|
||||
|
||||
[self addChildViewController:self.topViewController];
|
||||
[self.topViewController didMoveToParentViewController:self];
|
||||
}
|
||||
|
||||
- (void)updateTopViewLeftEdgePositionWithRecognizer:(UIPanGestureRecognizer *)recognizer
|
||||
@@ -95,10 +131,17 @@
|
||||
self.initialTouchPositionX = currentTouchPositionX;
|
||||
self.initialLeftEdgePosition = self.topView.frame.origin.x;
|
||||
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
|
||||
[self updateTopViewLeftEdgePosition:self.initialLeftEdgePosition + currentTouchPositionX - self.initialTouchPositionX];
|
||||
CGFloat newLeftEdge = self.initialLeftEdgePosition + currentTouchPositionX - self.initialTouchPositionX;
|
||||
|
||||
if ((!self.underLeftViewController && newLeftEdge > 0) || (!self.underRightViewController && newLeftEdge < 0)) {
|
||||
newLeftEdge = 0;
|
||||
}
|
||||
|
||||
[self updateTopViewLeftEdgePosition:newLeftEdge];
|
||||
} else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
|
||||
CGPoint currentVelocityPoint = [recognizer velocityInView:self.view];
|
||||
CGFloat currentVelocityX = currentVelocityPoint.x;
|
||||
|
||||
if ([self underLeftShowing] && currentVelocityX > 100) {
|
||||
[self anchorToRight];
|
||||
} else if ([self underRightShowing] && currentVelocityX < 100) {
|
||||
@@ -111,8 +154,6 @@
|
||||
|
||||
- (void)anchorToRight
|
||||
{
|
||||
[self addTopViewSnapshot];
|
||||
[self.topView addGestureRecognizer:self.resetTapGesture];
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
[self updateTopViewLeftEdgePosition:self.anchorRightRevealAmount];
|
||||
}];
|
||||
@@ -120,8 +161,6 @@
|
||||
|
||||
- (void)anchorToLeft
|
||||
{
|
||||
[self addTopViewSnapshot];
|
||||
[self.topView addGestureRecognizer:self.resetTapGesture];
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
[self updateTopViewLeftEdgePosition:-self.anchorLeftRevealAmount];
|
||||
}];
|
||||
@@ -131,9 +170,19 @@
|
||||
{
|
||||
[UIView animateWithDuration:0.25f animations:^{
|
||||
[self updateTopViewLeftEdgePosition:0];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)replaceTopViewController:(UIViewController *)newTopViewController
|
||||
{
|
||||
[UIView animateWithDuration:0.125 animations:^{
|
||||
[self updateTopViewLeftEdgePosition:self.screenWidth];
|
||||
} completion:^(BOOL finished) {
|
||||
[self.topView removeGestureRecognizer:self.resetTapGesture];
|
||||
[self removeTopViewSnapshot];
|
||||
self.topViewController = newTopViewController;
|
||||
[self updateTopViewLeftEdgePosition:self.screenWidth];
|
||||
[UIView animateWithDuration:0.125 animations:^{
|
||||
[self updateTopViewLeftEdgePosition:0];
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -157,11 +206,27 @@
|
||||
return self.underLeftViewController.view;
|
||||
}
|
||||
|
||||
- (UIView *)underRightView
|
||||
{
|
||||
return self.underRightViewController.view;
|
||||
}
|
||||
|
||||
- (void)updateTopViewLeftEdgePosition:(CGFloat)position
|
||||
{
|
||||
CGRect frame = self.topView.frame;
|
||||
|
||||
if (frame.origin.x <= 0 && position > 0) {
|
||||
[self underLeftWillAppear];
|
||||
} else if (frame.origin.x >= 0 && position < 0) {
|
||||
[self underRightWillAppear];
|
||||
}
|
||||
|
||||
frame.origin.x = position;
|
||||
self.topView.frame = frame;
|
||||
|
||||
if (position == 0) {
|
||||
[self topDidReset];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)addTopViewSnapshot
|
||||
@@ -210,4 +275,26 @@
|
||||
return self.topView.frame.origin.x < 0;
|
||||
}
|
||||
|
||||
- (void)underLeftWillAppear
|
||||
{
|
||||
[self addTopViewSnapshot];
|
||||
[self.topView addGestureRecognizer:self.resetTapGesture];
|
||||
[self.view bringSubviewToFront:self.underLeftView];
|
||||
[self.view bringSubviewToFront:self.topView];
|
||||
}
|
||||
|
||||
- (void)underRightWillAppear
|
||||
{
|
||||
[self addTopViewSnapshot];
|
||||
[self.topView addGestureRecognizer:self.resetTapGesture];
|
||||
[self.view bringSubviewToFront:self.underRightView];
|
||||
[self.view bringSubviewToFront:self.topView];
|
||||
}
|
||||
|
||||
- (void)topDidReset
|
||||
{
|
||||
[self.topView removeGestureRecognizer:self.resetTapGesture];
|
||||
[self removeTopViewSnapshot];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user