Skip to content

Instantly share code, notes, and snippets.

@inamiy
inamiy / SwiftElmFrameworkList.md
Last active March 11, 2024 10:20
React & Elm inspired frameworks in Swift
@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()")
@jz709u
jz709u / Calendar.swift
Last active March 2, 2023 03:41 — forked from mecid/Calendar.swift
SwiftUI Calendar view using LazyVGrid
import SwiftUI
fileprivate extension DateFormatter {
static var month: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM"
return formatter
}
static var monthAndYear: DateFormatter {
//
// Lightweight but powerful state machine.
//
// Usage:
// enum TrafficLight: State {
// case red, orange, green
// }
//
// var trafficLights = StateMachine<TrafficLight>(initialState: .red)
// trafficLights.addRoute(from: .red, to: .green)
extension UIImage {
private final class UIImageActivityItemSource: NSObject, UIActivityItemSource {
var image = UIImage()
var title: String?
var url: URL?
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
UIImage()
}
@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 }
)
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)
}
@inamiy
inamiy / iOS16-NavigationStack.swift
Last active June 12, 2022 06:55
iOS 16 NavigationStack binding + navigationDestination behavior (Xcode 14 Beta 1, Swift 5.7) https://twitter.com/inamiy/status/1535861181998862337
import SwiftUI
struct RootStringNavStack: View {
@State
var navigationPath: [String] = []
// For hook-printing.
var navigationPathBinding: Binding<[String]> {
.init(
get: { navigationPath },