Ril*_*Dev 13 uikit ios swiftui
我正在尝试检测设备何时在 iPad 上且处于纵向状态。
目前我使用UIDevice
UIKit 中的 API 并使用环境对象来观察变化。我使用此处找到的解决方案 -确定当前设备和方向.
然而orientationInfo.orientation
最初总是等于.portrait
until rotated into portrait and then back to landscape.
因此,当执行以下操作来显示FABView
struct HomeView: View {
@EnvironmentObject var orientationInfo: OrientationInfo
let isPhone = UIDevice.current.userInterfaceIdiom == .phone
var body: some View {
ZStack(alignment: .bottom) {
#if os(iOS)
if isPhone == false && orientationInfo.orientation == .portrait {
FABView()
}
#endif
}
}
}
Run Code Online (Sandbox Code Playgroud)
当 iPad 最初处于横向时,会加载该视图,但当更改为纵向并返回横向时,该视图会被删除。为什么会发生这种情况?如何确保第一次加载时未加载视图?
完整代码
struct HomeTab: View {
var body: some View {
NavigationView {
HomeView()
.environmentObject(OrientationInfo())
}
}
}
struct HomeView: View {
@EnvironmentObject var orientationInfo: OrientationInfo
let isPhone = UIDevice.current.userInterfaceIdiom == .phone
var body: some View {
ZStack(alignment: .bottom) {
#if os(iOS)
if isPhone == false && orientationInfo.orientation == .portrait {
FABView()
}
#endif
}
}
}
final class OrientationInfo: ObservableObject {
enum Orientation {
case portrait
case landscape
}
@Published var orientation: Orientation
private var _observer: NSObjectProtocol?
init() {
// fairly arbitrary starting value for 'flat' orientations
if UIDevice.current.orientation.isLandscape {
self.orientation = .landscape
}
else {
self.orientation = .portrait
}
// unowned self because we unregister before self becomes invalid
_observer = NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: nil) { [unowned self] note in
guard let device = note.object as? UIDevice else {
return
}
if device.orientation.isPortrait {
self.orientation = .portrait
}
else if device.orientation.isLandscape {
self.orientation = .landscape
}
}
}
deinit {
if let observer = _observer {
NotificationCenter.default.removeObserver(observer)
}
}
}
Run Code Online (Sandbox Code Playgroud)
paw*_*222 17
您可以用于UIDevice.orientationDidChangeNotification
检测方向变化,但应用程序启动时不应依赖它。
UIDevice.current.orientation.isValidInterfaceOrientation
一开始就是假的,因此两者
UIDevice.current.orientation.isLandscape
和
UIDevice.current.orientation.isPortrait
将返回false
。
interfaceOrientation
相反,您可以从第一个窗口场景中使用:
struct ContentView: View {
@State private var isPortrait = false
var body: some View {
Text("isPortrait: \(String(isPortrait))")
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
guard let scene = UIApplication.shared.windows.first?.windowScene else { return }
self.isPortrait = scene.interfaceOrientation.isPortrait
}
}
}
Run Code Online (Sandbox Code Playgroud)
另请注意,设备方向不等于界面方向。当设备在纵向模式下倒置时,设备方向为纵向,但界面方向也可以为横向。
我认为在您的情况下最好依靠界面方向。
归档时间: |
|
查看次数: |
8016 次 |
最近记录: |