Skip to content

Instantly share code, notes, and snippets.

View Ariandr's full-sized avatar

Oleksandr Honcharov Ariandr

View GitHub Profile
@Ariandr
Ariandr / Debouncer.swift
Last active May 13, 2021 09:25
A simple Swift class that lets you debounce any closures
import Foundation
class Debouncer {
private let queue: DispatchQueue
private var task: DispatchWorkItem?
private(set) var hasPerformedWorkAfterCancel = false // helper property which is needed for some use cases to understand if the work was done
init(queue: DispatchQueue = DispatchQueue.main) {
self.queue = queue
}
import UIKit
class InsetLabel: UILabel {
private let topInset: CGFloat
private let leftInset: CGFloat
private let bottomInset: CGFloat
private let rightInset: CGFloat
init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
@Ariandr
Ariandr / GradientLayeredView.swift
Created June 13, 2018 10:05
UIView with default gradient layer and function to setup colors
import UIKit
class GradientLayeredView: UIView {
enum GradientDirection {
case leftToRight
case rightToLeft
case topToBottom
case bottomToTop
}
@Ariandr
Ariandr / gist:033ac61fde405b180d866b385d95642c
Created May 17, 2018 10:07
Adding the automated script for incrementing a build number
#In your Xcode target project settings select the "Build phases" tab.
#Click the "+" button add select "New Run Script Phase"
#In the script area paste this tiny script which will increment the bundle version every time the project is built:
#!/bin/bash
# This script takes the build number from the release target plist, increments and then saves back
bN=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PRODUCT_SETTINGS_PATH}")
bN=$(($bN + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $bN" "${PRODUCT_SETTINGS_PATH}"
@Ariandr
Ariandr / UIApplication+tryURL.swift
Created May 15, 2018 13:08
In order to open an app or a website (if the app is not available)
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
guard let url = URL(string: url) else {
continue
}
if application.canOpenURL(url) {
application.open(url, options: [:], completionHandler: nil)
return
@Ariandr
Ariandr / UIColor+ColorShade.swift
Created March 5, 2018 11:47
Make UIColor darker(-0.1) or lighter(0.5). For example, let color = color.shadeColor(factor: 0.35) // makes color lighter
import UIKit
extension UIColor {
func shadeColor(factor: CGFloat) -> UIColor {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
let t: CGFloat = factor < 0 ? 0 : 1
@Ariandr
Ariandr / UIColor+Hex.swift
Last active March 5, 2018 11:43
Creating UIColor from a hex string. Example: let color = UIColor(hex: "FFFFFF")
import UIKit
extension UIColor {
convenience init(hex: String) {
let scanner = Scanner(string: hex)
scanner.scanLocation = 0
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)