Skip to content

Instantly share code, notes, and snippets.

@IanKeen
IanKeen / Example_Complex.swift
Last active January 23, 2024 07:53
PropertyWrapper: @transaction binding for SwiftUI to make changes to data supporting commit/rollback
struct User: Equatable {
var firstName: String
var lastName: String
}
@main
struct MyApp: App {
@State var value = User(firstName: "", lastName: "")
@State var showEdit = false
@JohnSundell
JohnSundell / StarPlane.swift
Created July 8, 2020 22:41
A simple game written in SwiftUI. Note that this is just a fun little hack, the code is not meant to be taken seriously, and only works on iPhones in portrait mode.
// A fun little game written in SwiftUI
// Copyright (c) John Sundell 2020, MIT license.
// This is a hacky implementation written just for fun.
// It's only verified to work on iPhones in portrait mode.
import SwiftUI
final class GameController: ObservableObject {
@Published var plane = GameObject.plane()
@Published private(set) var clouds = [GameObject]()
@mergesort
mergesort / AnimationDemo.swift
Last active December 18, 2023 02:34
A SwiftUI prototype animation for adding/removing players from a game
import SwiftUI
let colors = [
Color.pink,
Color.blue,
Color.green,
Color.orange,
Color.purple,
Color.black,
]
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[Serializable]
public class BackpropagationNeuralNetwork
{
private static readonly System.Random Random = new System.Random();
@uchcode
uchcode / ImageViewer.swift
Last active January 11, 2023 10:49
Developing a Document-Based App in SwiftUI
import SwiftUI
struct ContentView: View {
@ObservedObject var browser = { () -> DocumentBrowserObject in
let dbo = DocumentBrowserObject (
forOpeningFilesWithContentTypes: [
"public.png",
"public.jpeg"
]
)
@epilys
epilys / dump_core.rs
Created March 12, 2020 06:26
easy core dump on panic in rust for debugging
pub fn register_panic_handler() {
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
default_panic(panic_info);
// Don't forget to enable core dumps on your shell with eg `ulimit -c unlimited`
let pid = std::process::id();
eprintln!("dumping core for pid {}", std::process::id());
@mayoff
mayoff / ConversationView.swift
Created February 14, 2020 20:05
playground demonstrating SwiftUI PreferenceKey, EnvironmentKey, and GeometryReader
import SwiftUI
import PlaygroundSupport
struct EqualWidthKey: PreferenceKey {
static var defaultValue: CGFloat? { nil }
static func reduce(value: inout CGFloat?, nextValue: () -> CGFloat?) {
switch (value, nextValue()) {
case (_, nil): break
case (nil, let next): value = next
@tomhicks
tomhicks / plink-plonk.js
Last active March 18, 2024 02:23
Listen to your web pages
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let headerView = tableView.tableHeaderView else {
return
}
// The table view header is created with the frame size set in
// the Storyboard. Calculate the new size and reset the header
// view to trigger the layout.