专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

界面切换

  • 界面切换分为2种情况:
    • 1.程序启动后需要根据不同的条件设置 根控制器
    • 2.在根控制器根据不同的操作进入 下一个控制器

程序启动后需要根据不同的条件设置 根控制器

  • 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()
    }
    
  • AppDelegatedidFinishLaunchingWithOptions 方法中设置 window根控制器
    window?.rootViewController = defaultController()
    
  • HMBaseViewControlleruserLogin 属性修改成:

    class HMBaseViewController: UIViewController {
    
      /// 用户是否登入标志
      var userLogin: Bool = HMUserAccountViewModel.shared.userLogin
    
      ...
    }
    

根控制器根据不同的操作进入 下一个控制器

  • 有3个界面需要切换到下一个控制器
  • 目标控制器有2个 HMWelcomeViewControllerHMMainTabBarController
  • 切换控制器其实就是切换 key window 的根控制器, windowAppDelegate 的一个属性,所以由 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())
      

results matching ""

    No results matching ""