Swift一款絲滑的側滑返回

weixin_34320159發表於2017-06-22

SwiftFullScreenPop - 基於Swift3.0實現全屏側滑返回

Features

  • 解決自定義navigationBar側滑返回不能用快速幫你實現側滑返回功能。
  • 解決當某個介面navigationBar隱藏時下個介面有導航導致側滑返回體驗很差。
  • 解決UIScrollView不能使用側滑返回。

CocoaPods

platform :ios, '7.0'

target 'Your Project' do
    pod 'SwiftFullScreenPop'
end

Usage

  • 在需要使用的地方匯入標頭檔案import SwiftFullScreenPop
  • 可以在自己的UINavigationController基類裡面加入下面程式碼
 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        UINavigationController.swizzle()
        UIViewController.viewControllerSwizzle()
    }
  • 在ViewController裡面直接使用
// 不支援側滑true
isInteractivePopDisable = true
// 隱藏當前頁面導航
self.isPrefersNavigationBarHidden = true

Swizzle

  • 基於extension UInavigationController 和 extension UIViewController實現swizzle controller push function;viewWillAppear and ViewWillDisappear
DispatchQueue.once(token: Static.token) {
            let originalSelector = #selector(UINavigationController.pushViewController(_:animated:))
            let swizzlerSelector = #selector(UINavigationController.myPushViewController(_:animated:))
            let originalMethod = class_getInstanceMethod(UINavigationController.self, originalSelector)
            let swizzlerMethod = class_getInstanceMethod(UINavigationController.self, swizzlerSelector)
            //在進行 Swizzling 的時候,需要用 class_addMethod 先進行判斷一下原有類中是否有要替換方法的實現
            let isAddMethod = class_addMethod(UINavigationController.self, originalSelector, method_getImplementation(swizzlerMethod), method_getTypeEncoding(swizzlerMethod))
            if isAddMethod
            {
                class_replaceMethod(UINavigationController.self, swizzlerSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            }else {
                method_exchangeImplementations(originalMethod, swizzlerMethod)
            }
        }
        
        DispatchQueue.once(token: Static.token) {
            let originalSelector = #selector(UIViewController.viewWillAppear(_:))
            let swizzlerSelector = #selector(UIViewController.myViewWillApper(_:))
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzlerMethod = class_getInstanceMethod(self, swizzlerSelector)
            let isAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzlerMethod), method_getTypeEncoding(swizzlerMethod))
            if isAddMethod
            {
                class_replaceMethod(self, swizzlerSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            }else {
                method_exchangeImplementations(originalMethod, swizzlerMethod)
            }
            
            let disappear_originalSEL = #selector(UIViewController.viewWillDisappear(_:))
            let disappear_swizzleSEL = #selector(UIViewController.myViewWillDisappear(_:))
            let disappear_originalMethod = class_getInstanceMethod(self, disappear_originalSEL)
            let disappear_swizzleMethod = class_getInstanceMethod(self, disappear_swizzleSEL)
            let isAdd: Bool = class_addMethod(self, disappear_originalSEL, method_getImplementation(disappear_swizzleMethod), method_getTypeEncoding(disappear_swizzleMethod))
            if isAdd {
                class_replaceMethod(self, disappear_swizzleSEL, method_getImplementation(disappear_originalMethod), method_getTypeEncoding(disappear_originalMethod))
            }else {
                 method_exchangeImplementations(disappear_originalMethod, disappear_swizzleMethod)
            }
        }
  • 具體看本文Demo程式碼。
  • 我的:github

在實現cocoaPods遇到的坑

[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run: 
    `echo "2.3" > .swift-version`. 

在終端裡面執行:echo "3.0" > .swift-version

  • 如果提示Authentication token is invalid or unverified. Either verify it with the email that was sent or register a new session.
    重新在終端裡面執行:pod trunk register 郵箱名 “使用者名稱” 再到郵箱裡面重新驗證即可
  • 如果想知道如何實現自己的程式碼用cocoapods管理可移動我的簡書文章

順便提及下關於本地的程式碼如何git管理主要以下幾步

  1. git init. //當前需要提交的檔案路徑,和github上面XXXX.git一致
  2. git add filename
  3. git commit -m '提交資訊說明' //新增描述
  4. git pull //資料同步
  5. git push origin master //提交資料
  • 遇到如下錯誤
error: failed to push some refs to 'git@github.com:LeeFengHY/SwiftFullScreenPop.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

是因為遠端repository和我本地的repository衝突導致的,如下解決方法:

  • 1:

    git pull origin master

    git push -u origin master
  • 2:若不想merge遠端和本地修改,可以先建立新的分支:

    git branch [name]

    git push -u origin [name]

聯絡

  • 留言或者加我QQ:578545715

相關文章