iOSDC 2023 JapaniOS16で変わった画面の向きを操作する方法2023/09/02 17:20〜 Track B ルーキーズLTRas
View Slide
自己紹介Ras (@ras0q) / Kira Kawai• 東京工業大学 (2020/04 ~)• 情報通信工学専攻• デジタル創作同好会 traP• 株式会社ピクシブ (2022/04 ~)• iOSエンジニアアルバイトとして参加• iOSDC Japan (2022 ~)• 参加2回目• 初登壇 🎉 🎉 🎉
iOS16から画面の向き操作APIが(正式に)追加されました
画面の向き(orientation)を回転させるアプリ• 通常時は縦向きのみを許可• orientation = .portrait• 回転ボタンを押すことで強制的に横向きにする• orientation = .landscape• toggleOrientations(後述)が発火されるGitHub: ras0q/iosdc2023-interface-orientation題材
class ViewController: UIViewController {override var supportedInterfaceOrientations {return currentOrientation}実装方針 (共通)supportedInterfaceOrientations Ͱ͖Λ੍ޚ͢ΔViewControllerごとに特定の向きのみを許可することができる固定値であればInfo.plistに書いて制御することもできる
func toggleOrientations() {// ͖ͷঢ়ଶΛtoggle͢ΔcurrentOrientation = ...// ࣮ࡍʹ͖Λมߋ͢ΔchangeOrientation(to: currentOrientation)}実装方針 (共通)toggleOrientations Ͱ͖Λมߋ͢ΔճసϘλϯ͕ԡ͞Εͨͱ͖ʹൃՐ͢Δ
これまで
class ViewController: UIViewController {override var shouldAutorotate: Bool {// ճసϘλϯΛԡ͢લ͚ͩtrueʹ͢Δreturn canRotate}iOS15以前の向き操作 - 制御編shouldAutorotate をoverrideする端末を傾けたときの自動回転を制御する真偽値今回の題材では基本縦向きのみなのでfalseʹ͓ͯ͘͠回転時のみ一時的にtrueにして回転を許容する 🤔
// ճస࣌ͷΈshouldAutorotateΛڐ༰͢Δඞཁ͕͋ΔcanRotate = trueUIDevice.current.setValue(orientation.rawValue,forKey: "orientation")canRotate = falseiOS15以前の向き操作 - 操作編UIDevice.current.setValue で向きを変更するなんでもセットメソッド 🤔あまりにもHACK排他制御しなくて大丈夫?
currentOrientation: .portraitsupportedInterfaceOrientations: .portraitshouldAutorotate: false
👆push!currentOrientation: .portraitsupportedInterfaceOrientations: .portraitshouldAutorotate: false
currentOrientation: .portraitsupportedInterfaceOrientations: .portraitshouldAutorotate: falsetoggleOrientations()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraitshouldAutorotate: falsetoggleOrientations()ココはまだ読み込まれていないので変わっていない状態を変更(プロパティを書き換えただけ)👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraitshouldAutorotate: truetoggleOrientations()一時的に回転を許可 ココはまだ読み込まれていないので変わっていない👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraitshouldAutorotate: truetoggleOrientations()ココはまだ読み込まれていないので変わっていないUIDevice.current.setValue()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraitshouldAutorotate: truetoggleOrientations()読み込まれたUIDevice.current.setValue()変更後の向きがサポートされているか確認👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspaceshouldAutorotate: truetoggleOrientations()UIDevice.current.setValue()変更後の向きがサポートされているか確認参照👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspaceshouldAutorotate: truetoggleOrientations()UIDevice.current.setValue()変更後の向きがサポートされているか確認OK!👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspaceshouldAutorotate: truetoggleOrientations()回転できるか確認UIDevice.current.setValue()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspaceshouldAutorotate: truetoggleOrientations()回転できるか確認OK!UIDevice.current.setValue()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspaceshouldAutorotate: truetoggleOrientations()回転しろ!命令だ!!😡UIDevice.current.setValue()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspaceshouldAutorotate: true🎉
※ެࣜͰαϙʔτ͞Εͨํ๏Ͱ͋Γ·ͤΜそして......
iOS16登場
shouldAutorotate setNeedsUpdateOfSupportedInterfaceOrientationsUIDevice.current.setValue requestGeometryUpdatedeprecated 😵unavailable 🤯~iOS15 iOS16~
iOS16以降の向き操作 - 制御編setNeedsUpdateOfSupportedInterfaceOrientations を呼ぶ回転できる向きが変更されたことを明示的にViewControllerに知らせるメソッドsupportedInterfaceOrientationsが再度読まれる// ͖ͷঢ়ଶΛมߋ͢Δ (·ͩViewControllerʹมߋ͕Θ͍ͬͯͳ͍)currentOrientation = ...// ໌ࣔతʹsupportedInterfaceOrientationsΛ࠶ಡΈࠐΈมߋΛ͑ΔsetNeedsUpdateOfSupportedInterfaceOrientations()
// ͖ͷঢ়ଶΛมߋ͢Δ (·ͩViewControllerʹมߋ͕Θ͍ͬͯͳ͍)currentOrientation = ...// ໌ࣔతʹsupportedInterfaceOrientationsΛ࠶ಡΈࠐΈมߋΛ͑ΔsetNeedsUpdateOfSupportedInterfaceOrientations()// ͖ͷมߋΛཁٻ͢ΔwindowScene.requestGeometryUpdate(.iOS(interfaceOrientations: ...))iOS16以降の向き操作 - 操作編requestGeometryUpdate で向きを変更する指定した向きへの回転を要求するメソッド許可されていない場合は設定したerrorHandler͕ݺΕΔ (optional)
currentOrientation: .portraitsupportedInterfaceOrientations: .portraitshouldAutorotate: falsedeprecated
currentOrientation: .portraitsupportedInterfaceOrientations: .portrait👆push!
currentOrientation: .portraitsupportedInterfaceOrientations: .portraittoggleOrientations()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraittoggleOrientations()ココはまだ読み込まれていないので変わっていない状態を変更(プロパティを書き換えただけ)👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraittoggleOrientations()サポートの変更を明示的に通知ココはまだ読み込まれていないので変わっていないsetNeedsUpdateOfSupportedInterfaceOrientations()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .portraittoggleOrientations()サポートの変更を明示的に通知setNeedsUpdateOfSupportedInterfaceOrientations()読み込まれたリロード👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspacetoggleOrientations()サポートの変更を明示的に通知setNeedsUpdateOfSupportedInterfaceOrientations()リロード参照👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspacetoggleOrientations()setNeedsUpdateOfSupportedInterfaceOrientations()requestGeometryUpdate()👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspacetoggleOrientations()setNeedsUpdateOfSupportedInterfaceOrientations()requestGeometryUpdate()変更してください🙇👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspacetoggleOrientations()setNeedsUpdateOfSupportedInterfaceOrientations()requestGeometryUpdate()変更後の向きがサポートされているか確認👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspacetoggleOrientations()setNeedsUpdateOfSupportedInterfaceOrientations()requestGeometryUpdate()変更後の向きがサポートされているか確認OK!👆push!
currentOrientation: .landspacesupportedInterfaceOrientations: .landspace🎉
iOS16以降のAPIで安全に回転しよう🫨
ご清聴ありがとうございました!フィードバックお待ちしています
• setNeedsUpdateOfSupportedInterfaceOrientations() | Apple Developer Documentation• requestGeometryUpdate(_:errorHandler:) | Apple Developer Documentation• Swiftのorientation操作 (zenn.dev)• iOS15以前の話です• iOS16をサポートしよう! - bravesoft blog• ほとんどこれ参考記事など