简体   繁体   English

iOS:强制在回调后应用当前设备方向

[英]iOS: Force apply current device orientation after callback

I'm currently building an iOS app (supporting iOS >= 8) that has initial splash screen animation on start up. 我目前正在构建一个iOS应用程序(支持iOS> = 8),它在启动时具有初始启动画面动画。 This animation blocks the entire screen during 2~3 seconds and then hides itself. 此动画在2~3秒内阻挡整个屏幕,然后隐藏自己。

Goal is that the screen orientation should be fixed to portrait mode, and be released when the animation is over. 目标是屏幕方向应固定为纵向模式,并在动画结束时释放。 This can be easily achieved by the following code: 这可以通过以下代码轻松实现:

- (void)onSplashAnimationEnded {
    _isFinished = YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (_isFinished) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

The problem is that when a user launched the app in landscape mode, and when the animation is finished, the device orientation (currently in landscape mode) does not apply to the app (locked in portrait mode, as intended). 问题是当用户以横向模式启动应用程序时,以及动画完成时,设备方向(当前处于横向模式)不适用于应用程序(按照预期锁定在纵向模式下)。

It seems natural that the current orientation of device is automatically applied to the app right after the animation is over. 在动画结束后,设备的当前方向自动应用于应用程序似乎很自然。 I tried the followings to solve this and none of them works. 我尝试了以下解决方案,但没有一个能够解决问题。

- (void)onSplashAnimationEnded {
    _isFinished = YES;

    // #1
    [UIViewController attemptRotationToDeviceOrientation];

    // #2
    [[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];

    // #3
    [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidChangeStatusBarOrientationNotification object:[UIApplication sharedApplication]];
}

I really hope someone to share any workarounds for this issue. 我真的希望有人分享这个问题的解决方法。

Edit #1 编辑#1

I have read the other question that is linked to mine, and want to add some detail about it. 我已经阅读了与我有关的另一个问题,并希望添加一些有关它的细节。

It is possible to lock or unlock device rotation in a certain situation as described in the link, and I already implemented the feature in other way. 如链接中所述,可以在某种情况下锁定或解锁设备旋转,并且我已经以其他方式实现了该功能。

What is left can be regenerated by the following procedure: 剩余的内容可以通过以下过程重新生成:

  1. Set iPad in landscape mode. 将iPad设置为横向模式。
  2. Launch the app (which will force the orientation in portrait mode), without rotating the device. 启动应用程序(将以纵向模式强制方向),而无需旋转设备。
  3. When the animation is over, the device remains in portrait mode ( which is odd because the device is in landscape mode ). 当动画结束时,设备将保持纵向模式( 这是奇怪的,因为设备处于横向模式 )。
  4. Orientation can be fixed when user tries to rotate to portrait > landscape mode one or twice. 当用户尝试旋转到纵向>横向模式一次或两次时,可以修复方向。
  5. What I do want to do is to eliminate the step #4. 我想要做的是取消第4步。

After all, it is possible to implement the feature I wanted by the following combination, without any other settings in other view controllers: 毕竟,可以通过以下组合实现我想要的功能,而不需要在其他视图控制器中进行任何其他设置:

// Control device orientation only in AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return _isFinished ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
}

- (void)onSplashAnimationEnded {
    _isFinished = YES;

    // Invoke OS orientation change message
    [UIViewController attemptRotationToDeviceOrientation];
}

For this, device orientation should be available in other view controllers. 为此,应在其他视图控制器中提供设备方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM