Skip to content

Instantly share code, notes, and snippets.

# Long-press FIRST before scrubbing
Similar to Safari and long-pressing on a photo or GIF, this already brings up the Haptic Touch menu for share options.
# Use edges of screen for next/prev, center for scrubbing video
The ergonomics of this aren't great on large phones, makes something as common as swiping between posts quite an effort. Plus I find the "dual layer" thing kinda confusing, for instance the Apple News app does this.
# Swipe up/down instead (AKA TikTok style)
import SwiftUI
struct ContentView: View {
var body: some View {
List {
ForEach(0 ..< 5, id: \.self) { index in
Button {
print("Tapped index \(index)")
} label: {
HStack {
@christianselig
christianselig / swiftui-magic-number-helper.md
Last active July 27, 2022 21:24
Make magic numbers more accessible in SwiftUI

More Accessible Magic Numbers

A lot of StackOverflow answers for dealing with things in SwiftUI sometimes being positioned not how you want (for example) understandably solve it with a magic number to put something where they want.

The downside of this is that it likely only works at the default Dynamic Type size, so anyone with larger or smaller text settings (for accessibility reasons, for instance) will likely have a layout that doesn't look as optimized (for instance, if you assume 24.0 is a good number based on the default .large size, if they have their phone set to accessibilityExtraExtraExtraLarge, 24.0 will likely be way too small).

So here's a quick helper extension that simply scales a number based on the Dynamic Type size, so that 24.0 will increase and decrease with the user's Dynamic Type setting.

// Extending BinaryFloatingPoint so that this ext
class ViewController: UIViewController {
let toolbar = MyToolbar()
override func viewDidLoad() {
super.viewDidLoad()
toolbar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toolbar)
NSLayoutConstraint.activate([
import SwiftUI
struct ContentView: View {
@State var pictureExpanded = false
var body: some View {
VStack(alignment: .leading, spacing: 20.0) {
Text("Once upon a time there was a turtle named George who made friends with a giraffe at the local water park and then they went on lots of adventures together.")
Button {
@State var firstParagraphTextColor = Color.orange
let colorTimer = Timer.publish(every: 1.0, on: .main, in: .common).autoconnect()
var body: some View {
Text("PLEASE READ 🥺 In-app purchases and subscriptions are free in TestFlight versions of apps, so you will not actually be charged (see confirmation sheet).")
.font(Font(FontManager.shared.largeFont(isBold: true).rounded() as CTFont))
.foregroundColor(.white)
.colorMultiply(firstParagraphTextColor)
.animation(.linear(duration: 1.0), value: firstParagraphTextColor)
.onReceive(colorTimer) { _ in
struct ContentView: View {
@FocusState var isFocused: Bool
@State var text = "Hello please type inside me!"
var body: some View {
TextEditor(text: $text)
.padding(.top, 100.0)
.focused($isFocused)
.onAppear {
// Does not work
func fetchIceCream(attempt: Attempt = .one) async throws -> String {
// Hit the network
let (data, response): (Data, URLResponse)
// Allow reattempting this one
// So prevent the error from propagating the first time
do {
(data, response) = try await URLSession.shared.data(from: request)
} catch {
LogBuddy.error(error)
// Hit the network
let (data, response): (Data, URLResponse)
// Prevent error from propagating to allow for reattempting
do {
(data, response) = try await URLSession.shared.data(from: request)
} catch {
LogBuddy.error("Error connecting to server: \(error.localizedDescription)")
// Allow reattempts with exponential backoff
@christianselig
christianselig / backflip.swift
Created April 29, 2022 23:43
Async await code waiting on user interaction to continue
class CheckIfUserCanDoABackflipViewController: UIViewController {
var dismissedResultHandler: ((Bool) -> Void)?
var didABackflip = false
func userDidABackflip {
didABackflip = true
}
override func viewDidDisappear(_ animated: Bool) {