Skip to content

Instantly share code, notes, and snippets.

View magnuskahr's full-sized avatar

Magnus Kahr Jensen magnuskahr

View GitHub Profile
@magnuskahr
magnuskahr / viewroot_variadics_environment.swift
Last active February 16, 2024 13:22
SwiftUI ViewRoot unable to change environment
// Working with SwiftUI variadics, i need to set the environment of a child - which seems like is impossible.
// The spy always outputs "fail".
struct Transform<Content: View, Result: View>: View {
@ViewBuilder let content: Content
let transformation: (_VariadicView.Children.Element) -> Result
var body: some View {
_VariadicView.Tree(
@magnuskahr
magnuskahr / AnyTransition+flip.swift
Last active August 9, 2022 20:28
SwiftUI Flip transition
extension AnyTransition {
static var flip: AnyTransition {
.modifier(
active: FlipEffect(flip: .active, animatableData: 1),
identity: FlipEffect(flip: .identity, animatableData: 0)
)
}
}
private struct FlipEffect: GeometryEffect {
extension View {
func fullscreenOverlay<Overlay: View>(isPresented show: Binding<Bool>, @ViewBuilder overlay: () -> Overlay) -> some View {
background {
FullscreenOverlayPresenter(show: show, overlay: overlay())
}
}
}
struct FullscreenOverlayPresenter<Overlay: View>: UIViewControllerRepresentable {

SE-0401: unimport for cleaning up files

Introduction

Initialization and deinitalization are important parts of how we create structs and classes in Swift. Being a bit more abstract, we have files that can import other modules to expand local capabilities using the import keyword, however, the reversed ain’t true, requiring developers to hassle with imported functionality at lines were it ain’t used.

@magnuskahr
magnuskahr / ValueToggle.swift
Last active January 14, 2022 18:15
A SwiftUI toggle to switch on/off a value for an optional state
struct ValueToggle<Value, Label: View>: View {
@Binding var value: Value?
let onValue: Value
let label: () -> Label
@State private var isOn = false
var body: some View {
Toggle(isOn: $isOn, label: label)
import SwiftUI
extension View {
func cascadingAction<ActionInput>(path: CascadingActionNodeModifier<ActionInput>.Path, handler: @escaping (ActionInput) -> Void) -> some View {
self.modifier(CascadingActionNodeModifier(path: path, handler: handler))
}
func cascadingAction(path: CascadingActionNodeModifier<Void>.Path, handler: @escaping () -> Void) -> some View {
self.modifier(CascadingActionNodeModifier(path: path, handler: handler))
}
@magnuskahr
magnuskahr / UnwrappingButton.swift
Last active December 11, 2021 18:55
Need to show a button that is always visible and which action relies on a value, but disabled if the value is missing? Use UnwrappingButton for an easy solution.
import SwiftUI
struct UnwrappingButton<Label: View, T>: View {
enum Unwrapping<T> {
case element(value: T?, action: (T) -> Void)
case binding(value: Binding<T?>, action: (Binding<T>) -> Void)
var value: T? {
switch self {
@magnuskahr
magnuskahr / View+pulsating.swift
Created October 3, 2021 08:25
SwiftUI View pulsating effect
//
// View+pulsating.swift
//
// Created by Magnus Jensen on 02/10/2021.
//
import SwiftUI
extension View {
func pulsating() -> some View {
@magnuskahr
magnuskahr / IntegerField.swift
Created August 2, 2021 18:54
An integer field in swiftui
struct IntegerField: View {
@Binding var value: Int
@State private var stringValue: String
init(value binding: Binding<Int>) {
self._value = binding
self._stringValue = State(wrappedValue: binding.wrappedValue.formatted())
}
@magnuskahr
magnuskahr / AnimateableView.swift
Created May 13, 2021 21:21
Rolling animatable view
struct AnimateableView<ContentView: View, Value: VectorArithmetic>: View {
let value: Value
@ViewBuilder let builder: (Value) -> ContentView
var body: some View {
builder(value)
.modifier(Animation<ContentView, Value>(value: value, builder: builder))
}