mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-11 15:53:30 +01:00
Reorganize AltStore project into UIKit and SwiftUI folders
This commit is contained in:
47
AltStore/SwiftUI/View Components/ObservableScrollView.swift
Normal file
47
AltStore/SwiftUI/View Components/ObservableScrollView.swift
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// ObservableScrollView.swift
|
||||
// SideStore
|
||||
//
|
||||
// Created by Fabian Thies on 20.11.22.
|
||||
// Copyright © 2022 Fabian Thies. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ObservableScrollView<Content: View>: View {
|
||||
@Namespace var scrollViewNamespace
|
||||
|
||||
@Binding var scrollOffset: CGFloat
|
||||
|
||||
let content: (ScrollViewProxy) -> Content
|
||||
|
||||
init(scrollOffset: Binding<CGFloat>, @ViewBuilder content: @escaping (ScrollViewProxy) -> Content) {
|
||||
self._scrollOffset = scrollOffset
|
||||
self.content = content
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
ScrollViewReader { proxy in
|
||||
content(proxy)
|
||||
.background(GeometryReader { geoReader in
|
||||
let offset = -geoReader.frame(in: .named(scrollViewNamespace)).minY
|
||||
Color.clear
|
||||
.preference(key: ScrollViewOffsetPreferenceKey.self, value: offset)
|
||||
})
|
||||
}
|
||||
}
|
||||
.coordinateSpace(name: scrollViewNamespace)
|
||||
.onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { value in
|
||||
scrollOffset = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ScrollViewOffsetPreferenceKey: PreferenceKey {
|
||||
static var defaultValue = CGFloat.zero
|
||||
|
||||
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
|
||||
value += nextValue()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user