-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
659798f
commit d11680c
Showing
4 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// SwiftUIView.swift | ||
// | ||
// | ||
// Created by Bartolomeo Sorrentino on 28/11/22. | ||
// | ||
// Inspired by https://www.hackingwithswift.com/quick-start/swiftui/how-to-detect-device-rotation | ||
|
||
import SwiftUI | ||
|
||
// Our custom view modifier to track rotation and | ||
// call our action | ||
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) | ||
} | ||
} | ||
} | ||
|
||
// A View wrapper to make the modifier easier to use | ||
extension View { | ||
func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { | ||
self.modifier(DeviceRotationViewModifier(action: action)) | ||
} | ||
} |
This file contains 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
This file contains 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