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 / UIPageViewController+Extra.swift
Created November 17, 2017 12:16
UIPageViewController 经验总结
extension UIPageViewController {
var bk_scrollView: UIScrollView? {
if let v = view as? UIScrollView {
return v // 事实上,view不是UIScrollView类型
}
for v in view.subviews where v is UIScrollView {
return v as? UIScrollView // view.subviews 只有一个元素,其类型是 _UIQueuingScrollView,是 UIScrollView 的子类
}
return nil
@acalism
acalism / UIScrollView+SwipeBack.swift
Last active November 17, 2017 12:22
scrollView页面如何不影响屏幕左边缘返回手势
// 如果 UIViewController.view.addSubView(scrollView),会导致屏幕左边缘的返回手势失效。
// 解决办法如下:
if let ges = navigationController?.interactivePopGestureRecognizer {
scrollView.panGestureRecognizer.require(toFail: ges) // 允许左边缘手势返回
}
// 对于 UIPageViewController,可以先找到其 scrollView,再如法炮制
@acalism
acalism / FileURL_or_Path.swift
Last active November 23, 2017 03:34
pitfall when converting file url to path
// 1. file url 与 path 转换
let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory())
let path = tmpURL.path //
// 事实上,tmpURL.absoluteString得到的是 file:// 格式,而path是没有协议(scheme)部分的,应以slash开头。
// 2. 检查文件存在性
@acalism
acalism / Unsafe Bit Cast.swift
Created December 25, 2017 12:10 — forked from JadenGeller/Unsafe Bit Cast.swift
A Quick Overview of Unsafe Bit Cast
// Let's declare two structs that with different variables and different boolean values:
struct A {
let x = true
}
struct B {
let y = false
}
@acalism
acalism / UIDebug
Created January 10, 2018 10:33
UIDebuggingInformationOverlay
UIDebuggingInformationOverlay is private class created by Apple.
http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/
https://www.raywenderlich.com/177890/swizzling-in-ios-11-with-uidebugginginformationoverlay
FLEX is preferred,
https://github.com/Flipboard/FLEX
po view.recersiveDescription 可以输出整个view层次
po Thread.callStackSymbols 输出栈信息
@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)
}
}
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
// 这两个方法有冲突(调用时无法区分),除非其中一个去掉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 {
@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:someone@example.com"
if let url = URL(string: str), let uc = URLComponents(url: url, resolvingAgainstBaseURL: true) {
print(url)
print(uc, uc.url!)
print(uc.scheme!, uc.path)