Skip to content

Instantly share code, notes, and snippets.

View iamchiwon's full-sized avatar
💭
I'm awake

Song Chiwon iamchiwon

💭
I'm awake
View GitHub Profile
@iamchiwon
iamchiwon / fn+rx.swift
Created September 17, 2018 17:02
Functions to Observable Wrapper
func reactivex<I, O>(_ f: @escaping (I) -> O) -> (I) -> Observable<O> {
return { input in
return Observable.create { emitter in
emitter.onNext(f(input))
emitter.onCompleted()
return Disposables.create()
}
}
}
@iamchiwon
iamchiwon / lastValue.swift
Created August 29, 2018 05:06
현재 값을 넣고, 이전 값을 얻는다.
func lastValue<T>(_ initValue: T) -> (T) -> T {
var value: T = initValue
return { newValue in
let last = value
value = newValue
return last
}
}
@iamchiwon
iamchiwon / ratioMap.swift
Created August 29, 2018 05:05
비례에 따른 값의 범위 변환
func ratioMap(_ _min: CGFloat, _ _max: CGFloat) -> (CGFloat, CGFloat) -> (CGFloat) -> CGFloat {
return { start, end in
{ value in
start + (value - _min) * (end - start) / (_max - _min)
}
}
}
@iamchiwon
iamchiwon / globalVars.swift
Created August 27, 2018 09:41
iPhoneX margins
let screenSize = UIScreen.main.bounds.size
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let statusHeight: CGFloat = {
guard #available(iOS 11.0, *), let window = UIApplication.shared.keyWindow else {
return 0
}
return window.safeAreaInsets.top
}()
@iamchiwon
iamchiwon / LongTableViewHeaderViewController.swift
Last active September 17, 2018 13:34
Long-TableHeaderView Sample
//
// ViewController.swift
// TableViewExample
//
// Created by iamchiwon on 2018. 8. 2..
// Copyright © 2018년 ncode. All rights reserved.
//
import UIKit
import RxSwift
@iamchiwon
iamchiwon / Rx+Control.swift
Last active August 1, 2018 08:45
Control 이벤트에 sender 를 알게 한다.
//
// Rx+Control.swift
// ReactiveSample
//
// Created by iamchiwon on 2018. 8. 1..
// Copyright © 2018년 iamchiwon. All rights reserved.
//
import UIKit
import RxSwift
@iamchiwon
iamchiwon / UIViewController+Rx.swift
Last active June 11, 2018 14:49
Rx Wrapping 예제 (methodInvoked)
import RxSwift
import RxCocoa
extension Reactive where Base: UIViewController {
var viewWillAppear: ControlEvent<Void> {
let source = methodInvoked(#selector(Base.viewWillAppear)).map { _ in }
return ControlEvent(events: source)
}
var viewDidAppear: ControlEvent<Void> {
let source = methodInvoked(#selector(Base.viewDidAppear)).map { _ in }
@iamchiwon
iamchiwon / ViewCreator.swift
Created May 18, 2018 15:22
creating view programatically
@discardableResult
func createView<T>(_ view: T,
parent: UIView?,
setting: ((T) -> ())? = nil,
constraint: ((ConstraintMaker) -> ())? = nil) -> T where T: UIView {
switch parent {
case let stack as UIStackView:
stack.addArrangedSubview(view)
case let collectionCell as UICollectionViewCell:
extension String {
func calcHeight(for width: CGFloat, font: UIFont) -> CGFloat {
let rect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingRect = self.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
return ceil(boundingRect.height)
}
func calcWidth(for height: CGFloat, font: UIFont) -> CGFloat {