专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
界面切换
- 界面切换分为2种情况:
- 1.程序启动后需要根据不同的条件设置
根控制器
- 2.在
根控制器
根据不同的操作进入下一个控制器
- 1.程序启动后需要根据不同的条件设置
程序启动后需要根据不同的条件设置 根控制器
在
HMUserAccountViewModel
添加userLogin
属性,返回用户是否登录/// 用户是否登录 /// 计算性属性,只提供get方法 var userLogin: Bool { // 有账号返回true,没有账号返回false return userAccount != nil }
在
AppDelegate
中定义defaultController
方法,根据不同条件
返回对应的控制器
// MARK: - 程序启动后默认的控制器 private func defaultController() -> UIViewController { // 没有登录直接返回MainViewController if !HMUserAccountViewModel.shared.userLogin { return HMMainTabbarController() } // 判断是否是新版本 return isNewVersion() ? HMNewFeatureViewController() : HMWelcomeViewController() }
- 在
AppDelegate
的didFinishLaunchingWithOptions
方法中设置window
的根控制器
window?.rootViewController = defaultController()
将
HMBaseViewController
的userLogin
属性修改成:class HMBaseViewController: UIViewController { /// 用户是否登入标志 var userLogin: Bool = HMUserAccountViewModel.shared.userLogin ... }
根控制器
根据不同的操作进入 下一个控制器
- 有3个界面需要切换到下一个控制器
- 目标控制器有2个
HMWelcomeViewController
和HMMainTabBarController
切换控制器其实就是切换
key window
的根控制器,window
是AppDelegate
的一个属性,所以由AppDelegate
来负责切换// MARK: - 切换控制器 func switchRootViewController(controller: UIViewController) { self.window?.rootViewController = controller }
1.
欢迎界面
切换到HMMainTabbarController
/// 头像从下方移动到上方 private func moveAnimation() { // 动画, 使用弹簧效果 // usingSpringWithDamping: 范围 0 - 1之间, 数值越小弹簧效果月明显 // initialSpringVelocity: 初始速度. UIView.animate(withDuration: 1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in // 更新约束 self.iconViewBottoCons?.constant = -(UIScreen.main.bounds.height - 160) // AutoLayout动画必须调用layoutIfNeeded,layoutIfNeeded会根据约束重新设置frame self.view.layoutIfNeeded() }) { (_) -> Void in // 移动动画完成, 是label.alpha = 1 UIView.animate(withDuration: 1, animations: { () -> Void in self.messageLabel.alpha = 1 }, completion: { (_) -> Void in // TODO: 回到首页 print("动画完成") let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.switchRootViewController(controller: HMMainTabbarController()) }) } }
- 2.
新特性界面
切换到HMMainTabbarController
/// 开始按钮被点击 func didClickStartButton() { // TODO:切换到HMMainTabbarController let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.switchRootViewController(controller: HMMainTabbarController()) }
3.
OAuth界面
用户登录成功获取到用户信息后切换到HMWelcomeViewController
, 在loadUserInfo
中闭包回调中增加:// 判断后面是否跟有参数 if let query = request.url!.query { let nsQuery = query as NSString let codeString = "code=" // 判断query里面是否包含code=字符串 if nsQuery.hasPrefix(codeString) { let code = nsQuery.substring(from: codeString.characters.count) print("拦截到code = \(code)") HMUserAccountViewModel.shared.loadAccessToken(code: code, completion: { (error: Error?) in if error != nil { print("加载accessToken出错: \(error)") SVProgressHUD.showError(withStatus: "您的网络不给力") DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: { // 关闭授权控制器 self.close() }) return } // 直接关闭控制器 print("加载accessToken完成") self.close() let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.switchRootViewController(controller: HMWelcomeViewController()) }) } else { //点击取消 close() } }
发现其切换控制器代码给别人调用不是很方便
在
AppDelegate
里面添加:/// 给外面的人调用来切换界面 class func outSwitchRootViewController(controller: UIViewController) { let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.window?.rootViewController = controller }
- 调用的地方修改为类名调用:
// 切换到 HMWelcomeViewController AppDelegate.outSwitchRootViewController(controller: HMWelcomeViewController())