Skip to content

Instantly share code, notes, and snippets.

View p4checo's full-sized avatar

André Pacheco Neves p4checo

View GitHub Profile
@ole
ole / RelativeSizeLayout.swift
Last active March 24, 2024 22:24
A SwiftUI layout and modifier for working with relative sizes ("50 % of your container"). https://oleb.net/2023/swiftui-relative-size/
import SwiftUI
extension View {
/// Proposes a percentage of its received proposed size to `self`.
///
/// This modifier multiplies the proposed size it receives from its parent
/// with the given factors for width and height.
///
/// If the parent proposes `nil` or `.infinity` to us in any dimension,
/// we’ll forward these values to our child view unchanged.
@beesandbombs
beesandbombs / spinCubes.pde
Created February 3, 2023 15:10
spinning cubes
float[][] result;
float t, c;
float ease(float p) {
p = c01(p);
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
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)
}
@maximkrouk
maximkrouk / !Button.swift
Last active January 11, 2024 10:55
Easily customisable declarative UIKit button
// - Depends on https://github.com/swift-declarative-configuration
// - Depends on https://github.com/swift-foundation-extensions
// - Depends on https://gist.github.com/maximkrouk/942125396a857e49203ddb933d557c31
import UIKit
import FoundationExtensions
import DeclarativeConfiguration
fileprivate extension UIView {
func pinToSuperview() {
@keith
keith / README.md
Last active January 9, 2024 07:52
A test of `@_dynamicReplacement` in Swift

Usage

  1. swiftc main.swift -emit-module-path main.swiftmodule -emit-executable -enable-private-imports -Xfrontend -enable-implicit-dynamic
  2. ./main -> prints From original bar()
  3. swiftc -emit-library inject.swift -o inject.dylib -I . -Xlinker -undefined -Xlinker suppress -Xlinker -flat_namespace -Xfrontend -disable-access-control
  4. DYLD_INSERT_LIBRARIES=inject.dylib ./main -> prints From replacement bar()

Notes

  • Passing -Xfrontend -enable-implicit-dynamic removes you from having to add dynamic to everything you want to be replacable
// Original article here: https://fivestars.blog/swiftui/swiftui-share-layout-information.html
import SwiftUI
extension View {
func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: SizePreferenceKey.self, value: geometryProxy.size)
@IanKeen
IanKeen / Publisher+Result.swift
Created July 14, 2020 17:18
Extension to convert a failable Publisher into a non-failable Result based publisher (as well as extensions to break out values/errors) - Same idea as RxSwifts Materialize
extension Publisher {
func asResult() -> AnyPublisher<Result<Output, Failure>, Never> {
return map(Result.success)
.catch { Just(Result.failure($0)) }
.eraseToAnyPublisher()
}
}
extension Publisher {
func values<S, F: Error>() -> AnyPublisher<S, Never> where Output == Result<S, F>, Failure == Never {
@RuiAAPeres
RuiAAPeres / Assertion+ReactiveSwift.swift
Last active August 5, 2020 10:46
Unit Testing ReactiveSwift streams with Assertions (idea by @andersio)
import ReactiveSwift
import XCTest
private let counterFailure: (Int, Int) -> String = { count, maxCount in
return "Counter is currently at \(count) which higher than the max \(maxCount)"
}
private let timeoutFailure = "Expectation timeout"
private let infoTemplate: (String, String, Int) -> String = { fileName, functionName, lineNumber in
@wotjd
wotjd / debug_preview.swift
Created May 14, 2020 04:26
resolve sizeThatFits not working on Xcode Preview using UIView
import SwiftUI
// https://medium.com/@lyeskinnikitaalexandrovich/mastering-xcode-previews-with-snapkit-uikit-aa82a146059a
enum DebugPreviewLayout {
case fitWidth(CGFloat)
case fitHeight(CGFloat)
case `default`
}
final class DebugPreviewLayoutView: UIView {
@nunogoncalves
nunogoncalves / BetterDecodingError.swift
Last active January 26, 2024 08:37
Better Decoding Error Messages
import Foundation
enum BetterDecodingError: CustomStringConvertible {
case dataCorrupted(_ message: String)
case keyNotFound(_ message: String)
case typeMismatch(_ message: String)
case valueNotFound(_ message: String)
case any(_ error: Error)