Skip to content

Instantly share code, notes, and snippets.

@steipete
steipete / RandomColor.swift
Created April 6, 2021 17:20
Random Color for SwiftUI
extension Color {
/// Return a random color
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
@djds23
djds23 / SKAction+Keypath.swift
Created March 2, 2021 03:41
An SKAction that writes a value to a specified object.
extension SKAction {
static func write<Root: AnyObject, Value>(
value: Value,
to keyPath: ReferenceWritableKeyPath<Root, Value>,
on object: Root
) -> SKAction {
SKAction.customAction(withDuration: 0) { [weak object] _, _ in
object?[keyPath: keyPath] = value
}
@IanKeen
IanKeen / Example.swift
Created February 7, 2021 03:09
PropertyWrapper: Ignore codable properties
struct Foo: Codable {
var name: String
@Ignore var foo: Int?
}
let model = Foo(name: "Ian", foo: 42)
let data = try! JSONEncoder().encode(model)
print(String(data: data, encoding: .utf8)!) // {"name":"Ian"}
@marcpalmer
marcpalmer / CombineExportProgressSample.swift
Last active April 10, 2023 02:09
Pulling down a video asset from Photos and exporting it, with progress and cancellation with Combine
/// Get the payload of a video asset and export it to a local temp file.
/// The resulting publsher will emit values until completed or cancelled. The `Double` is the progress,
/// which is a combination of the download progress and the export progress, so it will range from 0 to 1 but the
/// last export part is probably a lot quicker than the download part.
///
/// Calling cancel() on the publisher will cancel both the image request and the export as appropriate
func exportAVAsset(forPHAsset phAsset: PHAsset) -> (AnyPublisher<(URL?, Double), MediaError>) {
let avAssetOptions = PHVideoRequestOptions()
avAssetOptions.isNetworkAccessAllowed = true
avAssetOptions.deliveryMode = .highQualityFormat
@truizlop
truizlop / CircleWave.swift
Created September 29, 2020 15:43
Circle wave animation
import SwiftUI
struct CircleWave: Shape {
let N = 360
let SPEED: Double = 1 / 1000.0
let SHIFT: Double = 2 * Double.pi / 3
let FREQUENCY: Double = 8
var factor: Double
var colorIndex: Double
@mobilinked
mobilinked / gist:9b6086b3760bcf1e5432932dad0813c0
Last active February 9, 2024 13:03
SwiftUI - prevent auto dismiss the sheet by drag down
//
// Created by https://quickplan.app on 2020/11/8.
//
import SwiftUI
/// Control if allow to dismiss the sheet by the user actions
/// - Drag down on the sheet on iPhone and iPad
/// - Tap outside the sheet on iPad
/// No impact to dismiss programatically (by calling "presentationMode.wrappedValue.dismiss()")
//
// ContentView.swift
//
//
// Created by Bernstein, Joel on 7/4/20.
//
import SwiftUI
struct ElementModel: Identifiable
@stammy
stammy / TipsView.swift
Created July 4, 2020 17:24
SwiftUI MicroAnimation example
//
// TipsView.swift
// Stonks
//
// Created by Paul Stamatiou on 7/3/20.
//
import SwiftUI
import PlaygroundSupport
import SwiftUI
import PlaygroundSupport
// constants
let cardWidth: CGFloat = 343
let cardHeight: CGFloat = 212
let spacing = 36
let animation = Animation.spring()
let cardColors = [
Color(UIColor.systemRed),
@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,
]