• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift实现一句代码不用写加载首次启动引导图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Swift实现一句代码不用写加载首次启动引导图

#

kImageNamsArray:引导页的图片数组
kHiddenBtnImageName: 关闭引导页的按钮图片
kHiddenBtnCenter: 关闭引导页按钮的位置

实现原理

1.给Appdelegate增加扩展, 添加guaidWindow属性

 private struct AssociatedKeys {
        static var guideWindowKey = "guideWindowKey"
    }

    private var guaidWindow: UIWindow? {

        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.guideWindowKey) as? UIWindow
        }

        set {
            if newValue != nil {
                objc_setAssociatedObject(self, &AssociatedKeys.guideWindowKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }

2.通过运行时进行方法的交换

public override class func initialize(){

        DispatchQueue.once(token: "交换appDelegate方法") {
            let lastVersion = UserDefaults.standard.float(forKey: "lastVersionKey")
            let currentVersion = Float((Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "0")!
            if currentVersion > lastVersion {
                let originalMethod = class_getInstanceMethod(self, #selector(application(_:didFinishLaunchingWithOptions:)))
                let customMethod = class_getInstanceMethod(self, #selector(guide_application(_:didFinishLaunchingWithOptions:)))
                method_exchangeImplementations(originalMethod, customMethod)
            }

        }

    }


    func guide_application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


        guaidWindow = UIWindow()
        guaidWindow!.frame = UIScreen.main.bounds
        guaidWindow!.backgroundColor = UIColor.white
        guaidWindow!.windowLevel = UIWindowLevelStatusBar + 1
        self.guaidWindow!.makeKeyAndVisible()

        let vc = ZSGuaidViewController()
        vc.hide = {[weak self] in
            print("1111")
            let currentVersion = Float((Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "0")!
            UserDefaults.standard.set(currentVersion, forKey: "lastVersionKey")
            UserDefaults.standard.synchronize()

            self?.guaidWindow?.resignKey()
            self?.guaidWindow?.isHidden = true
            self?.guaidWindow = nil
        }

        guaidWindow?.rootViewController = vc
        return guide_application(application, didFinishLaunchingWithOptions: launchOptions)
    }

}

Demo地址觉得有用给个��, Demo也实现了一句话加载广告页.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode981.基于时间的键值存储|TimeBasedKey-ValueStore发布时间:2022-07-13
下一篇:
Swift3颜色转换成图片Image发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap