Skip to content

Instantly share code, notes, and snippets.

@LePips
LePips / ScrollViewOffsetModifier.swift
Last active October 10, 2024 00:32
SwiftUI ViewModifier to keep track of the offset in a ScrollView. Can be expanded for all UIScrollViewDelegate methods and (x, y) offset.
// https://github.com/siteline/SwiftUI-Introspect
import Introspect
struct ScrollViewOffsetModifier: ViewModifier {
@Binding
var scrollViewOffset: CGFloat
private let scrollViewDelegate: ScrollViewDelegate?
@LePips
LePips / NavBarOffsetModifier.swift
Last active May 14, 2024 03:19
SwiftUI ViewModifier to create an offset fading-in/out navigation bar for a view based on a scroll view offset.
// Assumes usage of:
// - https://gist.github.com/LePips/3640ad0cd9b6e2ceb407e9d0e9e32b5c
// Embedding view for content
struct NavBarOffsetView<Content: View>: UIViewControllerRepresentable {
@Binding
private var scrollViewOffset: CGFloat
private let start: CGFloat
@LePips
LePips / BackgroundParallaxHeaderModifier.swift
Last active March 17, 2023 21:37
SwiftUI ViewModifier to create a parallax and stretchy background header based on ScrollView offset
// Assumes usage of:
// - https://gist.github.com/LePips/3640ad0cd9b6e2ceb407e9d0e9e32b5c
// - https://gist.github.com/LePips/5c6b4546e9bd8f91c029f705347be974
//
// Last gist not required, but nice to have for headers.
struct BackgroundParallaxHeaderModifier<Header: View>: ViewModifier {
@Binding
var scrollViewOffset: CGFloat
struct NavBarDrawerButtonsModifier<Buttons: View>: ViewModifier {
let buttons: () -> Buttons
init(@ViewBuilder buttons: @escaping () -> Buttons) {
self.buttons = buttons
}
func body(content: Content) -> some View {
NavBarDrawerButtonsView {
@LePips
LePips / gist:293d92f5fac07de256878e37e96e7bec
Created November 18, 2023 01:48
Swift ArrayBuilder resultBuilder
// Use to create an Array of elements in a result builder
//
// Best used with a typealias:
// typealias StringBuilder = ArrayBuilder<String>
@resultBuilder
public struct ArrayBuilder<Element> {
public static func buildBlock(_ components: [Element]...) -> [Element] {
components.flatMap { $0 }