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

xiaoxiao2021-02-28  88

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也实现了一句话加载广告页.

转载请注明原文地址: https://www.6miu.com/read-82679.html

最新回复(0)