Skip to content

Instantly share code, notes, and snippets.

@rsalesas
rsalesas / CaptureScrollingOffset.swift
Created May 22, 2023 18:24
Provides extensions to get called when a ScrollView is scrolled. First use .trackScrolling on the ScrollView, then put a ScrollViewReader inside. On the ScrollViewReader use the .onScrolled extension to receive the events (one argument, a UnitPoint).
import SwiftUI
public extension ScrollView {
// NB: CaptureScrollingOffset.EnvironmentKey is a class, so the value can be "shared" between Views,
// allowing both GeometryReader and Environment to participate in the same view.
func trackScrolling() -> some View {
let coordinateSpace = UUID()
let environmentKey = CaptureScrollingOffset.EnvironmentKey(coordinateSpace: coordinateSpace)
/*
Creates an array of C-strings that can be passed to C functions and automatically deallocated
Copyright 2020 Robert Salesas
*/
public struct CStringArray {
public let pointer: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
public let count: Int
private var data: Data