小巴学车有关项目的总结

上学期期末的大作业之一,做了一个小巴学车,现在想起来了,总结一下,顺便复习下iOS,新学期,要好好读书了。

自定义tabBarController 只需要写一个类继承自uitabbarcontroller 随后将几个子viewController拿到,再实现两个方法setupChildVC 用来设定子viewController

还有setupTabBar来实现相关界面的实现 这样既可

    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]
       
        
        
        
    }
setUpTabBar

 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的方法 用于切换不同的viewController 这样就可进行切换
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
    }

  }
这样既可

在一部分view中定义click事件 随后在主view中实现click事件可以用代理进行

首先在头像headerView的按钮添加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中,实现clickHeaderView事件

 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)

    
    
    }

这样既可

自定义present 使看起来像pushViewController一样的方法

首先继承UIVieControllerTransitionDelegate的代理

随后实现两个代理

 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)

    }
实现HalfWaySpringAnimatior类的方法

 

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

今天大致就写到这里 iOS隔得时间有点久了 要开始复习iOS了,为面试做好准备!!!



你可能感兴趣的:(iOS方面)