Skip to content

Instantly share code, notes, and snippets.

@IanKeen
IanKeen / View+Relative.swift
Last active January 16, 2023 13:03
SwiftUI relative frame
extension View {
func relative(width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) -> some View {
Color.clear
.frame(maxWidth: width.map { _ in .infinity }, maxHeight: height.map { _ in .infinity })
.overlay(GeometryReader { proxy in
ZStack {
self.frame(
width: width.map { proxy.size.width * $0 },
height: height.map { proxy.size.height * $0 }
)
import SwiftUI
import Combine
public struct ChangeObserver<V: Equatable>: ViewModifier {
public init(newValue: V, action: @escaping (V) -> Void) {
self.newValue = newValue
self.newAction = action
}
private typealias Action = (V) -> Void
@AliSoftware
AliSoftware / Bindings.swift
Last active April 2, 2024 23:47
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
@BjornRuud
BjornRuud / UILabel+Debug.swift
Created November 7, 2017 12:09
Debug mode for UILabel that can optionally show bounds, ascender, descender, x height, cap height, baseline and leading.
import UIKit
struct UILabelDebugOptions: OptionSet {
let rawValue: Int
static let bounds = UILabelDebugOptions(rawValue: 1 << 0)
static let ascender = UILabelDebugOptions(rawValue: 1 << 1)
static let descender = UILabelDebugOptions(rawValue: 1 << 2)
static let xHeight = UILabelDebugOptions(rawValue: 1 << 3)
static let capHeight = UILabelDebugOptions(rawValue: 1 << 4)
@fousa
fousa / FairPlayer.swift
Last active June 1, 2023 12:28
Integrate HLS with FairPlay.
class FairPlayer: AVPlayer {
private let queue = DispatchQueue(label: "com.icapps.fairplay.queue")
func play(asset: AVURLAsset) {
// Set the resource loader delegate to this class. The `resourceLoader`'s delegate will be
// triggered when FairPlay handling is required.
asset.resourceLoader.setDelegate(self, queue: queue)
// Load the asset in the player.
@rnapier
rnapier / json.swift
Last active January 31, 2024 12:49
Generic JSON Decodable
import Foundation
@dynamicMemberLookup
enum JSON: Codable, CustomStringConvertible {
var description: String {
switch self {
case .string(let string): return "\"\(string)\""
case .number(let double):
if let int = Int(exactly: double) {
return "\(int)"
@myell0w
myell0w / KeyboardLayoutGuide.swift
Created July 19, 2017 14:53
A UILayoutGuide that follows the Keyboard on iOS
import Foundation
import UIKit
/// Used to create a layout guide that pins to the top of the keyboard
final class KeyboardLayoutGuide {
private let notificationCenter: NotificationCenter
private let bottomConstraint: NSLayoutConstraint
@ollieatkinson
ollieatkinson / AnyDiffable.swift
Last active February 12, 2023 10:14
Implementation of Paul Heckel's Diff Algorithm in Swift 3
public protocol Diffable: Hashable {
var primaryKeyValue: String { get }
}
/// A type-erased diffable value.
/// The AnyDiffable type forwards diffing, equality comparisons and hashing operations to an underlying diffing value,
/// hiding its specific underlying type.
///
@praeclarum
praeclarum / ArrayDiff.swift
Last active January 8, 2021 06:10
A generic diffing operation that can calculate the minimal steps needed to convert one array to another. It can be used to generate standard diffs, or it can be used more creatively to calculate minimal UI updates.
//
// ArrayDiff.swift
//
// Created by Frank A. Krueger on 6/30/15.
// Copyright © 2015 Krueger Systems, Inc. All rights reserved.
// License: MIT http://opensource.org/licenses/MIT
//
import Foundation
@clementgenzmer
clementgenzmer / FBAnimationPerformanceTracker.h
Last active September 18, 2023 23:02
FBAnimationPerformanceTracker
/*
* This is an example provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN