Skip to content

Instantly share code, notes, and snippets.

import UIKit
protocol InnerCollectionViewCell: UICollectionViewCell {
associatedtype Item: Hashable
func configure(with item: Item, index: Int)
}
extension UICollectionViewFlowLayout {
func rect(at index: Int) -> CGRect {
CGRect(
@cjnevin
cjnevin / InfiniteClippingHorizontalCollectionView.swift
Last active May 13, 2023 16:39
Infinite horizontal collection view that clips items when scroll view comes to a rest
import UIKit
class InfiniteCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
private var total: Int { additionalItemCount + items.count }
private var additionalItemCount: Int = 0
private let threshold: Int = 3 // will need to be based on orientation and screen size to perform correctly
private var items: [UIColor] = []
private let itemSize: CGSize = .init(width: 150, height: 150)
private var itemsWidth: CGFloat { CGFloat(items.count) * itemSize.width }
private lazy var flowLayout = UICollectionViewFlowLayout()
import SwiftUI
struct Tab: Equatable, Identifiable {
var id: UUID = UUID()
var color: Color
}
struct OffsetKey: PreferenceKey {
static var defaultValue: CGFloat = 0
@cjnevin
cjnevin / InfiniteHorizontalStackView.swift
Last active May 4, 2023 14:27
Infinite Horizontal ScrollView in SwiftUI (no bounce behaviour)
import SwiftUI
struct ContentView: View {
@State var offset: CGFloat = 190
@State var endOffset: CGFloat = 190
@State var items: [Int] = [
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
]
var itemSpacing: CGFloat = 10
import SwiftUI
extension View {
func sticky(in coordinateSpace: CoordinateSpace) -> some View {
modifier(StickyViewModifier(coordinateSpace: coordinateSpace))
}
}
private struct StickyViewModifier: ViewModifier {
let coordinateSpace: CoordinateSpace
import SwiftUI
enum Behaviour {
case pinnedBelow
case normal
case pinnedAbove
}
struct NonStickyView<Content: View>: View {
let id: String
import SwiftUI
enum Constants {
static let backgroundHeight: CGFloat = 500
static let headerHeight: CGFloat = 150
static let contentHeight: CGFloat = 350
}
struct ColoredView: View {
let color: Color
import SwiftUI
enum Constants {
static let heroHeight: CGFloat = 500
static let menuHeight: CGFloat = 150
}
struct LargeHeaderView: View {
let color: Color
import UIKit
enum Style {
static let backgroundColor = UIColor.black
static let spacing = CGFloat(8)
static let filterStackHeight = CGFloat(34)
static let filterHeaderHeight = (filterStackHeight * 2) + (spacing * 3)
}
class ViewController: UIViewController {
import Foundation
var injectedLines = """
@Injected
private var someSharedValue: SharedValue.Type
@Injected
private var someUnregisteredValue: UnregisteredValue
#if os(tvOS)
@Injected