Created
August 26, 2024 16:04
-
-
Save bballentine/8db5ca95e4f238753e8fcf2b143a72a4 to your computer and use it in GitHub Desktop.
SwiftUI Device Rotation View Modifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct DeviceRotationViewModifier: ViewModifier { | |
let action: (UIDeviceOrientation) -> Void | |
func body(content: Content) -> some View { | |
content | |
.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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment