Skip to content

Instantly share code, notes, and snippets.

View Josscii's full-sized avatar
💭
??

Josscii Josscii

💭
??
View GitHub Profile
@Josscii
Josscii / SwiftBlog.md
Created May 6, 2020 00:19
收集一些还不错的 Swift Blog
@Josscii
Josscii / AsyncOperation.swift
Created May 5, 2020 07:33
一个简单常用的异步 Operation 实现
// https://www.avanderlee.com/swift/asynchronous-operations/
class AsyncOperation: Operation {
private let lockQueue = DispatchQueue(label: "com.swiftlee.asyncoperation", attributes: .concurrent)
override var isAsynchronous: Bool {
return true
}
private var _isExecuting: Bool = false
@Josscii
Josscii / RequestInterceptor.swift
Last active July 16, 2020 06:16
如何用 alamofire 处理 JWT 过期?(不适用于不拿http状态码做错误码的方式
// https://www.avanderlee.com/swift/authentication-alamofire-request-adapter/
/// The storage containing your access token, preferable a Keychain wrapper.
protocol AccessTokenStorage: class {
typealias JWT = String
var accessToken: JWT { get set }
}
final class RequestInterceptor: Alamofire.RequestInterceptor {
@Josscii
Josscii / SwiftBySundell.md
Last active May 5, 2020 01:04
读 SwiftBySundell blog 学到的小技巧,截止2020.5.5,大部分内容已经快速过完

SwiftBySundell 阅读笔记

Writing small utility functions in Swift

func configure<T>(
    _ value: T,
    using closure: (inout T) throws -> Void
) rethrows -> T {
 var value = value
@Josscii
Josscii / UserDefaultsExtension.swift
Created May 4, 2020 05:15
比较好的 UserDefaults 的用法
//https://developer.apple.com/videos/play/wwdc2019/212
extension UserDefaults {
private static let isInfoBarHiddenKey = "IsInfoBarHidden"
@objc dynamic var isInfoBarHidden: Bool {
get { bool(forKey: UserDefaults.isInfoBarHiddenKey) }
set { set(newValue, forKey: UserDefaults.isInfoBarHiddenKey) }
}
}
@Josscii
Josscii / ImplictAnimationRemoval.swift
Created May 3, 2020 08:01
去掉 CALayer 的隐式动画
//https://stackoverflow.com/a/58518282/4819236
extension CALayer {
var areAnimationsEnabled: Bool {
get { delegate == nil }
set { delegate = newValue ? nil : CALayerAnimationsDisablingDelegate.shared }
}
}
private class CALayerAnimationsDisablingDelegate: NSObject, CALayerDelegate {
@Josscii
Josscii / FixControlTouchScrollView.swift
Created May 3, 2020 07:40
修复在 scrollView 中的 UIControl 及其子类的点击问题
import UIKit
class FixControlTouchScrollView: UIScrollView {
override init(frame: CGRect) {
super.init(frame: frame)
delaysContentTouches = false
}
required init?(coder: NSCoder) {
super.init(coder: coder)
@Josscii
Josscii / EnlargeButton.swift
Last active May 3, 2020 07:37
增大 button 的可点击范围,之所以写了两种,是因为第二种有可能会被其他重写或者 oc category 给覆盖掉。
class EnlargeButton: UIButton {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return bounds.insetBy(dx: -20, dy: -20).contains(point)
}
}
extension UIButton {
open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return bounds.insetBy(dx: -20, dy: -20).contains(point)
}
@Josscii
Josscii / BackgroundButton.swift
Last active May 3, 2020 07:38
让 UIButton 支持设置不同状态下背景颜色,支持 xib
import UIKit
// https://spin.atomicobject.com/2018/04/25/uibutton-background-color/
// Declare a global var to produce a unique address as the assoc object handle
private var normalColorHandle: UInt8 = 0
private var disabledColorHandle: UInt8 = 0
private var highlightedColorHandle: UInt8 = 0
private var selectedColorHandle: UInt8 = 0