Skip to content

Instantly share code, notes, and snippets.

View kazuhiro4949's full-sized avatar

Kazuhiro Hayashi kazuhiro4949

View GitHub Profile
@kazuhiro4949
kazuhiro4949 / Regex.swift
Last active May 7, 2016 06:38
Swift Regex wrapper snippet. ( supporting version 2.0+ )
/**
#### Example
if case Regex("^regular\s.+$") = "regular expression" {
return true
}
switch "regular expression" {
case Regex("^regular\s.+$"):
print("match ^regular\s.+$")
case Regex("^regurar\s.+$"):
@kazuhiro4949
kazuhiro4949 / IntRawValueEnumerable.swift
Last active May 4, 2016 05:55
It make enum return the array of cases
/**
adapt to enum which is Int Raw Value.
It make enum return the array of cases
#### Example
```
enum SomeEnum: IntRawValueEnumerable {
case AAA
case BBB
}
@kazuhiro4949
kazuhiro4949 / UIColor+Hex.swift
Created May 4, 2016 06:37
convert rgb to UIColor object
extension UIColor {
/**
convert rgb to UIColor object
*/
class func color(color: UInt32, alpha : CGFloat = 1.0) -> UIColor {
let r = CGFloat((color & 0xFF0000) >> 16) / 255.0
let g = CGFloat((color & 0x00FF00) >> 8) / 255.0
let b = CGFloat(color & 0x0000FF) / 255.0
return UIColor(red:r,green:g,blue:b,alpha:alpha)
}
@kazuhiro4949
kazuhiro4949 / MyViewController.swift
Last active February 11, 2020 03:08
UICollectionViewController including type erased delegate
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
class MyViewController<Delegate: MyViewControllerDelegate>: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var delegate: AnyMyViewControllerDelegate<Delegate>?
var items = [Array<Delegate.Item>]() {
@kazuhiro4949
kazuhiro4949 / PickerKeyboard.swift
Last active January 4, 2019 04:10
Show UIPickerView as "Custom Keyboard"
import UIKit
class PickerKeyboard: UIControl {
var data: [String] = ["Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sut.", "Sun."]
fileprivate var textStore: String = ""
override func draw(_ rect: CGRect) {
UIColor.black.set()
UIRectFrame(rect)
@kazuhiro4949
kazuhiro4949 / CharactorAnimation.swift
Created March 21, 2017 14:37
potatotips #37 sample code
hoge
class TextView: UITextView {
lazy var moreLabel: UILabel = {
let label = UILabel()
label.text = "さらに表示"
label.textColor = .gray
label.font = self.font
label.textAlignment = .center
return label
}()
//: Playground - noun: a place where people can play
import UIKit
// Cake PatternもどきをSwiftで実装したもの。要するにProtocolのコンポジションによって依存の定義、解決をしていく
protocol Animal {}
struct Cat: Animal {}
@kazuhiro4949
kazuhiro4949 / one_shot_animated_gif.swift
Last active July 29, 2018 01:55
One shot gif animation with UIImageView.images
import UIKit
import ImageIO
// make one shot gif animation from Gif.
let gif = Gif(with: "GIF File Name (not in asset catalog)")!
let base: Double = 4000
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
// set start image before starting animation
imageView.image = gif.frames.first
import SwiftUI
struct ContentView: View {
private static let imageHeight: CGFloat = 300
@State private var offset: CGFloat = 0
var body: some View {
ScrollView {