Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryanlintott/rotationmatchingorientation
Rotates any SwiftUI view to match device orientation
https://github.com/ryanlintott/rotationmatchingorientation
swiftui
Last synced: about 1 month ago
JSON representation
Rotates any SwiftUI view to match device orientation
- Host: GitHub
- URL: https://github.com/ryanlintott/rotationmatchingorientation
- Owner: ryanlintott
- Created: 2022-05-23T17:41:43.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-23T20:46:50.000Z (over 2 years ago)
- Last Synced: 2024-11-09T03:25:31.346Z (3 months ago)
- Topics: swiftui
- Language: Swift
- Homepage: https://github.com/ryanlintott/FrameUp
- Size: 69.3 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RotationMatchingOrientation
Rotates any view so that it matches the device orientation if it's in an array of allowed orientations.
*Submission for SwiftUISeries - Workarounds & Hacks event*
# Why did I make this workaround?
When building [Old English Wordhord](https://oldenglishwordhord.com/app/) I wanted the app to be portrait-only but the image view to rotate with the phone. Although this can be done in UIKit, SwiftUI doesn't have a way to control allowed orientations per view.# Example
The app only allows portrait and landscapeLeft orientations but the content can be set to rotate to any number of orientations using the toggles.[![Example Video](https://img.youtube.com/vi/Ae4hbOIU9jc/0.jpg)](https://www.youtube.com/watch?v=Ae4hbOIU9jc)
# How does it work?
- Device orientation is taken from `UIDevice.current.orientation`
- `supportedInterfaceOrientations` is taken from `UISupportedInterfaceOrientations` in the app bundle
- `interfaceOrientation` is determined from `deviceOrientation` and `supportedInterfaceOrientations`
- `contentOrientation` is determined from `deviceOrientation` and `allowedOrientations`
- Rotation between `interfaceOrientation` and `contentOrientation` is used to rotate the content view
- GeometryReader is used to calculate the size of the space
- Height and width of the content are swapped if there's a 90 degree different between `interfaceOrientation` and `contentOrientation`
- `.position()` modifier is used to center the view
- Orientations are updated whenever:
- `.onAppear`
- `allowedOrientations` are changed
- `.orientationDidChangeNotification` fires
- `.willEnterForegroundNotification` fires# Use it in your app
This component is available in my Swift package: