gpt4 book ai didi

iphone - iOS 7/iOS 6 应用程序不同部分的条件旋转纵向/横向

转载 作者:太空狗 更新时间:2023-10-30 03:50:30 24 4
gpt4 key购买 nike

问题:某应用程序的不同部分同时使用横向模式(锁定)和纵向模式(锁定)。现在我有了一个可行的解决方案,但它似乎不正确,而且确实有它自己的问题。

最理想的情况是我愿意强制改变方向。如果需要,甚至考虑进行 View 转换。

App基本流程:

  • HomeView(纵向)(它有一些子推送 View 也是纵向 View 并锁定到那个 View )。
  • LandscapeView(横向)(有 5 个推送的也是横向的 subview )

注意:

  • HomeView 有到 LandscapeView 的链接
  • LandscapeView可以回到HomeView
  • 在 LandscapeView subview 结束时返回到 HomeView

基本图像显示了不同 View 方向的外观。 (线条表示应用程序的流程,图像的方向表示每个屏幕应该如何)

current view changes

目前使用下面的实现通过[setLockedToPortait:YES](对于纵向 View )等调用/设置 View 是纵向模式还是横向模式。

如果设备旋转,这会查询从 iOS 使用的界面方向。

现在对于转到 LandscapeView 的情况,我在普通 View 的顶部显示了一个临时 View ,要求使用它们将手机旋转到横向。 (从横向 View 返回到HomeView时也会显示一个临时 View )

因此,一旦用户旋转了他们的设备,它就会触发正确的方向,然后临时 View 就会隐藏。

如果用户此时将手机旋转回纵向,它仍将锁定为横向,因此不会触发另一个 View 旋转(也不会出现临时 View 或任何内容)


当前实现代码:

// ---------------------- NavigationController (subclass of UINavigationController)
@interface NavigationController () {
BOOL isOrientationPortrait;
}
@end


@implementation NavigationController {
UIDeviceOrientation lastAccepted;
UIDeviceOrientation lastKnown;
}

-(void)setLockedToPortait:(BOOL)isLocked {
isOrientationPortrait = isLocked;
}

-(UIDeviceOrientation) getCurrentOrientation {
UIDeviceOrientation orientate = [[UIDevice currentDevice] orientation];
if(orientate == 0) { // needed for simulator
orientate = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
}
return orientate;
}

// Deprecated in iOS6, still needed for iOS5 support.
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastKnownOrientation:orientation];

if(isOrientationPortrait == YES) {
if([self isLastKnownPortrait] == YES) {
[self setLastAcceptedOrientation:orientation];
return YES;
} else {
return NO;
}
} else {
if([self isLastKnownLandscape] == YES) {
[self setLastAcceptedOrientation:orientation];
return YES;
} else {
return NO;
}
}
}

// iOS6/7 support
- (BOOL)shouldAutorotate
{
// find out the current device orientation
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastKnownOrientation:orientation];
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
if(isOrientationPortrait == YES) {
if([self isLastKnownPortrait] == YES)
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastAcceptedOrientation:orientation];
}
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
} else {
if([self isLastKnownLandscape] == YES)
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastAcceptedOrientation:orientation];
}
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight );
}
}
-(void)setLastAcceptedOrientation:(UIDeviceOrientation)orient {
lastAccepted = orient;
}

-(void)setLastKnownOrientation:(UIDeviceOrientation)orient {
lastKnown = orient;
}

-(BOOL)isLastKnownPortrait {
return UIDeviceOrientationIsPortrait(lastKnown);
}

-(BOOL)isLastKnownLandscape {
return UIDeviceOrientationIsLandscape(lastKnown);
}

-(BOOL)isLastAcceptedPortrait {
return UIDeviceOrientationIsPortrait(lastAccepted);
}

-(BOOL)isLastAcceptedLandscape {
return UIDeviceOrientationIsLandscape(lastAccepted);
}

当前的问题:

  • 在加载 View 后始终需要设备旋转,以便用户从纵向转到横向模式,反之亦然。
  • 如果用户锁定了设备方向,这将根本不起作用。
  • 当从横向模式转换回来,并且用户已经将他们的设备旋转到纵向(在最后一个横向 View 中)时,纵向 View 的界面将被锁定为“横向”布局,直到用户重新旋转他们的设备(所以目前我只是展示了用于旋转设备的叠加层,但它已经旋转了……这对用户来说非常烦人)。上述实现目前存在重大问题。

希望能够:

  • 针对当前 View 在手机上强制更改方向。
  • 为在 View 的推送/弹出之间强制设置的 View 设置首选布局。

我在这里和 Apple Dev 论坛上看了很多其他解决方案,但是似乎没有一个解决这个问题,或者两个 View 之间的方向错误仍然存​​在。

感谢您的帮助或指点!任何建议都不会打折 :D

--编辑::

感谢@leo-natan 找到了解决方案!!

因此,不要试图强制改变 View 的方向。只需推送一个新的模态视图。这迫使改变。您仍然需要上面的方向代码来管理旋转。

那么现在我的 HomeViewController 中有:

LandscapeViewController * viewController = [[[LandscapeViewController ViewController alloc] init] autorelease];
UINib * nib = [UINib nibWithNibName:@"NavigationController" bundle:nil];
NavigationController *navController = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[navController initWithRootViewController:viewController];
[self presentViewController:navController animated:YES completion:^{
// completion
}];

所以需要为这个modal view重新添加一个新的navigation controller。 另请注意,上面的“presentViewController”是推送模态视图的新方法。

为管理 View Controller 实现了这个重载方法:

-(id)initWithRootViewController:(UIViewController *)rootViewController {

self = [super initWithRootViewController:rootViewController];
if(self){

}
return self;
}

注意:上面没有使用 Storyboard。该问题可以通过使用 Storyboard并以相同方式模态显示 View 来解决。

最佳答案

看我的回答here ,包括一个测试项目。

基本上,只有在模态呈现 View Controller 时才能强制改变方向。例如,某些应用程序中的媒体播放。如果您希望从只能以纵向呈现的 View Controller 转换为只能以横向呈现的 View Controller ,您将需要模态呈现。推送将不起作用。

关于iphone - iOS 7/iOS 6 应用程序不同部分的条件旋转纵向/横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19041650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com