├── .github └── workflows │ └── main.yml ├── .gitignore ├── Examples ├── OrientationKitDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── OrientationKitDemo │ ├── AppDelegate.swift │ ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── ContentView.swift │ ├── HostingController.swift │ ├── Info.plist │ ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json │ └── SceneDelegate.swift ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── OrientationKit │ ├── Environment.swift │ ├── Orientation.swift │ ├── OrientationManager.swift │ └── WithOrientations.swift └── Tests ├── LinuxMain.swift └── OrientationKitTests ├── OrientationKitTests.swift └── XCTestManifests.swift /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions 2 | # https://github.com/actions/virtual-environments/blob/master/images/macos 3 | name: CI 4 | 5 | on: 6 | push: 7 | branches: 8 | - master 9 | - ci/** 10 | pull_request: 11 | 12 | jobs: 13 | build: 14 | runs-on: macos-15 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Select Xcode version 18 | run: sudo xcode-select -s '/Applications/Xcode_16.2.app' 19 | - name: Build iOS 20 | run: xcodebuild -configuration Debug -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 16 Pro" -scheme "OrientationKitDemo" build 21 | working-directory: ./Examples 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 52; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 333D6A5625C6539B00FB06EF /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333D6A5525C6539B00FB06EF /* ContentView.swift */; }; 11 | 333D6A5825C6539C00FB06EF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 333D6A5725C6539C00FB06EF /* Assets.xcassets */; }; 12 | 333D6A5B25C6539C00FB06EF /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 333D6A5A25C6539C00FB06EF /* Preview Assets.xcassets */; }; 13 | 333D6A6C25C6543A00FB06EF /* OrientationKit in Frameworks */ = {isa = PBXBuildFile; productRef = 333D6A6B25C6543A00FB06EF /* OrientationKit */; }; 14 | 333D6A7425C6717E00FB06EF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333D6A7225C6717E00FB06EF /* AppDelegate.swift */; }; 15 | 333D6A7525C6717E00FB06EF /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333D6A7325C6717E00FB06EF /* SceneDelegate.swift */; }; 16 | 333D6A7925C6745200FB06EF /* HostingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 333D6A7825C6745200FB06EF /* HostingController.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 333D6A5025C6539B00FB06EF /* OrientationKitDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OrientationKitDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 333D6A5525C6539B00FB06EF /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 22 | 333D6A5725C6539C00FB06EF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 333D6A5A25C6539C00FB06EF /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 24 | 333D6A5C25C6539C00FB06EF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | 333D6A6525C6540700FB06EF /* OrientationKit */ = {isa = PBXFileReference; lastKnownFileType = folder; name = OrientationKit; path = ../..; sourceTree = ""; }; 26 | 333D6A7225C6717E00FB06EF /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 27 | 333D6A7325C6717E00FB06EF /* SceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 28 | 333D6A7825C6745200FB06EF /* HostingController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HostingController.swift; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 333D6A4D25C6539B00FB06EF /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | 333D6A6C25C6543A00FB06EF /* OrientationKit in Frameworks */, 37 | ); 38 | runOnlyForDeploymentPostprocessing = 0; 39 | }; 40 | /* End PBXFrameworksBuildPhase section */ 41 | 42 | /* Begin PBXGroup section */ 43 | 333D6A4725C6539B00FB06EF = { 44 | isa = PBXGroup; 45 | children = ( 46 | 333D6A5225C6539B00FB06EF /* OrientationKitDemo */, 47 | 333D6A5125C6539B00FB06EF /* Products */, 48 | 333D6A6A25C6543A00FB06EF /* Frameworks */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | 333D6A5125C6539B00FB06EF /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 333D6A5025C6539B00FB06EF /* OrientationKitDemo.app */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | 333D6A5225C6539B00FB06EF /* OrientationKitDemo */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 333D6A6525C6540700FB06EF /* OrientationKit */, 64 | 333D6A7225C6717E00FB06EF /* AppDelegate.swift */, 65 | 333D6A7325C6717E00FB06EF /* SceneDelegate.swift */, 66 | 333D6A7825C6745200FB06EF /* HostingController.swift */, 67 | 333D6A5525C6539B00FB06EF /* ContentView.swift */, 68 | 333D6A5725C6539C00FB06EF /* Assets.xcassets */, 69 | 333D6A5C25C6539C00FB06EF /* Info.plist */, 70 | 333D6A5925C6539C00FB06EF /* Preview Content */, 71 | ); 72 | path = OrientationKitDemo; 73 | sourceTree = ""; 74 | }; 75 | 333D6A5925C6539C00FB06EF /* Preview Content */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 333D6A5A25C6539C00FB06EF /* Preview Assets.xcassets */, 79 | ); 80 | path = "Preview Content"; 81 | sourceTree = ""; 82 | }; 83 | 333D6A6A25C6543A00FB06EF /* Frameworks */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | ); 87 | name = Frameworks; 88 | sourceTree = ""; 89 | }; 90 | /* End PBXGroup section */ 91 | 92 | /* Begin PBXNativeTarget section */ 93 | 333D6A4F25C6539B00FB06EF /* OrientationKitDemo */ = { 94 | isa = PBXNativeTarget; 95 | buildConfigurationList = 333D6A5F25C6539C00FB06EF /* Build configuration list for PBXNativeTarget "OrientationKitDemo" */; 96 | buildPhases = ( 97 | 333D6A4C25C6539B00FB06EF /* Sources */, 98 | 333D6A4D25C6539B00FB06EF /* Frameworks */, 99 | 333D6A4E25C6539B00FB06EF /* Resources */, 100 | ); 101 | buildRules = ( 102 | ); 103 | dependencies = ( 104 | 333D6A6925C6543600FB06EF /* PBXTargetDependency */, 105 | ); 106 | name = OrientationKitDemo; 107 | packageProductDependencies = ( 108 | 333D6A6B25C6543A00FB06EF /* OrientationKit */, 109 | ); 110 | productName = OrientationKitDemo; 111 | productReference = 333D6A5025C6539B00FB06EF /* OrientationKitDemo.app */; 112 | productType = "com.apple.product-type.application"; 113 | }; 114 | /* End PBXNativeTarget section */ 115 | 116 | /* Begin PBXProject section */ 117 | 333D6A4825C6539B00FB06EF /* Project object */ = { 118 | isa = PBXProject; 119 | attributes = { 120 | LastSwiftUpdateCheck = 1230; 121 | LastUpgradeCheck = 1230; 122 | TargetAttributes = { 123 | 333D6A4F25C6539B00FB06EF = { 124 | CreatedOnToolsVersion = 12.3; 125 | }; 126 | }; 127 | }; 128 | buildConfigurationList = 333D6A4B25C6539B00FB06EF /* Build configuration list for PBXProject "OrientationKitDemo" */; 129 | compatibilityVersion = "Xcode 9.3"; 130 | developmentRegion = en; 131 | hasScannedForEncodings = 0; 132 | knownRegions = ( 133 | en, 134 | Base, 135 | ); 136 | mainGroup = 333D6A4725C6539B00FB06EF; 137 | productRefGroup = 333D6A5125C6539B00FB06EF /* Products */; 138 | projectDirPath = ""; 139 | projectRoot = ""; 140 | targets = ( 141 | 333D6A4F25C6539B00FB06EF /* OrientationKitDemo */, 142 | ); 143 | }; 144 | /* End PBXProject section */ 145 | 146 | /* Begin PBXResourcesBuildPhase section */ 147 | 333D6A4E25C6539B00FB06EF /* Resources */ = { 148 | isa = PBXResourcesBuildPhase; 149 | buildActionMask = 2147483647; 150 | files = ( 151 | 333D6A5B25C6539C00FB06EF /* Preview Assets.xcassets in Resources */, 152 | 333D6A5825C6539C00FB06EF /* Assets.xcassets in Resources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXResourcesBuildPhase section */ 157 | 158 | /* Begin PBXSourcesBuildPhase section */ 159 | 333D6A4C25C6539B00FB06EF /* Sources */ = { 160 | isa = PBXSourcesBuildPhase; 161 | buildActionMask = 2147483647; 162 | files = ( 163 | 333D6A7925C6745200FB06EF /* HostingController.swift in Sources */, 164 | 333D6A7425C6717E00FB06EF /* AppDelegate.swift in Sources */, 165 | 333D6A5625C6539B00FB06EF /* ContentView.swift in Sources */, 166 | 333D6A7525C6717E00FB06EF /* SceneDelegate.swift in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXTargetDependency section */ 173 | 333D6A6925C6543600FB06EF /* PBXTargetDependency */ = { 174 | isa = PBXTargetDependency; 175 | productRef = 333D6A6825C6543600FB06EF /* OrientationKit */; 176 | }; 177 | /* End PBXTargetDependency section */ 178 | 179 | /* Begin XCBuildConfiguration section */ 180 | 333D6A5D25C6539C00FB06EF /* Debug */ = { 181 | isa = XCBuildConfiguration; 182 | buildSettings = { 183 | ALWAYS_SEARCH_USER_PATHS = NO; 184 | CLANG_ANALYZER_NONNULL = YES; 185 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 186 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 187 | CLANG_CXX_LIBRARY = "libc++"; 188 | CLANG_ENABLE_MODULES = YES; 189 | CLANG_ENABLE_OBJC_ARC = YES; 190 | CLANG_ENABLE_OBJC_WEAK = YES; 191 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 192 | CLANG_WARN_BOOL_CONVERSION = YES; 193 | CLANG_WARN_COMMA = YES; 194 | CLANG_WARN_CONSTANT_CONVERSION = YES; 195 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 196 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 197 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 198 | CLANG_WARN_EMPTY_BODY = YES; 199 | CLANG_WARN_ENUM_CONVERSION = YES; 200 | CLANG_WARN_INFINITE_RECURSION = YES; 201 | CLANG_WARN_INT_CONVERSION = YES; 202 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 203 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 204 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 205 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 206 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 207 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 208 | CLANG_WARN_STRICT_PROTOTYPES = YES; 209 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 210 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 211 | CLANG_WARN_UNREACHABLE_CODE = YES; 212 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 213 | COPY_PHASE_STRIP = NO; 214 | DEBUG_INFORMATION_FORMAT = dwarf; 215 | ENABLE_STRICT_OBJC_MSGSEND = YES; 216 | ENABLE_TESTABILITY = YES; 217 | GCC_C_LANGUAGE_STANDARD = gnu11; 218 | GCC_DYNAMIC_NO_PIC = NO; 219 | GCC_NO_COMMON_BLOCKS = YES; 220 | GCC_OPTIMIZATION_LEVEL = 0; 221 | GCC_PREPROCESSOR_DEFINITIONS = ( 222 | "DEBUG=1", 223 | "$(inherited)", 224 | ); 225 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 226 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 227 | GCC_WARN_UNDECLARED_SELECTOR = YES; 228 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 229 | GCC_WARN_UNUSED_FUNCTION = YES; 230 | GCC_WARN_UNUSED_VARIABLE = YES; 231 | IPHONEOS_DEPLOYMENT_TARGET = 14.3; 232 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 233 | MTL_FAST_MATH = YES; 234 | ONLY_ACTIVE_ARCH = YES; 235 | SDKROOT = iphoneos; 236 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 237 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 238 | }; 239 | name = Debug; 240 | }; 241 | 333D6A5E25C6539C00FB06EF /* Release */ = { 242 | isa = XCBuildConfiguration; 243 | buildSettings = { 244 | ALWAYS_SEARCH_USER_PATHS = NO; 245 | CLANG_ANALYZER_NONNULL = YES; 246 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 247 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 248 | CLANG_CXX_LIBRARY = "libc++"; 249 | CLANG_ENABLE_MODULES = YES; 250 | CLANG_ENABLE_OBJC_ARC = YES; 251 | CLANG_ENABLE_OBJC_WEAK = YES; 252 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 253 | CLANG_WARN_BOOL_CONVERSION = YES; 254 | CLANG_WARN_COMMA = YES; 255 | CLANG_WARN_CONSTANT_CONVERSION = YES; 256 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 257 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 258 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 259 | CLANG_WARN_EMPTY_BODY = YES; 260 | CLANG_WARN_ENUM_CONVERSION = YES; 261 | CLANG_WARN_INFINITE_RECURSION = YES; 262 | CLANG_WARN_INT_CONVERSION = YES; 263 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 264 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 265 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 266 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 267 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 268 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 269 | CLANG_WARN_STRICT_PROTOTYPES = YES; 270 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 271 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 272 | CLANG_WARN_UNREACHABLE_CODE = YES; 273 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 274 | COPY_PHASE_STRIP = NO; 275 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 276 | ENABLE_NS_ASSERTIONS = NO; 277 | ENABLE_STRICT_OBJC_MSGSEND = YES; 278 | GCC_C_LANGUAGE_STANDARD = gnu11; 279 | GCC_NO_COMMON_BLOCKS = YES; 280 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 281 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 282 | GCC_WARN_UNDECLARED_SELECTOR = YES; 283 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 284 | GCC_WARN_UNUSED_FUNCTION = YES; 285 | GCC_WARN_UNUSED_VARIABLE = YES; 286 | IPHONEOS_DEPLOYMENT_TARGET = 14.3; 287 | MTL_ENABLE_DEBUG_INFO = NO; 288 | MTL_FAST_MATH = YES; 289 | SDKROOT = iphoneos; 290 | SWIFT_COMPILATION_MODE = wholemodule; 291 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 292 | VALIDATE_PRODUCT = YES; 293 | }; 294 | name = Release; 295 | }; 296 | 333D6A6025C6539C00FB06EF /* Debug */ = { 297 | isa = XCBuildConfiguration; 298 | buildSettings = { 299 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 300 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 301 | CODE_SIGN_STYLE = Automatic; 302 | DEVELOPMENT_ASSET_PATHS = "\"OrientationKitDemo/Preview Content\""; 303 | DEVELOPMENT_TEAM = UMBZ5WL247; 304 | ENABLE_PREVIEWS = YES; 305 | INFOPLIST_FILE = OrientationKitDemo/Info.plist; 306 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 307 | LD_RUNPATH_SEARCH_PATHS = ( 308 | "$(inherited)", 309 | "@executable_path/Frameworks", 310 | ); 311 | PRODUCT_BUNDLE_IDENTIFIER = com.inamiy.OrientationKitDemo; 312 | PRODUCT_NAME = "$(TARGET_NAME)"; 313 | SWIFT_VERSION = 5.0; 314 | TARGETED_DEVICE_FAMILY = "1,2"; 315 | }; 316 | name = Debug; 317 | }; 318 | 333D6A6125C6539C00FB06EF /* Release */ = { 319 | isa = XCBuildConfiguration; 320 | buildSettings = { 321 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 322 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 323 | CODE_SIGN_STYLE = Automatic; 324 | DEVELOPMENT_ASSET_PATHS = "\"OrientationKitDemo/Preview Content\""; 325 | DEVELOPMENT_TEAM = UMBZ5WL247; 326 | ENABLE_PREVIEWS = YES; 327 | INFOPLIST_FILE = OrientationKitDemo/Info.plist; 328 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 329 | LD_RUNPATH_SEARCH_PATHS = ( 330 | "$(inherited)", 331 | "@executable_path/Frameworks", 332 | ); 333 | PRODUCT_BUNDLE_IDENTIFIER = com.inamiy.OrientationKitDemo; 334 | PRODUCT_NAME = "$(TARGET_NAME)"; 335 | SWIFT_VERSION = 5.0; 336 | TARGETED_DEVICE_FAMILY = "1,2"; 337 | }; 338 | name = Release; 339 | }; 340 | /* End XCBuildConfiguration section */ 341 | 342 | /* Begin XCConfigurationList section */ 343 | 333D6A4B25C6539B00FB06EF /* Build configuration list for PBXProject "OrientationKitDemo" */ = { 344 | isa = XCConfigurationList; 345 | buildConfigurations = ( 346 | 333D6A5D25C6539C00FB06EF /* Debug */, 347 | 333D6A5E25C6539C00FB06EF /* Release */, 348 | ); 349 | defaultConfigurationIsVisible = 0; 350 | defaultConfigurationName = Release; 351 | }; 352 | 333D6A5F25C6539C00FB06EF /* Build configuration list for PBXNativeTarget "OrientationKitDemo" */ = { 353 | isa = XCConfigurationList; 354 | buildConfigurations = ( 355 | 333D6A6025C6539C00FB06EF /* Debug */, 356 | 333D6A6125C6539C00FB06EF /* Release */, 357 | ); 358 | defaultConfigurationIsVisible = 0; 359 | defaultConfigurationName = Release; 360 | }; 361 | /* End XCConfigurationList section */ 362 | 363 | /* Begin XCSwiftPackageProductDependency section */ 364 | 333D6A6825C6543600FB06EF /* OrientationKit */ = { 365 | isa = XCSwiftPackageProductDependency; 366 | productName = OrientationKit; 367 | }; 368 | 333D6A6B25C6543A00FB06EF /* OrientationKit */ = { 369 | isa = XCSwiftPackageProductDependency; 370 | productName = OrientationKit; 371 | }; 372 | /* End XCSwiftPackageProductDependency section */ 373 | }; 374 | rootObject = 333D6A4825C6539B00FB06EF /* Project object */; 375 | } 376 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | @UIApplicationMain 4 | class AppDelegate: UIResponder, UIApplicationDelegate 5 | { 6 | func application( 7 | _ application: UIApplication, 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? 9 | ) -> Bool 10 | { 11 | return true 12 | } 13 | 14 | // MARK: UISceneSession Lifecycle 15 | 16 | func application( 17 | _ application: UIApplication, 18 | configurationForConnecting connectingSceneSession: UISceneSession, 19 | options: UIScene.ConnectionOptions 20 | ) -> UISceneConfiguration 21 | { 22 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/ContentView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import OrientationKit 3 | 4 | struct ContentView: View 5 | { 6 | /// - Note: Call `.withOrientations()` at root view to extract orientations via `@Environment`. 7 | @Environment(\.deviceOrientation) 8 | private var deviceOrientation 9 | 10 | @Environment(\.interfaceOrientation) 11 | private var interfaceOrientation 12 | 13 | /// - Note: To grab other `OrientationManager`'s properties e.g. `deviceMotion`, just access via `EnvironmentObject`. 14 | @EnvironmentObject 15 | private var manager: OrientationManager 16 | 17 | var body: some View 18 | { 19 | return VStack(spacing: 20) { 20 | VStack(spacing: 10) { 21 | hStack( 22 | key: "@Environment deviceOrientation", 23 | value: "\(self.deviceOrientation.debugDescription)" 24 | ) 25 | Divider() 26 | hStack( 27 | key: "UIDevice.current orientation", 28 | value: "\(UIDevice.current.orientation.debugDescription)" 29 | ) 30 | } 31 | .paddingBorder() 32 | 33 | VStack(spacing: 10) { 34 | hStack( 35 | key: "@Environment interfaceOrientation", 36 | value: "\(self.interfaceOrientation.debugDescription)" 37 | ) 38 | Divider() 39 | hStack( 40 | key: "statusBarOrientation", 41 | value: "\(UIApplication.shared.statusBarOrientation.debugDescription)" 42 | ) 43 | } 44 | .paddingBorder() 45 | 46 | accelerometerText() 47 | } 48 | .frame(maxWidth: 500) 49 | } 50 | 51 | private func hStack(key: String, value: String) -> some View 52 | { 53 | HStack(spacing: 20) { 54 | Text(key) 55 | .multilineTextAlignment(.center) 56 | .font(.footnote) 57 | .lineLimit(2) 58 | .frame(minWidth: 0, maxWidth: 140) 59 | 60 | Divider() 61 | .frame(maxHeight: 24) 62 | 63 | Text(value) 64 | .minimumScaleFactor(0.5) 65 | .lineLimit(1) 66 | .frame(minWidth: 0, maxWidth: .infinity) 67 | } 68 | } 69 | 70 | @ViewBuilder 71 | private func accelerometerText() -> some View 72 | { 73 | if let acc = manager.deviceMotion?.gravity { 74 | HStack { 75 | Text("x = \(percent(acc.x))") 76 | Text("y = \(percent(acc.y))") 77 | Text("z = \(percent(acc.z))") 78 | } 79 | } else { 80 | Text("none") 81 | } 82 | } 83 | } 84 | 85 | private func percent(_ x: Double) -> String 86 | { 87 | percentFormatter.string(from: NSNumber(value: Double(x))) ?? "NaN" 88 | } 89 | 90 | private let percentFormatter: NumberFormatter = { 91 | let formatter = NumberFormatter() 92 | formatter.numberStyle = .percent 93 | formatter.minimumIntegerDigits = 1 94 | formatter.maximumIntegerDigits = 3 95 | formatter.maximumFractionDigits = 0 96 | return formatter 97 | }() 98 | 99 | extension View 100 | { 101 | func paddingBorder() -> some View { 102 | self 103 | .padding() 104 | .border(Color.black) 105 | .padding(.horizontal) 106 | } 107 | } 108 | 109 | struct ContentView_Previews: PreviewProvider 110 | { 111 | static var previews: some View 112 | { 113 | Group { 114 | ContentView() 115 | .previewLayout(.fixed(width: 390, height: 844)) 116 | .environment(\.horizontalSizeClass, .regular) 117 | .environment(\.verticalSizeClass, .compact) 118 | 119 | ContentView() 120 | .previewLayout(.fixed(width: 844, height: 390)) 121 | .environment(\.horizontalSizeClass, .compact) 122 | .environment(\.verticalSizeClass, .compact) 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/HostingController.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Combine 3 | import SwiftUI 4 | 5 | class HostingController: UIHostingController where Content: View 6 | { 7 | override var shouldAutorotate: Bool 8 | { 9 | return true 10 | } 11 | 12 | override var supportedInterfaceOrientations: UIInterfaceOrientationMask 13 | { 14 | return [.all] 15 | // return [.portrait, .landscapeRight] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UILaunchStoryboardName 33 | LaunchScreen 34 | UISceneConfigurationName 35 | Default Configuration 36 | UISceneDelegateClassName 37 | $(PRODUCT_MODULE_NAME).SceneDelegate 38 | 39 | 40 | 41 | 42 | UIApplicationSupportsIndirectInputEvents 43 | 44 | UILaunchScreen 45 | 46 | UIRequiredDeviceCapabilities 47 | 48 | armv7 49 | 50 | UISupportedInterfaceOrientations 51 | 52 | UIInterfaceOrientationPortrait 53 | UIInterfaceOrientationLandscapeLeft 54 | UIInterfaceOrientationLandscapeRight 55 | 56 | UISupportedInterfaceOrientations~ipad 57 | 58 | UIInterfaceOrientationPortrait 59 | UIInterfaceOrientationPortraitUpsideDown 60 | UIInterfaceOrientationLandscapeLeft 61 | UIInterfaceOrientationLandscapeRight 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Examples/OrientationKitDemo/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Combine 3 | import SwiftUI 4 | 5 | class SceneDelegate: UIResponder, UIWindowSceneDelegate 6 | { 7 | var window: UIWindow? 8 | 9 | func scene( 10 | _ scene: UIScene, 11 | willConnectTo session: UISceneSession, 12 | options connectionOptions: UIScene.ConnectionOptions 13 | ) 14 | { 15 | if let windowScene = scene as? UIWindowScene { 16 | let window = UIWindow(windowScene: windowScene) 17 | 18 | let hostingVC = HostingController( 19 | // NOTE: 20 | // `withOrientations()` provides 21 | // @Environment(\.deviceOrientation) and @Environment(\.interfaceOrientation). 22 | rootView: ContentView().withOrientations() 23 | ) 24 | window.rootViewController = hostingVC 25 | 26 | self.window = window 27 | window.makeKeyAndVisible() 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Yasuhiro Inami 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:6.0 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "OrientationKit", 7 | platforms: [ 8 | .iOS(.v13), .macCatalyst(.v14), .tvOS(.v13), .watchOS(.v6) 9 | ], 10 | products: [ 11 | .library( 12 | name: "OrientationKit", 13 | targets: ["OrientationKit"]), 14 | ], 15 | dependencies: [], 16 | targets: [ 17 | .target( 18 | name: "OrientationKit", 19 | dependencies: []), 20 | .testTarget( 21 | name: "OrientationKitTests", 22 | dependencies: ["OrientationKit"]), 23 | ] 24 | ) 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OrientationKit 2 | iOS device/interface/image/video orientation translation & detection using CoreMotion + SwiftUI + Combine. 3 | 4 | ## How to Use 5 | 6 | ```swift 7 | @main 8 | struct DemoApp: App { 9 | var body: some Scene { 10 | WindowGroup { 11 | // Call `.withOrientations()` to start observing 12 | // CoreMotion-based device orientation changes. 13 | // This keeps notifying even when device orientation 14 | // is locked. 15 | ContentView() 16 | .withOrientations() 17 | } 18 | } 19 | } 20 | 21 | struct ContentView: View { 22 | @Environment(\.deviceOrientation) 23 | private var deviceOrientation 24 | 25 | @Environment(\.interfaceOrientation) 26 | private var interfaceOrientation 27 | 28 | /// - Note: To grab other `OrientationManager`'s properties 29 | /// e.g. `deviceMotion`, just access via `@EnvironmentObject`. 30 | @EnvironmentObject 31 | private var manager: OrientationManager 32 | 33 | var body: some View { 34 | ... 35 | } 36 | } 37 | ``` 38 | 39 | ## Example 40 | 41 | 42 | 43 | See [Examples](Examples) for more information. 44 | 45 | ## License 46 | 47 | [MIT](LICENSE) -------------------------------------------------------------------------------- /Sources/OrientationKit/Environment.swift: -------------------------------------------------------------------------------- 1 | #if canImport(SwiftUI) 2 | 3 | import SwiftUI 4 | import CoreMotion 5 | 6 | extension EnvironmentValues 7 | { 8 | /// CoreMotion-driven `UIDeviceOrientation` (can also observe changes while orientation lock). 9 | public var deviceOrientation: UIDeviceOrientation 10 | { 11 | get { 12 | self[DeviceOrientationKey.self] 13 | } 14 | set { 15 | self[DeviceOrientationKey.self] = newValue 16 | } 17 | } 18 | 19 | /// First window's `UIInterfaceOrientation`. 20 | public var interfaceOrientation: UIInterfaceOrientation 21 | { 22 | get { 23 | self[InterfaceOrientationKey.self] 24 | } 25 | set { 26 | self[InterfaceOrientationKey.self] = newValue 27 | } 28 | } 29 | } 30 | 31 | private struct DeviceOrientationKey: EnvironmentKey 32 | { 33 | public static let defaultValue: UIDeviceOrientation = .unknown 34 | } 35 | 36 | private struct InterfaceOrientationKey: EnvironmentKey 37 | { 38 | public static let defaultValue: UIInterfaceOrientation = .unknown 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Sources/OrientationKit/Orientation.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import AVFoundation 3 | 4 | // MARK: - CGImagePropertyOrientation 5 | 6 | extension CGImagePropertyOrientation 7 | { 8 | public init?(uiImageOrientation: UIImage.Orientation) 9 | { 10 | switch uiImageOrientation { 11 | case .up: 12 | self = .up 13 | case .upMirrored: 14 | self = .upMirrored 15 | case .down: 16 | self = .down 17 | case .downMirrored: 18 | self = .downMirrored 19 | case .left: 20 | self = .left 21 | case .leftMirrored: 22 | self = .leftMirrored 23 | case .right: 24 | self = .right 25 | case .rightMirrored: 26 | self = .rightMirrored 27 | @unknown default: 28 | return nil 29 | } 30 | } 31 | 32 | public var uiImageOrientation: UIImage.Orientation 33 | { 34 | UIImage.Orientation.init(cgImageOrientation: self) 35 | } 36 | } 37 | 38 | // MARK: - AVCaptureVideoOrientation 39 | 40 | extension AVCaptureVideoOrientation: @retroactive CustomDebugStringConvertible 41 | { 42 | public var debugDescription: String 43 | { 44 | switch self { 45 | case .portrait: 46 | return "portrait" 47 | case .portraitUpsideDown: 48 | return "portraitUpsideDown" 49 | case .landscapeRight: 50 | return "landscapeRight" 51 | case .landscapeLeft: 52 | return "landscapeLeft" 53 | @unknown default: 54 | return "unknown" 55 | } 56 | } 57 | 58 | public init?(deviceOrientation: UIDeviceOrientation) 59 | { 60 | switch deviceOrientation { 61 | case .portrait: 62 | self = .portrait 63 | case .portraitUpsideDown: 64 | self = .portraitUpsideDown 65 | case .landscapeLeft: 66 | self = .landscapeRight 67 | case .landscapeRight: 68 | self = .landscapeLeft 69 | case .faceUp, 70 | .faceDown, 71 | .unknown: 72 | return nil 73 | @unknown default: 74 | return nil 75 | } 76 | } 77 | 78 | public init?(interfaceOrientation: UIInterfaceOrientation) 79 | { 80 | switch interfaceOrientation { 81 | case .portrait: 82 | self = .portrait 83 | case .portraitUpsideDown: 84 | self = .portraitUpsideDown 85 | case .landscapeLeft: 86 | self = .landscapeLeft 87 | case .landscapeRight: 88 | self = .landscapeRight 89 | case .unknown: 90 | return nil 91 | @unknown default: 92 | return nil 93 | } 94 | } 95 | } 96 | 97 | // MARK: - UIDeviceOrientation 98 | 99 | extension UIDeviceOrientation: @retroactive CustomDebugStringConvertible 100 | { 101 | public var debugDescription: String 102 | { 103 | switch self { 104 | case .portrait: 105 | return "portrait" 106 | case .portraitUpsideDown: 107 | return "portraitUpsideDown" 108 | case .landscapeLeft: 109 | return "landscapeLeft" 110 | case .landscapeRight: 111 | return "landscapeRight" 112 | case .faceUp: 113 | return "faceUp" 114 | case .faceDown: 115 | return "faceDown" 116 | case .unknown: 117 | fallthrough 118 | @unknown default: 119 | return "unknown" 120 | } 121 | } 122 | 123 | public var interfaceOrientation: UIInterfaceOrientation 124 | { 125 | UIInterfaceOrientation.init(deviceOrientation: self) 126 | } 127 | 128 | public var avCaptureVideoOrientation: AVCaptureVideoOrientation? 129 | { 130 | AVCaptureVideoOrientation.init(deviceOrientation: self) 131 | } 132 | 133 | public var estimatedImageOrientation: UIImage.Orientation? 134 | { 135 | switch self { 136 | case .portrait: 137 | return .right 138 | case .portraitUpsideDown: 139 | return .left 140 | case .landscapeLeft: 141 | return .up 142 | case .landscapeRight: 143 | return .down 144 | case .faceUp: 145 | return nil 146 | case .faceDown: 147 | return nil 148 | case .unknown: 149 | fallthrough 150 | @unknown default: 151 | return nil 152 | } 153 | } 154 | } 155 | 156 | // MARK: - UIInterfaceOrientation 157 | 158 | extension UIInterfaceOrientation: @retroactive CustomDebugStringConvertible 159 | { 160 | public var debugDescription: String 161 | { 162 | switch self { 163 | case .portrait: 164 | return "portrait" 165 | case .portraitUpsideDown: 166 | return "portraitUpsideDown" 167 | case .landscapeLeft: 168 | return "landscapeLeft" 169 | case .landscapeRight: 170 | return "landscapeRight" 171 | case .unknown: 172 | fallthrough 173 | @unknown default: 174 | return "unknown" 175 | } 176 | } 177 | 178 | public init(deviceOrientation: UIDeviceOrientation) 179 | { 180 | switch deviceOrientation { 181 | case .portrait: 182 | self = .portrait 183 | case .portraitUpsideDown: 184 | self = .portraitUpsideDown 185 | case .landscapeLeft: 186 | self = .landscapeRight 187 | case .landscapeRight: 188 | self = .landscapeLeft 189 | case .faceUp, 190 | .faceDown, 191 | .unknown: 192 | self = .unknown 193 | @unknown default: 194 | self = .unknown 195 | } 196 | } 197 | 198 | public var avCaptureVideoOrientation: AVCaptureVideoOrientation? 199 | { 200 | AVCaptureVideoOrientation.init(interfaceOrientation: self) 201 | } 202 | } 203 | 204 | // MARK: - UIImage.Orientation 205 | 206 | extension UIImage.Orientation 207 | { 208 | public init(cgImageOrientation: CGImagePropertyOrientation) 209 | { 210 | switch cgImageOrientation { 211 | case .up: 212 | self = .up 213 | case .upMirrored: 214 | self = .upMirrored 215 | case .down: 216 | self = .down 217 | case .downMirrored: 218 | self = .downMirrored 219 | case .left: 220 | self = .left 221 | case .leftMirrored: 222 | self = .leftMirrored 223 | case .right: 224 | self = .right 225 | case .rightMirrored: 226 | self = .rightMirrored 227 | } 228 | } 229 | 230 | public init?(deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position) 231 | { 232 | switch cameraPosition { 233 | case .back: 234 | switch deviceOrientation { 235 | case .portrait: 236 | self = .right 237 | case .portraitUpsideDown: 238 | self = .left 239 | case .landscapeLeft: 240 | self = .up 241 | case .landscapeRight: 242 | self = .down 243 | case .faceUp, 244 | .faceDown, 245 | .unknown: 246 | fallthrough 247 | @unknown default: 248 | return nil 249 | } 250 | 251 | case .front: 252 | switch deviceOrientation { 253 | case .portrait: 254 | self = .leftMirrored 255 | case .portraitUpsideDown: 256 | self = .rightMirrored 257 | case .landscapeLeft: 258 | self = .downMirrored 259 | case .landscapeRight: 260 | self = .upMirrored 261 | case .faceUp, 262 | .faceDown, 263 | .unknown: 264 | fallthrough 265 | @unknown default: 266 | return nil 267 | } 268 | 269 | case .unspecified: 270 | fallthrough 271 | 272 | @unknown default: 273 | return nil 274 | } 275 | } 276 | 277 | public var cgImagePropertyOrientation: CGImagePropertyOrientation? 278 | { 279 | CGImagePropertyOrientation.init(uiImageOrientation: self) 280 | } 281 | 282 | public var estimatedDeviceOrientation: UIDeviceOrientation 283 | { 284 | switch self { 285 | case .up: 286 | return .landscapeLeft 287 | case .down: 288 | return .landscapeRight 289 | case .left: 290 | return .portraitUpsideDown 291 | case .right: 292 | return .portrait; 293 | default: 294 | return .unknown 295 | } 296 | } 297 | } 298 | 299 | // MARK: - SwiftUI.Image.Orientation 300 | 301 | #if canImport(SwiftUI) 302 | 303 | import SwiftUI 304 | 305 | extension SwiftUI.Image.Orientation 306 | { 307 | public init(cgImageOrientation: CGImagePropertyOrientation) 308 | { 309 | switch cgImageOrientation { 310 | case .up: 311 | self = .up 312 | case .down: 313 | self = .down 314 | case .left: 315 | self = .left 316 | case .right: 317 | self = .right 318 | case .upMirrored: 319 | self = .upMirrored 320 | case .downMirrored: 321 | self = .downMirrored 322 | case .leftMirrored: 323 | self = .leftMirrored 324 | case .rightMirrored: 325 | self = .rightMirrored 326 | } 327 | } 328 | 329 | public var cgImageOrientation: CGImagePropertyOrientation 330 | { 331 | switch self { 332 | case .up: 333 | return .up 334 | case .upMirrored: 335 | return .upMirrored 336 | case .down: 337 | return .down 338 | case .downMirrored: 339 | return .downMirrored 340 | case .left: 341 | return .left 342 | case .leftMirrored: 343 | return .leftMirrored 344 | case .right: 345 | return .right 346 | case .rightMirrored: 347 | return .rightMirrored 348 | } 349 | } 350 | } 351 | 352 | #endif 353 | -------------------------------------------------------------------------------- /Sources/OrientationKit/OrientationManager.swift: -------------------------------------------------------------------------------- 1 | #if canImport(Combine) 2 | 3 | import UIKit 4 | import Combine 5 | import CoreMotion 6 | 7 | /// `CMDeviceMotion` wrapper to comform `ObservableObject`. 8 | @MainActor 9 | public final class OrientationManager: ObservableObject 10 | { 11 | @Published 12 | public private(set) var deviceOrientation: UIDeviceOrientation = .unknown 13 | 14 | @Published 15 | public private(set) var deviceMotion: CMDeviceMotion? 16 | 17 | private var coreMotionManager: CMMotionManager? 18 | 19 | public init() {} 20 | 21 | public func start( 22 | strategy: Strategy = .systemLike, 23 | interval: TimeInterval, 24 | queue: OperationQueue = .current ?? .main 25 | ) 26 | { 27 | guard !self.isRunning else { return } 28 | 29 | self.deviceOrientation = UIDevice.current.orientation 30 | 31 | let coreMotionManager = CMMotionManager() 32 | self.coreMotionManager = coreMotionManager 33 | 34 | coreMotionManager.deviceMotionUpdateInterval = interval 35 | 36 | coreMotionManager.startDeviceMotionUpdates(to: queue) { [weak self] data, error in 37 | guard let self = self, let data = data else { return } 38 | 39 | self.deviceMotion = data 40 | 41 | let deviceOrientation = strategy.calculate(data.gravity) 42 | 43 | if let deviceOrientation = deviceOrientation { 44 | self.deviceOrientation = deviceOrientation 45 | } 46 | } 47 | } 48 | 49 | public func stop() 50 | { 51 | self.coreMotionManager?.stopDeviceMotionUpdates() 52 | self.coreMotionManager = nil 53 | self.deviceOrientation = .unknown 54 | } 55 | 56 | private var isRunning: Bool 57 | { 58 | self.coreMotionManager != nil 59 | } 60 | } 61 | 62 | // MARK: - Strategy 63 | 64 | extension OrientationManager 65 | { 66 | /// `UIDeviceOrientation` estimation strategy. 67 | public struct Strategy: Sendable 68 | { 69 | /// - Note: Returning `nil` means "no update". 70 | fileprivate let calculate: @Sendable (_ gravity: CMAcceleration) -> UIDeviceOrientation? 71 | 72 | public init(calculate: @escaping @Sendable (_ gravity: CMAcceleration) -> UIDeviceOrientation?) 73 | { 74 | self.calculate = calculate 75 | } 76 | } 77 | } 78 | 79 | // MARK: Strategy Presets 80 | 81 | extension OrientationManager.Strategy 82 | { 83 | /// Largest axis wins the game. 84 | public static let largestAxis = Self.init(calculate: estimateLargestAxis) 85 | 86 | /// Mimics iOS system orientation change. 87 | public static let systemLike = Self.init(calculate: estimateSystemLike) 88 | } 89 | 90 | private func estimateLargestAxis(gravity g: CMAcceleration) -> UIDeviceOrientation? 91 | { 92 | if abs(g.z) > abs(g.x) && abs(g.z) > abs(g.y) { 93 | if (g.z > 0) { 94 | return .faceDown 95 | } 96 | else { 97 | return .faceUp 98 | } 99 | } 100 | else if abs(g.x) > abs(g.y) { 101 | if (g.x > 0) { 102 | return .landscapeRight 103 | } 104 | else { 105 | return .landscapeLeft 106 | } 107 | } 108 | else { 109 | if (g.y > 0) { 110 | return .portraitUpsideDown 111 | } 112 | else { 113 | return .portrait 114 | } 115 | } 116 | } 117 | 118 | private func estimateSystemLike(gravity g: CMAcceleration) -> UIDeviceOrientation? 119 | { 120 | // Just a quick guess, but mimics iPhone's behavior fairly well. 121 | if abs(g.z) > 0.88 { 122 | if (g.z > 0) { 123 | return .faceDown 124 | } 125 | else { 126 | return .faceUp 127 | } 128 | } 129 | 130 | /* 131 | 132 | Linear estimation from the following measured data (all in %) 133 | when orientation change occurs from the fixed axis. 134 | 135 | z x y 136 | ----------------- 137 | 0 88 48 138 | 10 88 48 139 | 20 86 46 140 | 30 84 44 141 | 40 82 41 142 | 50 78 37 143 | 60 73 33 144 | 70 66 26 145 | 80 -- -- (no orientation change) 146 | 147 | FIXME: A better algorithm! 148 | 149 | */ 150 | 151 | let x_ = abs(g.x) 152 | let y_ = abs(g.y) 153 | let threshold = 1.07 + 0.1 * abs(g.z) 154 | 155 | if x_ > y_ && atan2(x_, y_) > threshold { 156 | if (g.x > 0) { 157 | return .landscapeRight 158 | } 159 | else { 160 | return .landscapeLeft 161 | } 162 | } 163 | else if y_ > x_ && atan2(y_, x_) > threshold { 164 | if (g.y > 0) { 165 | return .portraitUpsideDown 166 | } 167 | else { 168 | return .portrait 169 | } 170 | } 171 | 172 | return nil 173 | } 174 | 175 | #endif 176 | -------------------------------------------------------------------------------- /Sources/OrientationKit/WithOrientations.swift: -------------------------------------------------------------------------------- 1 | #if canImport(SwiftUI) 2 | 3 | import SwiftUI 4 | 5 | extension View 6 | { 7 | /// Registers `OrientationManager` to provide 8 | /// `@Environment(\.deviceOrientation)` and `@Environment(\.interfaceOrientation)`. 9 | public func withOrientations( 10 | phase: WithOrientations.Phase = .start(strategy: .systemLike, interval: 0.1, queue: .main) 11 | ) -> some View 12 | { 13 | self.modifier(WithOrientations(phase: phase)) 14 | } 15 | } 16 | 17 | /// Registers `OrientationManager` to provide 18 | /// `@Environment(\.deviceOrientation)` and `@Environment(\.interfaceOrientation)`. 19 | public struct WithOrientations: ViewModifier 20 | { 21 | @ObservedObject 22 | private var orientationManager: OrientationManager 23 | 24 | @State 25 | private var interfaceOrientation: UIInterfaceOrientation = .unknown 26 | 27 | private let phase: Phase 28 | 29 | init(phase: Phase) 30 | { 31 | self.orientationManager = OrientationManager() 32 | self.phase = phase 33 | } 34 | 35 | @ViewBuilder 36 | public func body(content: Content) -> some View 37 | { 38 | content 39 | .environment( 40 | \.deviceOrientation, 41 | orientationManager.deviceOrientation 42 | ) 43 | .environment( 44 | \.interfaceOrientation, 45 | interfaceOrientation 46 | ) 47 | .environmentObject(self.orientationManager) 48 | .onReceive(self.orientationManager.$deviceOrientation) { deviceOrientation in 49 | let interfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? .unknown 50 | 51 | if interfaceOrientation != self.interfaceOrientation { 52 | self.interfaceOrientation = interfaceOrientation 53 | } 54 | } 55 | .onAppear { 56 | switch self.phase { 57 | case let .start(strategy, interval, queue): 58 | self.orientationManager.start(strategy: strategy, interval: interval, queue: queue) 59 | case .stop: 60 | self.orientationManager.stop() 61 | } 62 | } 63 | } 64 | } 65 | 66 | extension WithOrientations 67 | { 68 | public enum Phase: Equatable 69 | { 70 | case start( 71 | strategy: OrientationManager.Strategy = .systemLike, 72 | interval: TimeInterval = 0.1, 73 | queue: OperationQueue = .main 74 | ) 75 | 76 | case stop 77 | 78 | public static func == (lhs: Self, rhs: Self) -> Bool 79 | { 80 | switch (lhs, rhs) { 81 | case (.start, .start): return true 82 | case (.stop, .stop): return true 83 | default: return false 84 | } 85 | } 86 | } 87 | } 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import OrientationKitTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += OrientationKitTests.allTests() 7 | XCTMain(tests) 8 | -------------------------------------------------------------------------------- /Tests/OrientationKitTests/OrientationKitTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import OrientationKit 3 | 4 | final class OrientationKitTests: XCTestCase { 5 | func testExample() { 6 | // This is an example of a functional test case. 7 | // Use XCTAssert and related functions to verify your tests produce the correct 8 | // results. 9 | } 10 | 11 | static var allTests = [ 12 | ("testExample", testExample), 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /Tests/OrientationKitTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(OrientationKitTests.allTests), 7 | ] 8 | } 9 | #endif 10 | --------------------------------------------------------------------------------