Skip to content

Instantly share code, notes, and snippets.

class TouchGoThroughView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
super.point(inside: point, with: event) && pointInsideVisibleSubviews(point, with: event)
}
}
extension UIView {
func pointInsideVisibleSubviews(_ point: CGPoint, with event: UIEvent?) -> Bool {
@danielgarbien
danielgarbien / SortDescriptor.swift
Created July 14, 2022 14:45
Multi-attribute sorting with sort descriptors
import Foundation
struct SortDescriptor<Value> {
let compare: (Value, Value) -> ComparisonResult
}
extension Collection {
func sorted(by sortDescriptors: [SortDescriptor<Element>]) -> [Element] {
sorted { a, b in
extension String {
func draw(fontSize: CGFloat) -> UIImage? {
draw(font: .systemFont(ofSize: fontSize))
}
func draw(font: UIFont) -> UIImage? {
draw(attributes: [.font: font], backgroundColor: .clear)
}
extension KeyedDecodingContainer {
func decode<T: NSCoding>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T? {
let data = try decode(Data.self, forKey: key)
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T ?? nil
}
}
extension KeyedEncodingContainer {
@objcMembers
private class BlockHandler: NSObject {
var block: (() -> Void)?
@objc func runBlock() {
block?()
}
}
import UIKit
class FullContentIntrinsicSizeTableView: UITableView {
override var contentSize: CGSize {
didSet {
if contentSize != oldValue {
invalidateIntrinsicContentSize()
}
}
import UIKit
extension UIAlertController {
func setLeftAlignedMessage(_ message: String) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let messageText = NSMutableAttributedString(
string: message,
import UIKit
extension UITableView {
func dequeueReusableCell(withIdentifier identifier: String, registerNibIfNeededWithNibName nibName: String) -> UITableViewCell? {
guard let cell = dequeueReusableCell(withIdentifier: identifier) else {
register(UINib(nibName: nibName, bundle: nil), forCellReuseIdentifier: identifier)
return dequeueReusableCell(withIdentifier: identifier)
}
func delay(_ delay: Double, closure: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: closure)
}
import Foundation
import UIKit
class SimpleCollectionViewDataSource<Cell: AnyObject, Object>: NSObject, UICollectionViewDataSource {
var objects: [[Object]]
func objectAtIndexPath(indexPath: NSIndexPath) -> Object {
return objects[indexPath.section][indexPath.row]
}