LoginSignup
0
1

More than 1 year has passed since last update.

【SwiftUI】ScrollViewが1番下までスクロールした事を検知する

Posted at

はじめに

現在SwiftUIではScrollViewやListを1番下までスクロールした事を検知することができません。
実現しようと調べて見ましたがあまりスマートなやり方は見つかりませんでした。
(唯一見つけたのはGeometryReaderで挟んでスクロールした幅を取得するというもの)

色々試した中で1番スマートにかけたものを紹介します。
(検知はできてるけど使い物にならない)

やり方

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical, showsIndicators: true) {
            ForEach(0..<100) { index in
                Text("テキスト: \(index)")
            }
        }
    }
}
extension UIScrollView: UIScrollViewDelegate {
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let top = scrollView.contentOffset.y
        let bottom = top + scrollView.frame.size.height
        if top > 0 && scrollView.contentSize.height <= bottom {
            print("1番下です")
        }
    }
}

問題点

スクロールできるView全てに反映されるます。
ですので全く使い物になりません笑

おわり

これがちゃんと使えるようになる方法あったら教えてください。

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