1
2

【SwiftUI】1番下までスクロールしたら同意ボタンが押せるようになる機能を実装したい

Last updated at Posted at 2024-02-24

はじめに

SNS系のアプリで利用規約に同意させる画面が出ると思います。
利用規約を最後までスクロールしないと同意が押せないようなUIをよくみます。
それを再現してみます。

サンプルアプリ

Simulator Screen Recording - iPhone 15 Pro - 2024-02-24 at 22.20.58.gif

実装

import SwiftUI

struct ContentView: View {
    @State private var scrolledToBottom = false
    @State private var isAgreement = false
    
    var body: some View {
        VStack {
            ScrollView {
                LazyVStack {
                    Text("利用規約")
                        .font(.title)
                        .fontWeight(.bold)
                        .frame(maxWidth: .infinity, alignment: .leading)
                    ForEach(0..<100) { _ in
                        Text("利用規約の本文です")
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    Color.clear
                        .hidden()
                        .onAppear {
                            scrolledToBottom = true
                        }
                }
            }
            .border(.black)
            .clipped()
            
            Toggle(isOn: $isAgreement) {
                Text("同意しますか?")
            }
            .disabled(!scrolledToBottom)
            .foregroundStyle(scrolledToBottom ? .primary : .secondary)
        }
        .padding(.horizontal, 20)
    }
}

おわり

あるある機能なので記録しておきます

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