Skip to content

Instantly share code, notes, and snippets.

class RenderView: UIView {
private var renderLayer: CALayer?
func update(renderLayer: CALayer) {
self.renderLayer = renderLayer
}
override func draw(_ rect: CGRect) {
guard let renderLayer = renderLayer,
import SwiftUI
extension View {
func cascadingAction<ActionInput>(path: CascadingActionNodeModifier<ActionInput>.Path, handler: @escaping (ActionInput) -> Void) -> some View {
self.modifier(CascadingActionNodeModifier(path: path, handler: handler))
}
func cascadingAction(path: CascadingActionNodeModifier<Void>.Path, handler: @escaping () -> Void) -> some View {
self.modifier(CascadingActionNodeModifier(path: path, handler: handler))
}
@gdavis
gdavis / ThreadSafe.swift
Last active October 21, 2024 13:49
ThreadSafe is a property wrapper that can be used to control atomic access to the underlying property while allowing concurrent access to reading the value.
//
// ThreadSafe.swift
// GDICore
//
// Created by Grant Davis on 1/2/21.
// Updated to support `_modify` accessor on 12/5/21.
//
// Copyright © 2021 Grant Davis Interactive, LLC. All rights reserved.
//
import Foundation
@globulus
globulus / EnhancedVideoPlayer.swift
Created September 12, 2021 20:02
Enhanced SwiftUI Video Player
// Full recipe at https://swiftuirecipes.com/blog/play-video-in-swiftui
import SwiftUI
import AVKit
import Foundation
struct EnhancedVideoPlayer<VideoOverlay: View>: View {
@StateObject private var viewModel: ViewModel
@ViewBuilder var videoOverlay: () -> VideoOverlay
@IanKeen
IanKeen / Banner.swift
Last active March 1, 2024 21:49
Persistent banner in SwiftUI : Supports top/bottom edge, swipe to dismiss + auto dismissal after time
public enum BannerEdge {
case top, bottom
}
public enum BannerAutoDismiss {
case after(TimeInterval)
case never
}
extension View {
public func banner<C: View>(
import Cocoa
// MARK: - Protocols definition and syntax (2 slide)
// Declaration:
protocol SomeProtocol {
// protocol definition goes here
}
class Superclass {}
protocol ProtocolOne {}
@joshbetz
joshbetz / Webview.swift
Created February 12, 2020 15:36
A simple SwiftUI Webview
import SwiftUI
import WebKit
struct ContentView: View {
var body: some View {
Webview(url: URL(string: "https://google.com")!)
}
}
struct Webview: UIViewRepresentable {
@HiddenJester
HiddenJester / UnitTestingSceneDelegateGist.md
Last active January 17, 2025 19:31
Mocking SceneDelegate for Unit Tests on iOS 13

Replacing the SceneDelegate When Running Unit Tests

Overview

I've been working through the exercises in the excellent iOS Unit Testing by Example book by Jon Reid, which I highly recommend. However, the book is in beta at the moment and there are some curveballs thrown by iOS 13 that aren't handled in the text yet. Specifically, when I hit the section about using a testing AppDelegate class I thought "This is very good. But what about the SceneDelegate?"

In Chapter 4 the recommendation is to remove the @UIApplicationMain decoration and make a manual top-level call to UIApplicationMain. To wit:

import UIKit
@danylokos
danylokos / aria2c.md
Created October 29, 2019 08:57
aria2c multithread download
aria2c \
-x 16 \
-s 16 \
--load-cookies=cookies.txt \
https://download.developer.apple.com/Developer_Tools/Xcode_10.3/Xcode_10.3.xip
@nbasham
nbasham / Data+Extensions.swift
Last active January 21, 2024 14:57
Get a random date between two values. Swift 4.2+ uses Random(in:).
import Foundation
extension Date {
static func randomBetween(start: String, end: String, format: String = "yyyy-MM-dd") -> String {
let date1 = Date.parse(start, format: format)
let date2 = Date.parse(end, format: format)
return Date.randomBetween(start: date1, end: date2).dateString(format)
}