我有一个SwiftUI应用,应支持横向和纵向。但是我想要iOS中Camera.app的行为。旋转设备时,仅在旋转风景/肖像时旋转所有其他内容。
因此,我想禁用某些视图的自动旋转并启用某些视图的旋转。不确定如何实现。
struct ContentView: View {
var view: some View {
HStack {
Text("Hello") // Don't rotate
Text("World") // Rotate
}
}
}
理论上,我尝试HERE_1或HERE_2来检测旋转并在旋转设备时旋转“ Hello”文本。
我已经尝试过使用link_2并更改如下代码:
在SceneDelegate:
中func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {
switch windowScene.interfaceOrientation {
case .landscapeLeft:
model.angel = 90
case .landscapeRight:
model.angel = -90
case .portraitUpsideDown:
model.angel = 180
default:
model.angel = 0
}
}
在模型:
class Model: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var angel: Double = 0 { willSet { objectWillChange.send() } }
}
以及ContentView:
struct ContentView: View {
@EnvironmentObject var model: Model
var body: some View {
HStack {
Text("Hello")
.rotationEffect(Angle(degrees: model.angel))
Text("World")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
希望获得帮助!