Skip to content

Instantly share code, notes, and snippets.

Natan Rolnik natanrolnik

Block or report user

Report or block natanrolnik

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@natanrolnik
natanrolnik / UIViewFitInSuperview2.swift
Last active Oct 11, 2017
Handy methods on UIView to allow easier additions of subviews (and constraints via code), only in one UIView extension
View UIViewFitInSuperview2.swift
//: 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)
}
View UICollectionViewFlowLayout+EqualSpacing.swift
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 Oct 26, 2018
Guard weak self Code Snippet
View guardweak.codesnippet
<!--
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 {
View IfAndSwitchAsExpression.swift
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 Feb 12, 2019
A playground for a small wrapper that manages multiple DispatchWorkItems
View SchedulerPlayground.swift
import Foundation
import PlaygroundSupport
typealias DispatcherIdentifier = String
class Dispatcher {
private var items = [DispatcherIdentifier: DispatchWorkItem]()
private let queue: DispatchQueue
@natanrolnik
natanrolnik / Scheduler2.swift
Created Feb 8, 2019
Another small wrapper for multiple DispatchWorkItems
View Scheduler2.swift
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 Feb 12, 2019
A simple notification throttler using DispatchWorkItem
View NotificationThrottler.swift
class NotificationThrottler {
let notificationCenter: NotificationCenter
let timeInterval: TimeInterval
let handler: () -> Void
private var workItem: DispatchWorkItem?
deinit {
notificationCenter.removeObserver(self)
}
View SimpleAsyncAfter.swift
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
self?.micButton.jump()
}
View CancelAsyncAfter.swift
//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
View CancelDispatchWorkItem.swift
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()
}
You can’t perform that action at this time.