Skip to content

Instantly share code, notes, and snippets.

View Tunous's full-sized avatar

Łukasz Rutkowski Tunous

View GitHub Profile
@Tunous
Tunous / SwiftUIView.swift
Created November 9, 2022 14:55
SwiftUI view wrapper for UIKit that calculates its size
import SwiftUI
final class SwiftUIView<Content: View>: UIView {
private var heightConstraint: NSLayoutConstraint?
init(content: Content, onSizeChanged: @escaping (CGSize) -> Void) {
super.init(frame: .zero)
let sizeReadingContent = content.readSize { [weak self] newSize in
@Tunous
Tunous / EquatableAction.swift
Last active July 11, 2023 08:14
Equatable search action
import SwiftUI
struct ContentView: View {
@State private var id = UUID()
@State private var isOn = false
var body: some View {
VStack {
// Changing this toggle will cause child views to update colors
Toggle("Toggle", isOn: $isOn)
@Tunous
Tunous / TapPanGestureRecognizer.swift
Last active July 19, 2023 18:02
TapPanGestureRecognizer
import SwiftUI
public class TapPanGestureRecognizer: UIGestureRecognizer {
private var waitingForSecondTap: Task<Void, Never>?
public private(set) var initialLocation: CGPoint = .zero {
didSet {
currentLocation = initialLocation
}
}
@Tunous
Tunous / gist:4d7c56fb13813bf1912a99aaae062cc2
Last active September 3, 2023 09:35
StateObject memory leak
import SwiftUI
struct ContentView: View {
@State private var showSheet1 = false
@State private var showSheet2 = false
@State private var showSheet3 = false
var body: some View {
VStack {
Button("State object created directly") {
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
ChildView()
ChildView()
}
.padding()
.overlayPreferenceValue(ViewPreferenceKey.self) { $0 }