Swift Device Orientation обновляется неправильно ⇐ IOS
-
Anonymous
Swift Device Orientation обновляется неправильно
I want to detect device orientation in swift so that I can apply the correct orientation to a video. The code I have for updating device orientation works correctly but is extremely sensitive to tiny movements. Holding the device upward and barely tilting it causes the device orientation to update to a landscape orientation even though the phone is basically still portrait. Is there a way to lower the sensitivity for device orientation so that tiny movements dont cause an unwanted update?
struct TopVideo: View { var body: some View { ZStack { } .onRotate { newOrientation in withAnimation(.easeInOut(duration: 0.2)){ orien = newOrientation } } } } struct DeviceRotationViewModifier: ViewModifier { let action: (UIDeviceOrientation) -> Void func body(content: Content) -> some View { content .onAppear() .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in action(UIDevice.current.orientation) } } } extension View { func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { self.modifier(DeviceRotationViewModifier(action: action)) } }
Источник: https://stackoverflow.com/questions/780 ... ncorrectly
I want to detect device orientation in swift so that I can apply the correct orientation to a video. The code I have for updating device orientation works correctly but is extremely sensitive to tiny movements. Holding the device upward and barely tilting it causes the device orientation to update to a landscape orientation even though the phone is basically still portrait. Is there a way to lower the sensitivity for device orientation so that tiny movements dont cause an unwanted update?
struct TopVideo: View { var body: some View { ZStack { } .onRotate { newOrientation in withAnimation(.easeInOut(duration: 0.2)){ orien = newOrientation } } } } struct DeviceRotationViewModifier: ViewModifier { let action: (UIDeviceOrientation) -> Void func body(content: Content) -> some View { content .onAppear() .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in action(UIDevice.current.orientation) } } } extension View { func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { self.modifier(DeviceRotationViewModifier(action: action)) } }
Источник: https://stackoverflow.com/questions/780 ... ncorrectly
Мобильная версия