Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
iOS16で変わった画面の向きを操作する方法 - iOSDC Japan 2023
Search
ras0q
September 03, 2023
Technology
0
2.9k
iOS16で変わった画面の向きを操作する方法 - iOSDC Japan 2023
ras0q
September 03, 2023
Tweet
Share
More Decks by ras0q
See All by ras0q
PencilKitで実装するPDFへの手書き注釈 / Handwritten-annotations to PDF with PencilKit
ras0q
0
660
Embedded FrameworkからSPMへ 段階的移行の軌跡
ras0q
0
370
Other Decks in Technology
See All in Technology
AI自体のOps 〜LLMアプリの運用、AWSサービスとOSSの使い分け〜
minorun365
PRO
10
1.4k
クラウド関連のインシデントケースを収集して見えてきたもの
lhazy
10
2.2k
JAWS FESTA 2024「バスロケ」GPS×サーバーレスの開発と運用の舞台裏/jawsfesta2024-bus-gps-serverless
ma2shita
3
420
OCI IAM Identity Domains Entra IDとの認証連携設定手順 / Identity Domain Federation settings with Entra ID
oracle4engineer
PRO
1
1.4k
生成AI×財務経理:PoCで挑むSlack AI Bot開発と現場巻き込みのリアル
pohdccoe
1
880
RaspberryPi CM4(CM5も)面白いぞ!
nonnoise
1
280
開発者のための FinOps/FinOps for Engineers
oracle4engineer
PRO
2
300
Amazon Athenaから利用時のGlueのIcebergテーブルのメンテナンスについて
nayuts
0
150
QAエンジニアが スクラムマスターをすると いいなぁと思った話
____rina____
0
240
Platform Engineeringで クラウドの「楽しくない」を解消しよう
jacopen
5
310
VPoEの引き継ぎでやったこと、わかったこと
saitoryc
1
280
AI_Agent_の作り方_近藤憲児
kenjikondobai
6
1.2k
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
Speed Design
sergeychernyshev
28
820
Testing 201, or: Great Expectations
jmmastey
42
7.2k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
400
Git: the NoSQL Database
bkeepers
PRO
429
65k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Music & Morning Musume
bryan
46
6.4k
What's in a price? How to price your products and services
michaelherold
244
12k
Transcript
iOSDC 2023 Japan iOS16で変わった 画面の向きを操作する方法 2023/09/02 17:20〜 Track B ルーキーズLT
Ras
自己紹介 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 = true UIDevice.current.setValue( orientation.rawValue, forKey: "orientation" )
canRotate = false iOS15以前の向き操作 - 操作 編 UIDevice.current.setValue で向きを変更する なんでもセットメソッド 🤔 あまりにもHACK 排他制御しなくて大丈夫?
currentOrientation: .portrait supportedInterfaceOrientations: .portrait shouldAutorotate: false
👆 push! currentOrientation: .portrait supportedInterfaceOrientations: .portrait shouldAutorotate: false
currentOrientation: .portrait supportedInterfaceOrientations: .portrait shouldAutorotate: false toggleOrientations() 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait shouldAutorotate: false toggleOrientations() ココはまだ読み込まれていない ので変わっていない 状態を変更
(プロパティを書き換えただけ) 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait shouldAutorotate: true toggleOrientations() 一時的に回転を許可 ココはまだ読み込まれていない ので変わっていない
👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait shouldAutorotate: true toggleOrientations() ココはまだ読み込まれていない ので変わっていない UIDevice.current.setValue()
👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait shouldAutorotate: true toggleOrientations() 読み込まれた UIDevice.current.setValue() 変更後の向きが
サ ポ ートされているか確認 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace shouldAutorotate: true toggleOrientations() UIDevice.current.setValue() 変更後の向きが サ
ポ ートされているか確認 参照 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace shouldAutorotate: true toggleOrientations() UIDevice.current.setValue() 変更後の向きが サ
ポ ートされているか確認 OK! 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace shouldAutorotate: true toggleOrientations() 回転できるか確認 UIDevice.current.setValue() 👆
push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace shouldAutorotate: true toggleOrientations() 回転できるか確認 OK! UIDevice.current.setValue()
👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace shouldAutorotate: true toggleOrientations() 回転しろ! 命令だ!!😡 UIDevice.current.setValue()
👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace shouldAutorotate: true 🎉
※ެࣜͰαϙʔτ͞Εͨํ๏Ͱ͋Γ·ͤΜ そして......
iOS16登場
iOS16から 画面の向き操作APIが (正式に)追加されました
shouldAutorotate setNeedsUpdateOfSupportedInterfaceOrientations UIDevice.current.setValue requestGeometryUpdate deprecated 😵 unavailable 🤯 ~iOS15 iOS16
~
iOS16以降の向き操作 - 制御 編 setNeedsUpdateOfSupportedInterfaceOrientations を呼ぶ 回転できる向きが変更されたことを明示的にViewControllerに知らせるメソッド supportedInterfaceOrientationsが再度読まれる // ͖ͷঢ়ଶΛมߋ͢Δ
(·ͩViewControllerʹมߋ͕Θ͍ͬͯͳ͍) currentOrientation = ... // ໌ࣔతʹsupportedInterfaceOrientationsΛ࠶ಡΈࠐΈมߋΛ͑Δ setNeedsUpdateOfSupportedInterfaceOrientations()
// ͖ͷঢ়ଶΛมߋ͢Δ (·ͩViewControllerʹมߋ͕Θ͍ͬͯͳ͍) currentOrientation = ... // ໌ࣔతʹsupportedInterfaceOrientationsΛ࠶ಡΈࠐΈมߋΛ͑Δ setNeedsUpdateOfSupportedInterfaceOrientations() //
͖ͷมߋΛཁٻ͢Δ windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: ...)) iOS16以降の向き操作 - 操作 編 requestGeometryUpdate で向きを変更する 指定した向きへの回転を要求するメソッド 許可されていない場合は設定したerrorHandler͕ݺΕΔ (optional)
currentOrientation: .portrait supportedInterfaceOrientations: .portrait shouldAutorotate: false deprecated
currentOrientation: .portrait supportedInterfaceOrientations: .portrait 👆 push!
currentOrientation: .portrait supportedInterfaceOrientations: .portrait toggleOrientations() 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait toggleOrientations() ココはまだ読み込まれていない ので変わっていない 状態を変更 (プロパティを書き換えただけ) 👆
push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait toggleOrientations() サ ポ ートの変更を明示的に通知 ココはまだ読み込まれていない ので変わっていない
setNeedsUpdateOfSupportedInterfaceOrientations() 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .portrait toggleOrientations() サ ポ ートの変更を明示的に通知 setNeedsUpdateOfSupportedInterfaceOrientations() 読み込まれた
リロード 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace toggleOrientations() サ ポ ートの変更を明示的に通知 setNeedsUpdateOfSupportedInterfaceOrientations() リロード
参照 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace toggleOrientations() setNeedsUpdateOfSupportedInterfaceOrientations() requestGeometryUpdate() 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace toggleOrientations() setNeedsUpdateOfSupportedInterfaceOrientations() requestGeometryUpdate() 変更してください🙇 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace toggleOrientations() setNeedsUpdateOfSupportedInterfaceOrientations() requestGeometryUpdate() 変更後の向きが サ ポ
ートされているか確認 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace toggleOrientations() setNeedsUpdateOfSupportedInterfaceOrientations() requestGeometryUpdate() 変更後の向きが サ ポ
ートされているか確認 OK! 👆 push!
currentOrientation: .landspace supportedInterfaceOrientations: .landspace 🎉
iOS16以降のAPIで 安全に回転しよう🫨
ご清聴ありがとうご ざ いました! フィードバックお待ちしています
• setNeedsUpdateOfSupportedInterfaceOrientations() | Apple Developer Documentation • requestGeometryUpdate(_:errorHandler:) | Apple
Developer Documentation • Swiftのorientation操作 (zenn.dev) • iOS15以前の話です • iOS16をサ ポ ートしよう! - bravesoft blog • ほとんどこれ 参考記事など