Swift5.x版本,ABI(应用程序二进制接口)终于稳定,也是时候从OC转Swift了😁
熟悉的OC框架,有的可以桥接继续使用,如AFNetworking、MJRefresh、YYKit等;也有对Swift不友好的,如KSGuaidView
分析原因是Swift不支持宏定义,而KSGuaidView中使用宏定义单例manager
#ifndef KSGuaidManager
#define KSGuaidManager [KSGuaidViewManager manager]
@implementation KSGuaidViewManager
static KSGuaidViewManager* _manager = nil;
static dispatch_once_t _onceToken;
+ (instancetype)manager{
dispatch_once(&_onceToken, ^{
_manager = [[super allocWithZone:NULL] init];
});
return _manager;
}
...
所以仿照KSGuaidView思路写了一个Swift版的WelcomePage,方便转Swift的童鞋使用。同样可以实现两种展示方式
主要侵入代码:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
loadWelcomePage()
return true
}
private func loadWelcomePage() {
//显示要欢迎的图片数组
GuideManager.share.images = [UIImage(named: "start_page1")!, UIImage(named: "start_page2")!, UIImage(named: "start_page3")!]
//显示“点击体验”按钮,不赋值就是滚动进入应用
GuideManager.share.enterImage = UIImage(named: "start_enter")
//默认是展示UIPageControl控件,不需要展示打开下行注释即可
//GuideManager.share.isShowPageControl = false
GuideManager.share.checkVersion()
}
主要代码:
func showGuideView(_ currentVersion: String) {
guideWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
guideWindow?.windowLevel = .statusBar
let rootVC = GuideViewController()
rootVC.images = images
rootVC.enterImage = enterImage
rootVC.isShowPageControl = isShowPageControl
rootVC.dismissClosure = { [weak self] in
try? currentVersion.write(toFile: VersionPath, atomically: true, encoding: .utf8)
self?.dismissGuideView()
}
guideWindow?.rootViewController = rootVC
guideWindow?.makeKeyAndVisible()
}
将WelcomePage文件夹拖入项目,AppDelegate.swfit
中配置即可
- 特别说明:
需要删除SceneDelegate.swift
及相关配置代码才能正常显示欢迎页。
完整Demo