I have a SwiftUI app which should support landscape and portrait. However I want the behaviour of the Camera.app in iOS. When the device is rotated only the buttons are rotated when switching landscape/portrait all other content is as is.
So I want to disable the auto rotation of some views and enable rotation for some views. Not sure how to implement it.
struct ContentView: View {
var view: some View {
HStack {
Text("Hello") // Don't rotate
Text("World") // Rotate
}
}
}
In theory, I tried HERE_1 or HERE_2 to detect rotation and rotate the "Hello" text while rotate device.
I had already tried using link_2 and change a bit of code as below:
In 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
}
}
In Model:
class Model: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var angel: Double = 0 { willSet { objectWillChange.send() } }
}
And in 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()
}
}
Hope this help!