This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let randomInt = Int.random(in: 0...3) | |
let spelledOut: String = { | |
switch randomInt { | |
case 0: | |
return "Zero" | |
case 1: | |
return "One" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PlaygroundSupport | |
typealias DispatcherIdentifier = String | |
class Dispatcher { | |
private var items = [DispatcherIdentifier: DispatchWorkItem]() | |
private let queue: DispatchQueue | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PlaygroundPage.current.needsIndefiniteExecution = true | |
typealias DispatcherIdentifier = String | |
extension DispatchQueue { | |
static var associatedValueKey = 0 | |
func schedule(after timeInterval: TimeInterval, | |
with identifier: DispatcherIdentifier, | |
action: @escaping () -> Void) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotificationThrottler { | |
let notificationCenter: NotificationCenter | |
let timeInterval: TimeInterval | |
let handler: () -> Void | |
private var workItem: DispatchWorkItem? | |
deinit { | |
notificationCenter.removeObserver(self) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in your viewDidAppear method, schedule it: | |
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in | |
self?.micButton.jump() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |