LoginSignup
1
1

【SwiftUI】HomeIndicatorを非アクティブにしてミス操作でホーム画面に行かないようにする(iOS16)

Posted at

はじめに

以前、HomeIndicatorを非表示にするって記事を書きました。

今回は非表示にする訳ではなく、非アクティブ?(表現が適切かどうかは不明)にして、1度のスワイプでホーム画面に行かないようにします。

サンプルアプリ

allbottomでしかわかりませんが、HomeIndicatorがグレーになっていることがわかります。
Simulator Screen Recording - iPhone 15 - 2023-12-30 at 22.19.17.gif

実装

import SwiftUI

struct ContentView: View {
    @State private var edgeSet: Edge.Set = .all
    
    var body: some View {
        VStack {
            Button {
                edgeSet = .all
            } label: {
                Text("all")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .all)
            
            Button {
                edgeSet = .vertical
            } label: {
                Text("vertical")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .vertical)
            
            Button {
                edgeSet = .horizontal
            } label: {
                Text("horizontal")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .horizontal)

            Button {
                edgeSet = .top
            } label: {
                Text("top")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .top)
            
            Button {
                edgeSet = .bottom
            } label: {
                Text("bottom")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .bottom)
            
            Button {
                edgeSet = .leading
            } label: {
                Text("leading")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .leading)
            
            Button {
                edgeSet = .trailing
            } label: {
                Text("trailing")
            }
            .buttonStyle(.borderedProminent)
            .disabled(edgeSet == .trailing)
        }
        .defersSystemGestures(on: edgeSet)
    }
}

おわり

ゲームなどでよく見ますね。
ゲーム最中にホーム画面に行ったら困りますもんね。
スワイプをすることの多いアプリはこの実装をした方が良さそうですね

公式ドキュメント

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1