Skip to content

Instantly share code, notes, and snippets.

Avatar

Magnus Kahr Jensen magnuskahr

View GitHub Profile
@magnuskahr
magnuskahr / AnyTransition+flip.swift
Last active August 9, 2022 20:28
SwiftUI Flip transition
View AnyTransition+flip.swift
extension AnyTransition {
static var flip: AnyTransition {
.modifier(
active: FlipEffect(flip: .active, animatableData: 1),
identity: FlipEffect(flip: .identity, animatableData: 0)
)
}
}
private struct FlipEffect: GeometryEffect {
View FullscreenOverlay.swift
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 {
View se-0401.md

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
View ValueToggle.swift
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)
View CascadingAction.swift
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.
View UnwrappingButton.swift
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 View+pulsating.swift
//
// 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
View IntegerField.swift
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
View AnimateableView.swift
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))
}
@magnuskahr
magnuskahr / EnumPicker.swift
Last active February 11, 2022 03:08
A simple picker to pick a enum.
View EnumPicker.swift
import SwiftUI
struct EnumPicker<T: Hashable & CaseIterable, V: View>: View {
@Binding var selected: T
var title: String? = nil
let mapping: (T) -> V
var body: some View {