带有 SWRevealViewController 的 ios 滑动状态栏
Posted
技术标签:
【中文标题】带有 SWRevealViewController 的 ios 滑动状态栏【英文标题】:ios slide status bar with SWRevealViewController 【发布时间】:2014-02-08 22:07:51 【问题描述】:这是一个相当具体的问题,所以我预计不会有大量回复,但值得一试。我正在为我的应用程序使用SWRevealViewController,我想知道是否有人在使用 SWRevealViewController 时实现了sliding status 栏。我有一些运气,但在推送新视图时遇到了问题。
如果是这样,您介意分享一下您是如何做到这一点的吗?
【问题讨论】:
你有想过这个吗?我正在尝试解决同样的问题 【参考方案1】:上述解决方案并不完整,并不适用于所有情况。这里是完整而强大的解决方案。
1.定义本地布尔值,在菜单打开时设置为 true,在菜单关闭时设置为 false。 @property (nonatomic, readwrite) BOOL isMenuOpen;
。在我的例子中,我合成了它。
2。检测菜单何时打开以及何时关闭。
-(void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
switch (position)
case FrontViewPositionLeftSideMostRemoved:
NSLog(@"RevealView: FrontViewPositionLeftSideMostRemoved");
break;
case FrontViewPositionLeftSideMost:
NSLog(@"RevealView: FrontViewPositionLeftSideMost");
break;
case FrontViewPositionLeftSide:
NSLog(@"RevealView: FrontViewPositionLeftSide");
break;
case FrontViewPositionLeft:
NSLog(@"RevealView: FrontViewPositionLeft");
isMenuOpen = false;
break;
case FrontViewPositionRight:
NSLog(@"RevealView: FrontViewPositionRight");
isMenuOpen = true;
break;
case FrontViewPositionRightMost:
NSLog(@"RevealView: FrontViewPositionRightMost");
break;
case FrontViewPositionRightMostRemoved:
NSLog(@"RevealView: FrontViewPositionRightMostRemoved");
break;
default:
break;
3.动画状态栏类似于上面的答案,但如果例如更健壮菜单没有完全关闭,而是重新打开。
#pragma mark - SWRevealViewControllerDelegate
- (void)revealController:(SWRevealViewController *)revealController panGestureMovedToLocation:(CGFloat)location progress:(CGFloat)progress overProgress:(CGFloat)overProgress
UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];
statusBarView.transform = CGAffineTransformMakeTranslation(progress * self.revealViewController.rearViewRevealWidth, 0.0f);
- (void) revealController:(SWRevealViewController *)revealController animateToPosition:(FrontViewPosition)position
NSLog(@"animateToPosition: %ld", (long)position);
UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];
if (position == FrontViewPositionRight)
[UIView animateWithDuration:0.25 animations:^
statusBarView.transform = CGAffineTransformMakeTranslation(self.revealViewController.rearViewRevealWidth, 0.0f);
completion:^(BOOL finished)
isMenuOpen = true;
];
else if (FrontViewPositionLeft)
[UIView animateWithDuration:0.25 animations:^
statusBarView.transform = CGAffineTransformMakeTranslation(0, 0.0f);
completion:^(BOOL finished)
isMenuOpen = false;
];
4.使用通知检测设备旋转。这是需要的,因为状态栏在例如从风景回到肖像。把这个放在viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
5.处理设备方向更改回到纵向(要完成倒置)。
- (void)deviceOrientationDidChange:(NSNotification *)notification
[self updateOrientationForStatusBar];
- (void)updateOrientationForStatusBar
switch ([UIDevice currentDevice].orientation)
case UIDeviceOrientationPortrait:
UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];
if (isMenuOpen)
statusBarView.transform = CGAffineTransformMakeTranslation(self.revealViewController.rearViewRevealWidth, 0.0f);
else
statusBarView.transform = CGAffineTransformMakeTranslation(0, 0.0f);
break;
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
case UIDeviceOrientationPortraitUpsideDown:
case UIDeviceOrientationUnknown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
break;
【讨论】:
【参考方案2】:我认为您可以使用 RevealControllerdelegate
方法来设置动画或更新它的 x 位置。像这样的:
手动滑动时更新状态栏:
- (void)revealController:(SWRevealViewController *)revealController panGestureMovedToLocation:(CGFloat)location progress:(CGFloat)progress overProgress:(CGFloat)overProgress
NSLog(@"6progress: %f", progress);
UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];
statusBarView.transform = CGAffineTransformMakeTranslation(progress * self.revealViewController.rearViewRevealWidth, 0.0f);
在切换操作时更新状态栏:
- (void) revealController:(SWRevealViewController *)revealController animateToPosition:(FrontViewPosition)position
NSLog(@"animateToPosition: %ld", (long)position);
UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];
if (!isDrawerOpen)
[UIView animateWithDuration:0.25 animations:^
statusBarView.transform = CGAffineTransformMakeTranslation(self.revealViewController.rearViewRevealWidth, 0.0f);
completion:^(BOOL finished)
isDrawerOpen = true;
];
else
[UIView animateWithDuration:0.25 animations:^
statusBarView.transform = CGAffineTransformMakeTranslation(0, 0.0f);
completion:^(BOOL finished)
isDrawerOpen = false;
];
【讨论】:
以上是关于带有 SWRevealViewController 的 ios 滑动状态栏的主要内容,如果未能解决你的问题,请参考以下文章
具有实用视图的 SWRevealViewController
SWRevealViewController 的 Segue 问题