最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - Get optimized ScrollView value IOS - Stack Overflow

programmeradmin1浏览0评论

I need to get the ScrollView value, and I already have one solution, but it's not very optimized. And I have a strong feeling that I'm missing a simpler solution. My final goal is get the ScrollView value to animate the closure of the block (as in the weather app from Apple). Please Help

There is no optimized solution here

ScrollView(.vertical, content: {
    Vstack(content:{
        Rectaingle()
            .frame(width:100, height: 100)
    })
    .background(GeometryReader {
                            Color.clear.preference(key: ViewOffsetKey.self,
                            value: -$0.frame(in: .named("scroll")).origin.y)
    })
    .onPreferenceChange(ViewOffsetKey.self) {mainScrollValue = $0}
                 
})
.coordinateSpace(name: "scroll")
.scrollIndicators(.never)    

Working on IOS 16.0

I need to get the ScrollView value, and I already have one solution, but it's not very optimized. And I have a strong feeling that I'm missing a simpler solution. My final goal is get the ScrollView value to animate the closure of the block (as in the weather app from Apple). Please Help

There is no optimized solution here

ScrollView(.vertical, content: {
    Vstack(content:{
        Rectaingle()
            .frame(width:100, height: 100)
    })
    .background(GeometryReader {
                            Color.clear.preference(key: ViewOffsetKey.self,
                            value: -$0.frame(in: .named("scroll")).origin.y)
    })
    .onPreferenceChange(ViewOffsetKey.self) {mainScrollValue = $0}
                 
})
.coordinateSpace(name: "scroll")
.scrollIndicators(.never)    

Working on IOS 16.0

Share Improve this question edited Jan 20 at 15:29 Jan Ivashenko asked Jan 20 at 0:52 Jan IvashenkoJan Ivashenko 35 bronze badges 7
  • Not clear what optimized ScrollView value you are after, but you could try using ScrollViewReader. – workingdog support Ukraine Commented Jan 20 at 3:49
  • Are you trying to find the scroll offset in points? Or do you want to know, which view is currently the top-most visible view? For the latter, consider using .scrollPosition. If your target is iOS 18 then there are some new features that might be useful, see onScrollGeometryChange(for:of:action:) – Benzy Neez Commented Jan 20 at 9:52
  • @BenzyNeez As for which View is top and finding scroll offset in points, I probably need both options. And isn't .scrollPosition for IOS18.0+? Anyway, I'm limited to iOS 16.0. But thanks for trying to help :) – Jan Ivashenko Commented Jan 20 at 15:39
  • @JanIvashenko .scrollPosition is available in iOS 17, see scrollPosition(id:anchor:). If you're targeting iOS 16 then you can use .onGeometryChange instead of a PreferenceKey, at least. – Benzy Neez Commented Jan 20 at 15:52
  • @BenzyNeez if it doesn't bother you, what is the difference between .onGeometryChange and PreferenceKey? – Jan Ivashenko Commented Jan 20 at 15:55
 |  Show 2 more comments

1 Answer 1

Reset to default 0

If you are looking for a solution that involves less code then I would suggest using .onGeometryChange. This is compatible with iOS 16:

struct ContentView: View {
    @State private var scrollOffset = CGFloat.zero

    var body: some View {
        ScrollView {
            VStack {
                Rectangle()
                    .frame(width: 100, height: 100)
            }
            .onGeometryChange(for: CGFloat.self) { proxy in
                proxy.frame(in: .named("scroll")).minY
            } action: { minY in
                scrollOffset = minY
            }
        }
        .coordinateSpace(name: "scroll")
        .scrollIndicators(.never)
    }
}
发布评论

评论列表(0)

  1. 暂无评论