Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻

Lasha Efremidze efremidze

👨‍💻
View GitHub Profile
View Counter.swift
class Counter<T: Hashable> {
let id: T
private(set) var count: Int = 0
init(id: T) {
self.id = id
}
func increment() {
count += 1
}
}
View reverseStackView.swift
extension UIStackView {
func reverseZIndex() {
for view in arrangedSubviews {
sendSubview(toBack: view)
}
}
}
View hasSuperview.swift
extension UIView {
func hasSuperview(_ superview: UIView) -> Bool {
var view = self
while let _view = view.superview {
defer { view = _view }
if _view === superview {
return true
}
}
return false
View activeLabel.swift
import ActiveLabel
class ViewController: UIViewController {
lazy var footerView: UIView = {
let label = ActiveLabel()
label.customize { label in
label.text = "Tap me"
label.textAlignment = .center
let customType = ActiveType.custom(pattern: "\\bTap me\\b")
label.enabledTypes = [customType]
View TableSectionHeader.swift
class TableSectionHeader: UITableViewHeaderFooterView {
override func layoutSubviews() {
super.layoutSubviews()
textLabel?.frame.origin.y = 16
}
}
View indexPathEnum.swift
enum Section {
case profile(Row0)
case privacy(Row1)
enum Row0: Int {
case name, username, password
}
enum Row1: Int {
case email, phone, gender
}
}
View hiddenKeyPath.swift
import UIKit
let keyPath = "continuous" + "Corners"
extension CALayer {
var infiniteCorners: Bool {
get { return value(forKeyPath: keyPath) as! Bool }
set { setValue(newValue, forKeyPath: keyPath) }
}
}
View RadialGradientLayer.swift
import UIKit
class RadialGradientLayer: CALayer {
var colors = [CGColor]() { didSet { setNeedsDisplay() } }
var locations: [CGFloat]?
override func draw(in ctx: CGContext) {
ctx.saveGState()
guard let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: locations) else { return }
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
ctx.drawRadialGradient(gradient, startCenter: center, startRadius: 0, endCenter: center, endRadius: radius, options: .drawsBeforeStartLocation)
View UserDefaultsExtensions.swift
extension UserDefaults {
func set(forEvery: Int, for key: String, onChange: () -> Void) {
let count = integer(forKey: key) % forEvery
if count == 0 { onChange() }
set(count + 1, forKey: key)
}
}
View extractMethod.swift
import ObjectiveC.runtime
// https://codelle.com/blog/2016/2/calling-methods-from-strings-in-swift/
func extractMethod(_ owner: AnyObject, _ selector: Selector) -> ((Any?, Any?) -> AnyObject)? {
guard let method = getMethod(owner, selector) else { return nil }
let imp = method_getImplementation(method)
typealias CFunction = @convention(c) (AnyObject, Selector, Any?, Any?) -> Unmanaged<AnyObject>
let function = unsafeBitCast(imp, to: CFunction.self)
return { arg1, arg2 in function(owner, selector, arg1, arg2).takeUnretainedValue() }
}
You can’t perform that action at this time.