Skip to content

Instantly share code, notes, and snippets.

@yesleon
yesleon / ZoomTransitionAnimator.swift
Last active December 6, 2017 20:36
A transition animator class which zooms into a specified UIView.
//
// ZoomTransitionAnimator.swift
// ScrapBeta
//
// Created by Li-Heng Hsu on 06/12/2017.
// Copyright © 2017 Li-Heng Hsu. All rights reserved.
//
import UIKit
@yesleon
yesleon / UITextView+isEditableWhenBeingDataDetectable.swift
Last active June 4, 2018 18:40
This enables UITextView to be editable when being data detectable. Just set isEditableWhenBeingDataDetectable to true.
//
// UITextView+isEditableWhenBeingDataDetectable.swift
//
// Created by Li-Heng Hsu on 15/05/2018.
// Copyright © 2018 narrativesaw. All rights reserved.
//
import UIKit
extension UITextView {
@yesleon
yesleon / UITextView+placeholder.swift
Last active June 4, 2018 22:49
Add a placeholder property to UITextView.
//
// UITextView+placeholder.swift
//
// Created by Li-Heng Hsu on 02/06/2018.
// Copyright © 2018 narrativesaw. All rights reserved.
//
import UIKit
private let defaultPlaceholderColor = UIColor.gray
import Foundation
// 基礎協定
// 每個套用 Endpoint 的型別都有固定的回傳型別,所以如果回傳的型別不同,就要宣告不同的 Endpoint 型別。
protocol Endpoint {
// Endpoint 所關聯的回傳型別
associatedtype Result: Codable
@yesleon
yesleon / FluentInterface.swift
Created June 26, 2019 08:30
A simple struct for enabling fluent interface writing style in >= Swift 5.1.
@dynamicMemberLookup
public struct FluentInterface<Root> {
public let root: Root
public subscript<Value>(dynamicMember keyPath: WritableKeyPath<Root, Value>) -> (Value) -> Self {
var root = self.root
return { newValue in
root[keyPath: keyPath] = newValue
return FluentInterface(root: root)
}
@yesleon
yesleon / AsyncTask.swift
Last active June 27, 2019 06:52
A simple wrapper for enabling reactive programming in Swift. Some functionalities require Swift >= 5.1.
// MARK: Core
public typealias DeactivateHandler = () -> Void
public struct AsyncTask<Success, Failure> where Failure: Error {
public var activate: (@escaping (Success) -> Void, @escaping (Failure) -> Void) -> DeactivateHandler
}
extension AsyncTask where Failure == Never {
func makeObject() -> (getter: () -> String, setter: (String) -> Void) {
var text = "Hello "
return (
getter: { text },
setter: { text = $0 }
)
}
let object = makeObject()
import UIKit
typealias MutableContext<Value> = (@escaping (inout Value) -> Void) -> Void
class MasterViewController: UITableViewController {
var models = [String]()
// 點選 cell 時會呼叫的工廠方法。
func makeDetailViewController(indexPath: IndexPath) -> DetailViewController? {
@dynamicMemberLookup
protocol JSONType {
subscript(dynamicMember member: String) -> JSONType? { get set }
}
extension JSONType {
subscript(dynamicMember member: String) -> JSONType? {
get { nil }
set { }
}
subscript<T>(type: T.Type) -> T? {
//
// InteractivePushableNavigationController.swift
// InteractivePushGestureRecognizerExample
//
// Created by Li-Heng Hsu on 2020/3/11.
// Copyright © 2020 Li-Heng Hsu. All rights reserved.
//
import UIKit