iOS日常开发

Swift一款丝滑的侧滑返回

2017-06-22  本文已影响35人  风与鸾

SwiftFullScreenPop - 基于Swift3.0实现全屏侧滑返回

Features

CocoaPods

platform :ios, '7.0'

target 'Your Project' do
    pod 'SwiftFullScreenPop'
end

Usage

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        UINavigationController.swizzle()
        UIViewController.viewControllerSwizzle()
    }
// 不支持侧滑true
isInteractivePopDisable = true
// 隐藏当前页面导航
self.isPrefersNavigationBarHidden = true

Swizzle

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)
            }
        }

在实现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

顺便提及下关于本地的代码如何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冲突导致的,如下解决方法:

联系

上一篇 下一篇

猜你喜欢

热点阅读