Skip to content

Instantly share code, notes, and snippets.

View jimmythai's full-sized avatar

Atsushi Yamamoto jimmythai

View GitHub Profile
@jimmythai
jimmythai / 0_reuse_code.js
Created February 23, 2016 03:46
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
let frame = CGRect(x: 0, y: 0, width: 380, height: 600)
let view = UIView(frame: frame)
let gradient = CAGradientLayer()
gradient.frame = frame
gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]
gradient.startPoint = CGPoint(x: 0.408, y: 0)
gradient.endPoint = CGPoint(x: 0.746, y: 1)
view.layer.addSublayer(gradient)
import Foundation
import UIKit
public class Checkmark: UIView {
// MARK: Public variables
public var initialLayerColor: UIColor = UIColor.blue {
didSet {
initialLayer.strokeColor = initialLayerColor.cgColor
}
@jimmythai
jimmythai / Singleton.swift
Created September 30, 2017 04:17
Singleton
class Transaction {
static let shared = Transaction()
func buy(_ item: String) {
print("You bought \(item).")
}
func sell(_ item: String) {
print("You sold \(item).")
}
}
import UIKit
extension UIColor {
convenience init(hex: Int, alpha: CGFloat = 1.0) {
let max = 255
let red = CGFloat((hex & 0xFF0000) >> 16 / max)
let green = CGFloat((hex & 0x00FF00) >> 8 / max)
let blue = CGFloat((hex & 0x0000FF) / max)
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
import UIKit
extension UIView {
@IBInspectable var borderColor: UIColor? {
get {
return layer.borderColor.map { UIColor(cgColor: $0) }
}
set {
layer.borderColor = newValue?.cgColor
////////////////////////
// MARK: Array
////////////////////////
// map function
extension Array {
func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] {
var result: [T] = []
result.reserveCapacity(count)
import Foundation
// MARK: UserDefaults extension
extension UserDefaults {
struct SomeApp: UserDefaultable {
enum UserDefaultKey: String {
case someItem
}
private init() {}