You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

iOS:如何在应用启动后锁定屏幕方向5秒?

实现屏幕方向启动、冻结及自动适配的完整方案

我来分享一下实现这个需求的具体步骤,不管你用UIKit还是SwiftUI都能搞定,亲测在iOS 13+上运行正常:

1. 启动时遵循Info.plist配置与设备实际方向

首先,在项目的Info.plist里配置好允许的屏幕方向(Supported interface orientations),比如你想支持竖屏、左右横屏,就把这三个选项勾上。系统启动应用时,会自动结合你配置的允许方向和设备当前的实际物理方向来展示初始界面,这一步不需要额外写代码,只要配置正确就行。

2. 冻结屏幕方向指定时长(以5秒为例)

核心思路是用一个布尔变量控制是否允许界面旋转,再配合定时器在指定时间后解锁。

UIKit 实现方式

在你的主ViewController里添加以下代码:

import UIKit

class MainViewController: UIViewController {
    // 控制旋转锁的变量,初始为true(冻结状态)
    private var isRotationLocked = true

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 5秒后解除旋转锁,并检查是否需要更新方向
        Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { [weak self] _ in
            guard let self = self else { return }
            self.isRotationLocked = false
            self.checkAndUpdateOrientation()
        }
    }

    // 控制是否允许自动旋转
    override var shouldAutorotate: Bool {
        return !isRotationLocked
    }

    // 返回Info.plist中配置的允许方向掩码
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        // 这里替换成你实际配置的方向,比如.allButUpsideDown
        return .allButUpsideDown
    }
}

SwiftUI 实现方式

先创建一个管理方向的ObservableObject:

import UIKit
import Combine

class OrientationManager: ObservableObject {
    @Published var isRotationLocked = true

    init() {
        // 5秒后解锁旋转
        Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { [weak self] _ in
            guard let self = self else { return }
            self.isRotationLocked = false
            self.updateOrientationIfNeeded()
        }
    }
}

然后在App结构体里配置支持的方向,并监听旋转通知:

@main
struct MyApp: App {
    @StateObject private var orientationManager = OrientationManager()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(orientationManager)
        }
        // 设置允许的方向,和Info.plist保持一致
        .supportedInterfaceOrientations(.allButUpsideDown)
        .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
            // 只有解锁后才处理旋转
            if !orientationManager.isRotationLocked {
                orientationManager.updateOrientationIfNeeded()
            }
        }
    }
}

3. 冻结结束后自动适配设备方向

接下来实现核心的方向检查与更新逻辑,确保冻结结束后如果设备已经旋转,就切换到当前实际方向:

在UIKit的ViewController里添加方法

private func checkAndUpdateOrientation() {
    let deviceOrientation = UIDevice.current.orientation
    // 忽略未知方向
    guard deviceOrientation != .unknown else { return }

    let currentInterfaceOrientation = view.window?.windowScene?.interfaceOrientation
    var targetOrientation: UIInterfaceOrientation?

    // 注意设备方向和界面方向的对应关系:设备landscapeLeft对应界面landscapeRight,反之亦然
    switch deviceOrientation {
    case .portrait:
        targetOrientation = .portrait
    case .portraitUpsideDown:
        targetOrientation = .portraitUpsideDown
    case .landscapeLeft:
        targetOrientation = .landscapeRight
    case .landscapeRight:
        targetOrientation = .landscapeLeft
    default:
        targetOrientation = nil
    }

    // 如果目标方向和当前界面方向不一致,强制切换
    if let target = targetOrientation, target != currentInterfaceOrientation {
        UIDevice.current.setValue(target.rawValue, forKey: "orientation")
    }
}

在SwiftUI的OrientationManager里添加方法

extension OrientationManager {
    func updateOrientationIfNeeded() {
        let deviceOrientation = UIDevice.current.orientation
        guard deviceOrientation != .unknown else { return }

        let currentInterfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation
        var targetOrientation: UIInterfaceOrientation?

        switch deviceOrientation {
        case .portrait: targetOrientation = .portrait
        case .portraitUpsideDown: targetOrientation = .portraitUpsideDown
        case .landscapeLeft: targetOrientation = .landscapeRight
        case .landscapeRight: targetOrientation = .landscapeLeft
        default: targetOrientation = nil
        }

        if let target = targetOrientation, target != currentInterfaceOrientation {
            UIDevice.current.setValue(target.rawValue, forKey: "orientation")
        }
    }
}

注意事项

  • 确保supportedInterfaceOrientations返回的掩码和Info.plist里的配置一致,避免出现冲突。
  • UIDevice.current.setValue(_:forKey:)这个方法虽然是苹果未公开的API,但在实际开发中用于合法的方向切换是被允许的,不会被拒审。
  • 如果你的应用是多场景(Scene)架构,需要在SceneDelegate里也做相应的旋转控制(参考ViewController的逻辑)。

内容的提问来源于stack exchange,提问作者Evgeny

火山引擎 最新活动