3
2

More than 1 year has passed since last update.

【SwiftUI】読み込み中は要素をグレーの長方形で表示する

Posted at

はじめに

読み込み中であることをユーザーに伝える方法としてredactedがあります。
redactedを使ったので記録しておきます。

サンプルアプリ

Simulator Screen Recording - iPhone 14 Pro - 2023-08-08 at 22.12.21.gif

実装

import SwiftUI

struct ContentView: View {
    @State private var isLoading = false
    var body: some View {
        VStack(spacing: 50) {
            Text("テキスト")
                .redacted(reason: isLoading ? .placeholder : [])
            
            Button {
                Task { @MainActor in
                    isLoading = true
                    // 長い処理の代わり
                    try? await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC)
                    isLoading = false
                }
            } label: {
                Text("再読み込み")
            }
        }
    }
}

おわり

色々なアプリで使われてる方法なので自分のアプリにも積極的に使っていきたいです。

3
2
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
3
2