Skip to content

Instantly share code, notes, and snippets.

View natanrolnik's full-sized avatar

Natan Rolnik natanrolnik

View GitHub Profile
@natanrolnik
natanrolnik / UIViewFitInSuperview2.swift
Last active October 11, 2017 18:31
Handy methods on UIView to allow easier additions of subviews (and constraints via code), only in one UIView extension
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
extension UIView {
func addAndFitSubview(_ subview: UIView, insets: UIEdgeInsets = .zero) {
addSubview(subview)
subview.fitInSuperview(with: insets)
}
@natanrolnik
natanrolnik / UICollectionViewFlowLayout+EqualSpacing.swift
Created January 4, 2017 20:29
Make spacing and section insets equal
import UIKit
extension UICollectionViewFlowLayout {
func equalizeSpacing(with minimumSpacing: CGFloat, aItemSize: CGSize? = nil, sectionInsetTop: CGFloat = 0, sectionInsetBottom: CGFloat = 0) {
guard let collectionView = collectionView else { return }
let totalWidth = collectionView.frame.width
let itemSizeToUse = aItemSize ?? itemSize
itemSize = itemSizeToUse
@natanrolnik
natanrolnik / guardweak.codesnippet
Last active October 26, 2018 09:54
Guard weak self Code Snippet
<!--
Related: https://twitter.com/natanrolnik/status/1055756310023716865
The following snippet should be copied to:
~/Library/Developer/Xcode/UserData/CodeSnippets
as, for example, guardweak.codesnippet.
When you type `guardweakself` inside a function scope, it will generate the following code snippet:
guard let <#self#> = <#self#> else {
@natanrolnik
natanrolnik / IfAndSwitchAsExpression.swift
Created February 7, 2019 11:09
if and switch as expressions
import Foundation
let randomInt = Int.random(in: 0...3)
let spelledOut: String = {
switch randomInt {
case 0:
return "Zero"
case 1:
return "One"
@natanrolnik
natanrolnik / SchedulerPlayground.swift
Last active June 25, 2023 06:02
A playground for a small wrapper that manages multiple DispatchWorkItems
import Foundation
import PlaygroundSupport
typealias DispatcherIdentifier = String
class Dispatcher {
private var items = [DispatcherIdentifier: DispatchWorkItem]()
private let queue: DispatchQueue
@natanrolnik
natanrolnik / Scheduler2.swift
Created February 8, 2019 14:06
Another small wrapper for multiple DispatchWorkItems
PlaygroundPage.current.needsIndefiniteExecution = true
typealias DispatcherIdentifier = String
extension DispatchQueue {
static var associatedValueKey = 0
func schedule(after timeInterval: TimeInterval,
with identifier: DispatcherIdentifier,
action: @escaping () -> Void) {
@natanrolnik
natanrolnik / NotificationThrottler.swift
Created February 12, 2019 13:03
A simple notification throttler using DispatchWorkItem
class NotificationThrottler {
let notificationCenter: NotificationCenter
let timeInterval: TimeInterval
let handler: () -> Void
private var workItem: DispatchWorkItem?
deinit {
notificationCenter.removeObserver(self)
}
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
self?.micButton.jump()
}
//keep a variable to know if the user tapped the button:
var micButtonTapped = false
func recordVoiceMessage() {
//if the user tapped/held the mic button, set the variable to true
micButtonTapped = true
}
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
var micHintWorkItem: DispatchWorkItem?
func recordVoiceMessage() {
micHintWorkItem?.cancel()
}
//in your viewDidAppear method, create and schedule the work item:
micHintWorkItem = DispatchWorkItem { [weak self] in
self?.micButton.jump()
}