1. 程式人生 > >大作業小巴學車的總結

大作業小巴學車的總結

自定義tabbarcontroller 主要建立一個類繼承自uitabbarcontroller 隨後將tabView移除 設定自己的view即可

setupChildVC

  func setUpChildVC(){
    let sb = UIStoryboard(name: "Main", bundle: nil)
  
         learnCarVC = sb.instantiateViewController(withIdentifier: "learnVC") as? MapViewController
       serveVC = sb.instantiateViewController(withIdentifier: "serviceVC") as? ServiceViewController
         peiJiaVC = sb.instantiateViewController(withIdentifier: "peijiaVC") as? WithDrivingViewController
         qusVC = sb.instantiateViewController(withIdentifier: "qusVC") as? QusViewController
        let serviceNav = UINavigationController(rootViewController: serveVC!)
        let learnCarNav = UINavigationController(rootViewController: learnCarVC!)
        let qusNav = UINavigationController(rootViewController: qusVC!)
        let peijiaNav = UINavigationController(rootViewController: peiJiaVC!)
  
        
        
        
        
        
//        serviceNav.tabBarItem.title = "服務"
//        serviceNav.tabBarItem.image = UIImage(named: "ic_serve_tab")
//        serviceNav.tabBarItem.selectedImage =  UIImage(named: "icon_tab_right")
//        learnCarVC.tabBarItem.title = "學車"
//        learnCarVC.tabBarItem.image = UIImage(named: "ic_learn_tab")
//        learnCarNav.tabBarItem.selectedImage =  UIImage(named: "icon_tab_middle")
//        peiJiaVC?.tabBarItem.title = "陪駕"
//        peiJiaVC?.tabBarItem.image = UIImage(named: "ic_peijia_tab")
//        peiJiaVC?.tabBarItem.selectedImage =  UIImage(named: "icon_tab_middle")
//
//        qusNav.tabBarItem.title = "題庫"
//        learnCarNav.tabBarItem.image = UIImage(named: "ic_exercise_tab")
//        learnCarNav.tabBarItem.selectedImage =  UIImage(named: "icon_tab_left")
     
         //        qusVC.navigationItem.leftBarButtonItem = leftItem
//
//        serviceVC.navigationItem.leftBarButtonItem = leftItem
//
        
        
        
        self.viewControllers = [qusNav,learnCarNav,peijiaNav,serviceNav]
       
        
        
        
    }
setuptabbarView

  func setupTabBar() {
        let rect = self.tabBar.frame
        self.tabBar.removeFromSuperview()
         myView = UIView(frame: CGRect(x: 0, y: rect.origin.y - 20, width: SCREEN_WIDTH, height: rect.size.height + 20))
      
        self.view.addSubview(myView!)
        for i in 0 ..< 4{
            let button = CDTabBarButton()
        
            button.setTitle(titles[i], for: .normal)
            button.setImage(UIImage(named:unselImages[i]), for: .normal)
            button.setImage(UIImage(named:selImages[i]), for: .selected)
            let x = (CGFloat)(i)  * (myView?.frame.size.width)! / 4
            button.frame = CGRect(x: x, y: 0, width: (myView?.frame.size.width)! / 4, height: (myView?.frame.size.height)!)
            button.tag = i
         button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            button.imageEdgeInsets = UIEdgeInsetsMake(5, 13, 21,(button.titleLabel?.bounds.size.width)!)
            button.titleLabel?.textAlignment = .center
            button.titleEdgeInsets = UIEdgeInsetsMake(40, -1 * (button.titleLabel?.bounds.size.width)! - 45, 0, 0)
            button.setTitleColor(UIColor.black, for: .normal)
            myView?.addSubview(button)
            button.addTarget(self, action: #selector(CDTabbarViewController.clickBtn), for: .touchUpInside)
            if(i == 1){
                button.isSelected = true
                self.selectedBtn = button
               self.selectedIndex = 1
            }
        }
    }
隨後實現clickBtn即可

 func clickBtn(sender:UIButton) {
        
        
        
        
       self.selectedBtn.isSelected = false;
        sender.isSelected = true
        self.selectedBtn = sender
        self.selectedIndex = sender.tag
    }
整體程式碼如下

//
//  CDTabbarViewController.swift
//  xiaobaxuecheProject
//
//  Created by hznucai on 2016/12/13.
//  Copyright © 2016年 hznucai. All rights reserved.
//

import UIKit

class CDTabbarViewController: UITabBarController {
    var selectedBtn = UIButton()
    var myView:UIView?
  
    let selImages = ["ic_exercise_tab","ic_learn_tab","ic_peijia_tab","ic_serve_tab"]
    let unselImages = ["icon_tab_left","icon_tab_middle","icon_tab_middle","icon_tab_right"]
 let titles = ["題庫","學車","陪駕","服務"]
    var learnCarVC:MapViewController?
    var qusVC:QusViewController?
    var serveVC:ServiceViewController?
    var peiJiaVC:WithDrivingViewController?
    var leftItem:UIBarButtonItem?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpChildVC()
       self.setupTabBar()
      
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func setUpChildVC(){
    let sb = UIStoryboard(name: "Main", bundle: nil)
  
         learnCarVC = sb.instantiateViewController(withIdentifier: "learnVC") as? MapViewController
       serveVC = sb.instantiateViewController(withIdentifier: "serviceVC") as? ServiceViewController
         peiJiaVC = sb.instantiateViewController(withIdentifier: "peijiaVC") as? WithDrivingViewController
         qusVC = sb.instantiateViewController(withIdentifier: "qusVC") as? QusViewController
        let serviceNav = UINavigationController(rootViewController: serveVC!)
        let learnCarNav = UINavigationController(rootViewController: learnCarVC!)
        let qusNav = UINavigationController(rootViewController: qusVC!)
        let peijiaNav = UINavigationController(rootViewController: peiJiaVC!)
  
        
        
        
        
        
//        serviceNav.tabBarItem.title = "服務"
//        serviceNav.tabBarItem.image = UIImage(named: "ic_serve_tab")
//        serviceNav.tabBarItem.selectedImage =  UIImage(named: "icon_tab_right")
//        learnCarVC.tabBarItem.title = "學車"
//        learnCarVC.tabBarItem.image = UIImage(named: "ic_learn_tab")
//        learnCarNav.tabBarItem.selectedImage =  UIImage(named: "icon_tab_middle")
//        peiJiaVC?.tabBarItem.title = "陪駕"
//        peiJiaVC?.tabBarItem.image = UIImage(named: "ic_peijia_tab")
//        peiJiaVC?.tabBarItem.selectedImage =  UIImage(named: "icon_tab_middle")
//
//        qusNav.tabBarItem.title = "題庫"
//        learnCarNav.tabBarItem.image = UIImage(named: "ic_exercise_tab")
//        learnCarNav.tabBarItem.selectedImage =  UIImage(named: "icon_tab_left")
     
         //        qusVC.navigationItem.leftBarButtonItem = leftItem
//
//        serviceVC.navigationItem.leftBarButtonItem = leftItem
//
        
        
        
        self.viewControllers = [qusNav,learnCarNav,peijiaNav,serviceNav]
       
        
        
        
    }
    func setupTabBar() {
        let rect = self.tabBar.frame
        self.tabBar.removeFromSuperview()
         myView = UIView(frame: CGRect(x: 0, y: rect.origin.y - 20, width: SCREEN_WIDTH, height: rect.size.height + 20))
      
        self.view.addSubview(myView!)
        for i in 0 ..< 4{
            let button = CDTabBarButton()
        
            button.setTitle(titles[i], for: .normal)
            button.setImage(UIImage(named:unselImages[i]), for: .normal)
            button.setImage(UIImage(named:selImages[i]), for: .selected)
            let x = (CGFloat)(i)  * (myView?.frame.size.width)! / 4
            button.frame = CGRect(x: x, y: 0, width: (myView?.frame.size.width)! / 4, height: (myView?.frame.size.height)!)
            button.tag = i
         button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            button.imageEdgeInsets = UIEdgeInsetsMake(5, 13, 21,(button.titleLabel?.bounds.size.width)!)
            button.titleLabel?.textAlignment = .center
            button.titleEdgeInsets = UIEdgeInsetsMake(40, -1 * (button.titleLabel?.bounds.size.width)! - 45, 0, 0)
            button.setTitleColor(UIColor.black, for: .normal)
            myView?.addSubview(button)
            button.addTarget(self, action: #selector(CDTabbarViewController.clickBtn), for: .touchUpInside)
            if(i == 1){
                button.isSelected = true
                self.selectedBtn = button
               self.selectedIndex = 1
            }
        }
    }
    func clickBtn(sender:UIButton) {
        
        
        
        
       self.selectedBtn.isSelected = false;
        sender.isSelected = true
        self.selectedBtn = sender
        self.selectedIndex = sender.tag
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.myView?.isHidden = false
    }
    override func viewWillDisappear(_ animated: Bool) {
       
        super.viewWillDisappear(animated)
             self.myView?.isHidden = true
    }

  }
這樣即可

自定義present的方法 使看起來像push一樣

實現UIViewControllerTransitioningDelegate的方法

隨後實現這兩個方法

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let isFrom = true
        return HalfWaySpringAnimator(isFrom: isFrom)
    }
    
    
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let isFrom = false
        return HalfWaySpringAnimator(isFrom: isFrom)

    }
在要跳轉的過程之前

func clickHeader(){
    //到個人中心介面
        let learnCarSb = UIStoryboard(name: "Personal", bundle: nil)
        let PersonalNav = learnCarSb.instantiateViewController(withIdentifier: "PersonalNav") as! UINavigationController
      
        PersonalNav.transitioningDelegate = self
        //     self.isshow = false
        //  tabBar.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        
        //      myView.frame = CGRect(x: -100, y: 0, width:100, height: SCREEN_HEIGHT)
        //    self.modalPresentationStyle = .custom
        self.present(PersonalNav, animated: true, completion: nil)

    
    
    }
實現HalfWaySpringAnimator的方法

//
//  HalfWaySpringAnimator.swift
//  xiaobaxuecheProject
//
//  Created by hznucai on 2016/12/15.
//  Copyright © 2016年 hznucai. All rights reserved.
//

import UIKit

class HalfWaySpringAnimator: NSObject,UIViewControllerAnimatedTransitioning {
//設定動畫的持續時間
    var isFrom = false
     init(isFrom:Bool) {
        super.init()
        self.isFrom = isFrom
    }
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }
    //設定動畫的進行方式
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let fromVC = transitionContext.viewController(forKey: .from
        )
        let toVC = transitionContext.viewController(forKey: .to)
        let containerView = transitionContext.containerView
        let fromView = fromVC?.view
        let toView = toVC?.view
        if(isFrom) {
        toView?.frame = CGRect(x:SCREEN_WIDTH, y: 0, width:  SCREEN_WIDTH, height: SCREEN_HEIGHT)
        containerView.addSubview(toView!)
             let transitionDuration = self.transitionDuration(using: transitionContext)
     UIView.animate(withDuration: transitionDuration, animations: {
     toView?.frame = CGRect(x:0, y: 0, width:  SCREEN_WIDTH, height: SCREEN_HEIGHT)
     // 逐漸變為不透明
        toView?.frame = transitionContext.finalFrame(for: toVC!)
     }) { (finish:Bool) in
        let wasCancelled = transitionContext.transitionWasCancelled
        transitionContext.completeTransition(!wasCancelled)
        }
        }else{
            fromView?.frame = CGRect(x:0, y: 0, width:  SCREEN_WIDTH, height: SCREEN_HEIGHT)
            toView?.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
           containerView.addSubview(toView!)
            containerView.addSubview(fromView!)
           // containerView.bringSubview(toFront: fromView!)
            let transitionDuration = self.transitionDuration(using: transitionContext)
            UIView.animate(withDuration: transitionDuration, animations: {
                fromView?.frame = CGRect(x:SCREEN_WIDTH, y: 0, width:  SCREEN_WIDTH, height: SCREEN_HEIGHT)
                // 逐漸變為不透明
                toView?.frame = transitionContext.finalFrame(for: toVC!)
            }) { (finish:Bool) in
                let wasCancelled = transitionContext.transitionWasCancelled
                transitionContext.completeTransition(!wasCancelled)
            }

        }
    }
}

具體可以看這個部落格http://www.jianshu.com/p/38cd35968864 這樣即可

在子view中實現點選的方法

只需實現clickHeaderViewDeleage的方法 隨後在Button的click中將這個click傳遞過去即可

//
//  headerView.swift
//  xiaobaxuecheProject
//
//  Created by hznucai on 2016/12/18.
//  Copyright © 2016年 hznucai. All rights reserved.
//

import UIKit
protocol clickHeaderViewDelegate{
    func clickHeader()
}
class headerView: UIView {
    var headerDelegate:clickHeaderViewDelegate?
    override init(frame:CGRect){
        super.init(frame: frame)
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        imageView.image = UIImage(named:"ico_客服")
        let label = UILabel(frame: CGRect(x: 55, y: 0, width: 100, height: 30))
        label.text = "未設定"
        let subTitle = UILabel(frame: CGRect(x: 55, y: 35, width: 100, height: 30))
        subTitle.text = "17816869731"
        subTitle.font = UIFont.systemFont(ofSize: 10)
        self.addSubview(imageView)
        self.addSubview(label)
        self.addSubview(subTitle)
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerView.click)))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func click() {
       self.headerDelegate?.clickHeader()
    }
}
在主view中

    
    func clickHeader(){
    //到個人中心介面
        let learnCarSb = UIStoryboard(name: "Personal", bundle: nil)
        let PersonalNav = learnCarSb.instantiateViewController(withIdentifier: "PersonalNav") as! UINavigationController
      
        PersonalNav.transitioningDelegate = self
        //     self.isshow = false
        //  tabBar.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        
        //      myView.frame = CGRect(x: -100, y: 0, width:100, height: SCREEN_HEIGHT)
        //    self.modalPresentationStyle = .custom
        self.present(PersonalNav, animated: true, completion: nil)

    
    
    }
這樣即可
iOS很久沒打了 要開始複習了 為面試做好準備!!!