Skip to content

Instantly share code, notes, and snippets.

@chockenberry
chockenberry / ContentView.swift
Created April 18, 2024 21:21
Observable with backing store
//
// ContentView.swift
// ObservableTester
//
// Created by Craig Hockenberry on 4/18/24.
//
import SwiftUI
class BackingStore {
@chockenberry
chockenberry / ContentView.swift
Created April 17, 2024 17:43
NavigationPath Experiments
//
// ContentView.swift
// NavigationTest
//
// Created by Craig Hockenberry on 4/16/24.
//
import SwiftUI
struct OddView: View {
@rnapier
rnapier / MainActorRun.swift
Last active April 17, 2024 15:55
Regarding MainActor.run
// In regards to https://mastodon.social/@mattiem/112285978801305971
// MainActor class with synchronous methods
@MainActor final class M {
func methodA() {}
func methodB() {}
}
// Actor that relies on M.
actor A {
@chockenberry
chockenberry / ContentView.swift
Created April 12, 2024 17:58
Optional Bindable
//
// ContentView.swift
// BindableOptional
//
// Created by Craig Hockenberry on 4/12/24.
//
import SwiftUI
@Observable
@deebloo
deebloo / shadow-element.ts
Last active January 25, 2023 22:04
Small utility for apply html template and construct-able stylesheets to custom elements.
import { shadow, ShadowTemplate, html, css } from './shadow.js';
const template: ShadowTemplate = {
css: css`
:host {
display: contents;
}
`.
html: html`
<slot></slot>
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for (i, view) in scrollView.subviews.enumerated() {
var ty = 0.0
if scrollView.contentOffset.y < 0 {
// We're scrolling past the top of the scroll view.
// Translate each item in the scroll view by some amount based on its index and scroll offset.
ty = CGFloat(i) * abs(offsetY) / 8.0 * pow(1.12, CGFloat(i))
}
view.transform = CGAffineTransform(translationX: 0, y: ty)
}
@PimCoumans
PimCoumans / AnimationSequece.swift
Last active January 17, 2024 09:32
Simple way to chain and group multiple UIView animations
import UIKit
protocol StepAnimatable {
/// Start a sequence where you add each step in the `addSteps` closure. Use the provided `AnimationSequence` object
/// to add each step which should either be an actual animation or a delay.
/// The `completion` closure is executed when the last animation has finished.
/// - Parameters:
/// - addSteps: Closure used to add steps to the provided `AnimationSequence` object
/// - completion: Executed when the last animation has finished.
@nicklockwood
nicklockwood / Withable.swift
Created January 28, 2019 12:06
Withable.swift
/// Withable is a simple protocol to make constructing
/// and modifying objects with multiple properties
/// more pleasant (functional, chainable, point-free)
public protocol Withable {
init()
}
public extension Withable {
/// Construct a new instance, setting an arbitrary subset of properties
init(with config: (inout Self) -> Void) {
POST https://firestore.googleapis.com/v1beta1/projects/firestore-fun/databases/(default)/documents:commit?key={YOUR_API_KEY}
{
"writes": [
{
"update": {
"name": "projects/firestore-fun/databases/(default)/documents/foo/bar",
"fields": {
"some-field": {
"stringValue": "some-value"
@rnystrom
rnystrom / ViewController.swift
Created May 29, 2018 01:16
Interactive NSAttributedString link highlighting with TextKit
import UIKit
public extension CGSize {
func snapped(scale: CGFloat) -> CGSize {
var size = self
size.width = ceil(size.width * scale) / scale
size.height = ceil(size.height * scale) / scale
return size
}