Skip to content

Instantly share code, notes, and snippets.

View acalism's full-sized avatar
🏠
Working from home

Dawn Song acalism

🏠
Working from home
  • Tencent, Alibaba
  • Shenzhen City, Guangdong Province, China
View GitHub Profile
@acalism
acalism / UIButton+Extended.swift
Last active October 26, 2024 05:12
扩大了响应范围的 button,提供了两种办法
private var ExtendEdgeInsetsKey: Void?
extension UIButton {
/// 设置此属性即可扩大响应范围, 分别对应上左下右
/// 优势:与Auto-Layout无缝配合
/// 劣势:View Debugger 查看不到增加的响应区域有多大,
var extendEdgeInsets: UIEdgeInsets {
get {
return objc_getAssociatedObject(self, &ExtendEdgeInsetsKey) as? UIEdgeInsets ?? UIEdgeInsets.zero
@acalism
acalism / CoreGraphicsExtensions.swift
Created February 7, 2018 12:54
为 CoreGraphics 添加一些有用的扩展
extension UIOffset {
/// 转换为 CGPoint 形式
var point: CGPoint {
return CGPoint(x: horizontal, y: vertical)
}
}
extension CGPoint {
/// 转换为 UIOffset
var offset: UIOffset {
@acalism
acalism / String+Unwrap.swift
Created February 2, 2018 14:07
How to print Optional<Wrapped>
fileprivate protocol _Optional {
func unwrappedString() -> String
}
extension Optional: _Optional {
fileprivate func unwrappedString() -> String {
switch self {
case .some(let wrapped as _Optional): return wrapped.unwrappedString()
case .some(let wrapped): return String(describing: wrapped)
case .none: return String(describing: self)
@acalism
acalism / FindHost.swift
Last active April 4, 2020 00:03
Find hostView or hostViewController
import UIKit
// MARK: - FindHostView
protocol FindHostView {
associatedtype T
var hostView: T? { get }
}
@acalism
acalism / scrollDidEnd.swift
Created February 1, 2018 12:14
When UIScrollView end scrolling?
// MARK: - UIScrollViewDelegate
// 找到结束滚动事件
func scrollViewDidEndDragging(_ sv: UIScrollView, willDecelerate decelerate: Bool) {
if !sv.isDragging, !sv.isDecelerating, !decelerate {
scrollViewDidEndScroll(sv)
}
}
@acalism
acalism / URL.swift
Last active January 30, 2018 13:14
对于不带双斜杠的url,URL和URLComponents的处理结果不能让人满意
// 1. URL 转为 URLComponents 时,什么时候会变成 nil
// 2. URLComponents 转为 URL 时,什么时候会变成 nil
// 3. URL 协议后不带双斜杠时,为什么 host 会变成 path(如下例)
let str = "http:im.qq.com" // "mailto:[email protected]"
if let url = URL(string: str), let uc = URLComponents(url: url, resolvingAgainstBaseURL: true) {
print(url)
print(uc, uc.url!)
print(uc.scheme!, uc.path)
// 这两个方法有冲突(调用时无法区分),除非其中一个去掉IndexPath后的问号
/// 不能有默认参数 nil,因为inout不能操作常量
func shouldChange(indexPath: inout IndexPath?) -> Bool {
indexPath = IndexPath(item: 0, section: 1)
return true
}
/// 可以有默认参数 nil,
func shouldChange(indexPath: UnsafeMutablePointer<IndexPath?>? = nil) -> Bool {
extension WKWebView {
/// sync version
///
/// - Parameters:
/// - js: 要执行的javascript代码
/// - timeout: 限时返回(单位秒),0表示不限时
/// - Returns: 执行结果
func evaluatingJavaScript(_ js: String, timeout: TimeInterval = 0) -> Any? {
var res: Any? = nil
var finish = false
@acalism
acalism / howto+keyboardWillShow.swift
Last active January 22, 2018 16:35
keyboard comes with scrolling
// textField included by a cell, and the cell included by a scrollView
override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
if newWindow == nil {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillChangeFrame, object: nil)
} else {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardFrameWillChange(_:)), name: .UIKeyboardWillChangeFrame, object: nil)
}
}
po view.recersiveDescription 可以输出整个view层次
po Thread.callStackSymbols 输出栈信息