Skip to content

Instantly share code, notes, and snippets.

@trochoid
trochoid / SimpleFlowLayout.swift
Created October 19, 2025 01:16
Simple flow layout object and example
import SwiftUI
struct ContentView: View {
@State var tags = ["apple", "egg", "banana", "pop", "orange", "acid", "whiskey", "soda"]
@State var constrainedWidth = 300.0
var body: some View {
VStack {
addAndRemoveButtons
@trochoid
trochoid / Crustyshadertoy.swift
Created June 21, 2024 14:12
Barebones shadertoy. This is a stripped down version of someone else’s code
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
@trochoid
trochoid / ClassesRedraw.swift
Created September 9, 2023 12:55
Classes redraw only their view
import SwiftUI
class Foo: ObservableObject {
@Published var bars = [Bar(), Bar(), Bar()]
}
class Bar: ObservableObject, Identifiable {
@Published var value = 0
}
@trochoid
trochoid / StructsRedraw.swift
Created September 9, 2023 12:53
Array of structs redraw all views
import SwiftUI
class Foo: ObservableObject {
@Published var bars = [Bar(), Bar(), Bar()]
}
struct Bar: Identifiable {
var id = UUID()
var value = 0
}
import SwiftUI
struct Crossword {
var cells: [[Cell]] = [
[Cell(0), Cell(1), Cell(2)],
[Cell(11), Cell(22)],
[Cell(777)]
]
}
@trochoid
trochoid / BookmarkDemo.swift
Last active February 21, 2023 10:46
Use a bookmark Data to save and restore an outside of sandbox URL
import SwiftUI
let kBookmarkUDKey = "somekey"
struct ContentView: View {
class EZIO {
//URL <-> bookmark Data (assume security access)
public static func makeBookmark(url: URL) -> Data? {
do {
return try url.bookmarkData(
options: .minimalBookmark,
includingResourceValuesForKeys: nil,
@trochoid
trochoid / EmojiFall.swift
Last active December 1, 2022 10:37
Basic falling Emoji
import SwiftUI
class Emoji {
var character = "🧚🏼‍♀️"
var position = CGPoint(x: 100, y: 0)
}
struct ContentView: View {
let emoji = Emoji()
var body: some View {
import SwiftUI
struct ContentView: View {
let arr = [1, 2, 4, 7]
@State var currentIndex = 0
var body: some View {
VStack {
IntArraySlider(arr: arr, currentIndex: $currentIndex)
Text("index: \(currentIndex), value: \(arr[currentIndex])")
}
import SwiftUI
import AVFoundation
struct ContentView: View {
let mar = MyAudioRecorder()
var body: some View {
VStack(spacing: 16) {