項目框架

公共類(框架)

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //創建window
        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        
        //創建根視圖控制器
        self.creatViewControllers()
        
        //定制狀態欄
        self.statusBarSetting()
        
        return true
    }
    
    //MARK: - 定制狀態欄
    func statusBarSetting(){
        
        UIApplication.sharedApplication().statusBarStyle = .LightContent
    }
    
    //MARK: - 創建主框架視圖控制器
    func creatViewControllers() {
        
        //標簽欄控制器
        let tabBarC = YTTabBarController()
        
        //1.首頁
        let nav1 = YTNavigationController(rootViewController: MainPageViewController())
        tabBarC.addController(nav1, title: "首頁", imageName: "label_bar_movie_normal", selectImageName: "agnes-overjoyed-icon")
        
        //2.影評
        let nav2 = YTNavigationController(rootViewController: FilmCommentViewController())
        tabBarC.addController(nav2, title: "影評", imageName: "label_bar_film_critic_normal", selectImageName: "evil-minion-icon-2")
        
        //3.影單
        let nav3 = YTNavigationController(rootViewController: FilmListViewController())
        tabBarC.addController(nav3, title: "影單", imageName: "label_bar_film_list_normal", selectImageName: "margo-dispicable-me-2-icon")
        
        //4.我
        let nav4 = YTNavigationController(rootViewController: MeViewController())
        tabBarC.addController(nav4, title: "我", imageName: "label_bar_my_normal", selectImageName: "gru-icon-2")
        
        //設置tabBar上的文字顏色
        tabBarC.yt_tabBar.titleColor = UIColor.blackColor()
        
        self.window?.rootViewController = tabBarC
        
    }


///封裝四個模塊的主頁上的導航條上共有的左右item,以及其功能
class BasicViewController: YTViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBarItemSetting()
    }

    

}

//MARK: - 界面相關
extension BasicViewController{
    
    override func navigationBarItemSetting() {
        
        //1.添加左右兩邊的item
        //左
        let leftItem = UIBarButtonItem.init(image:UIImage.init(named: "lineposter_lines_adding"), style: .Done, target: self, action: "menuItemAction:")
        self.navigationItem.leftBarButtonItem = leftItem
        //右
        let rightItem = UIBarButtonItem.init(image: UIImage.init(named: "movie_all_search_light"), style: .Done, target: self, action: "searchItemAction:")
        self.navigationItem.rightBarButtonItem = rightItem
        
    }
}

//MARK: - 按鈕點擊
extension BasicViewController{
    
    //搜索按鈕被點擊
    func searchItemAction(item:UIBarButtonItem) {
        
        //跳轉到搜索界面
        let searchC = SearchViewController()
        searchC.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(searchC, animated: true)
        
    }
    
    //導航條上的菜單按鈕被點擊
    func menuItemAction(item:UIBarButtonItem){
        
        
    }
}

class MainPageViewController: BasicViewController {
    //MARK: - 屬性
    //1.滾動視圖
    let scrollView = UIScrollView()
    //2.分段選擇器
    var segment:YTSegmentControl!

    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationBarItemSetting()
        self.creatUI()
    }

}

//MARK: - 界面相關
extension MainPageViewController{
    
    override func creatUI() {
        super.creatUI()
        //1.創建滾動視圖
        //設置frame
        self.scrollView.frame = self.view.bounds
        //設置內容大小
        self.scrollView.contentSize = CGSizeMake(Screen_W*2,scrollView.frame.size.height-64-49)
        //設置分頁
        self.scrollView.pagingEnabled = true
        //設置代理
        self.scrollView.delegate = self
        //隱藏滾動條
        self.scrollView.showsHorizontalScrollIndicator = false
        //顯示在界面上
        self.view.addSubview(self.scrollView)
        
        //2.添加熱映界面
        let hotC = HotShowViewController()
        self.addChildViewController(hotC)
        hotC.tableView.frame = CGRectMake(0, 0,self.scrollView.frame.size.width, self.scrollView.frame.size.height)
        self.scrollView.addSubview(hotC.tableView)
        
        //3.添加預告界面
        let reailerC = TrailerTableViewController()
        self.addChildViewController(reailerC)
        reailerC.tableView.frame = CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)
        self.scrollView.addSubview(reailerC.tableView)
        
        
    }

    override func navigationBarItemSetting() {
        super.navigationBarItemSetting()
        
        //1.中間的item
        //創建分段選擇器對象
        self.segment = YTSegmentControl.init(items: ["熱映","預告"])
        //設置frame
        self.segment.frame = CGRectMake(0, 0, 100, 44)
        //添加到導航條上
        self.navigationItem.titleView = self.segment
        //設置正常和選中的文字顏色
        self.segment.titleSelectedColor = UIColor.whiteColor()
        self.segment.titleColor = UIColor.whiteColor()
        //設置正常狀態和選中的字體
        self.segment.selectedFont = UIFont.boldSystemFontOfSize(15)
        self.segment.normalFont = UIFont.systemFontOfSize(12)
        //設置滑塊的顏色
        self.segment.sliderColor = UIColor.yellowColor()
        //添加事件
        self.segment.addTarget(self, action: "segmentAction:")
        
        
    }
}

//MARK: - scrollView Delegate
extension MainPageViewController:UIScrollViewDelegate{

    //停止滾動
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        //切換分段選擇器
        if scrollView.contentOffset.x == 0 {
            self.segment.selectedSegmentIndex = 0
            return
        }
        self.segment.selectedSegmentIndex = 1
    }
}

//MARK: - 按鈕點擊
extension MainPageViewController{
    
    func segmentAction(segment:YTSegmentControl){
        
        if segment.selectedSegmentIndex == 0 {
            
            print("熱映被選中")
            self.scrollView.contentOffset = CGPointMake(0, 0)
        }else{
        
            print("預告被選中")
            self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.width, 0)
        }
    }
}

YTTabBarController

class YTTabBar: UIView {

    
    //MARK: - 屬性
    ///1.當前選中的下標
    var selectedIndex = 1
    
    ///2.聲明閉包用來傳值
    var changeSelectedIndex:((Int)->Void)? = nil
    
    ///3.設置按鈕的文字顏色
    var titleColor = UIColor.blackColor()
    
}

//MARK: - 添加按鈕
extension YTTabBar{

    ///添加按鈕
    func addButtonWithItem(item:UITabBarItem) {
        
        //創建按鈕
        let btn = YTTabBarButton(tabBarItem: item)
        //添加點擊事件
        btn.addTarget(self, action: "btnAction:")
        
        //設置按鈕的tag值
        btn.tag = 100+self.subviews.count
        
//        //判斷是否是需要選中按鈕
//        if self.selectedIndex == self.subviews.count{
//            
//            btn.isSelected = true
//        }
        
        //添加到界面上
        self.addSubview(btn)
    }
}

//MARK: - 按鈕點擊
extension YTTabBar{

    func btnAction(btn:YTTabBarButton){
        
        //將原來處于選中狀態的按鈕變成非選中狀態
        let selectBtn = self.viewWithTag(100+self.selectedIndex) as! YTTabBarButton
        selectBtn.isSelected = false
        
        //將當前按下的按鈕變成選中狀態
        btn.isSelected = true
        //更新選中下標
        self.selectedIndex = btn.tag - 100
        
        //通知tabBarController切換視圖控制器
        self.changeSelectedIndex!(self.selectedIndex)
        
    }
}

//MARK: - 計算子視圖的frame
extension YTTabBar{

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //通用
        let btnW = self.frame.size.width / CGFloat(self.subviews.count)
        let btnH = self.frame.size.height
        let btnY:CGFloat = 0
        
        //遍歷拿到所有的按鈕
        for (i,item) in self.subviews.enumerate() {
            
            let btn = item as! YTTabBarButton
            
            let btnX = CGFloat(i) * btnW
            //1.設置frame
            item.frame = CGRectMake(btnX, btnY, btnW, btnH)
            
            //2.設置默認選中的按鈕
            if i == self.selectedIndex {
                
                btn.isSelected = true
            }
            //3.設置按鈕顏色
            btn.titleLabel.textColor = self.titleColor
            
        }
        
        
        
    }
}


import UIKit

//自定義控件:
//1.聲明所有的子視圖的屬性
//2.在構造方法中將子視圖添加到界面上
//3.計算子視圖的frame


//1.小圖
//2.文字
//3.大圖
class YTTabBarButton: UIView {

    //MARK: - 屬性
    //1.小圖
    let smallImageView = UIImageView()
    //2.文字
    let titleLabel = UILabel()
    //3.大圖
    let bigImageView = UIImageView()
    
    ///4.按鈕的狀態
    var isSelected = false{
    
        didSet{
            
            if isSelected == true {
                self.bigImageView.hidden = false
                self.smallImageView.hidden = true
                self.titleLabel.hidden = true
                
            }else{
                self.bigImageView.hidden = true
                self.smallImageView.hidden = false
                self.titleLabel.hidden = false
            }
        }
    }
    
    //5.保存添加事件相關屬性
    var target: AnyObject? = nil
    var action: Selector? = nil
    

    //MARK: - 構造方法
    init(tabBarItem:UITabBarItem){
        super.init(frame: CGRectZero)
        //1.小圖
        self.addSubview(self.smallImageView)
        self.smallImageView.image = tabBarItem.image
        self.smallImageView.contentMode = .Center
        //2.文字
        self.addSubview(self.titleLabel)
        self.titleLabel.font = UIFont.systemFontOfSize(11)
        self.titleLabel.text = tabBarItem.title
        self.titleLabel.textAlignment = .Center
        //3.大圖
        self.addSubview(self.bigImageView)
        self.bigImageView.image = tabBarItem.selectedImage
        self.bigImageView.contentMode = .Center
        self.bigImageView.hidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: - 添加點擊事件
extension YTTabBarButton{

    ///添加事件
    func addTarget(target:AnyObject,action:Selector){
        
        self.target = target
        self.action = action
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        if self.target == nil {
            return
        }
        
         if (self.target!.respondsToSelector(self.action!) == true){
        
            self.target!.performSelector(self.action!, withObject: self)
        }else{
        
            print("按鈕點擊方法沒有實現")
        }
        
    }
    
    
}

//MARK: - 計算frame
extension YTTabBarButton{

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //通用
        let btnW = self.frame.size.width
        let btnH = self.frame.size.height
        let imageProportion = CGFloat(4)/5
        let beyondH:CGFloat = 25
        //1.小圖
        let smallX:CGFloat = 0
        let smallY:CGFloat = 0
        let smallW = btnW
        let smallH = btnH * imageProportion - 2
        self.smallImageView.frame = CGRectMake(smallX, smallY, smallW, smallH)
        //2.文字
        let titleX: CGFloat = 0
        let titleY: CGFloat = smallH
        let titleW = btnW
        let titleH = btnH * (1 - imageProportion)
        self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH)
        //3.大圖
        let bigX: CGFloat = 0
        let bigY = -beyondH
        let bigW = btnW
        let bigH = btnH + beyondH
        self.bigImageView.frame = CGRectMake(bigX, bigY, bigW, bigH)
        
        
        //判斷當前按鈕是否處于選中狀態
        if self.isSelected {
            
            self.bigImageView.hidden = false
        }
        
        //設置文字顏色
        self.titleLabel.textColor = UIColor.blackColor()
        
    }
}

class YTTabBarController: UITabBarController {
    
    //MARK: - 屬性
    lazy var yt_tabBar:YTTabBar = {
    
        //自己定義的tabBar的大小和系統自帶的tabBar的大小一樣
        let tempTabBar = YTTabBar(frame:self.tabBar.bounds)
        
        //點擊tabBar上的按鈕去切換視圖控制器
        tempTabBar.changeSelectedIndex = {(index)in
        
            //切換到指定的視圖控制器
            self.selectedIndex = index
        }
        
        //將自定義的tabBar貼到系統的tabBar上
        self.tabBar.addSubview(tempTabBar)
        
        
        return tempTabBar
    }()

    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
       

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //設置默認選中的按鈕
        self.yt_tabBar.selectedIndex = self.selectedIndex
        
        //改變文字顏色
        self.yt_tabBar.titleColor = UIColor.lightGrayColor()
        
        //移除tabBar上自動添加的所有的子視圖
        for item in self.tabBar.subviews {
            //判斷子視圖的類型是否是YTTabBar
            //isKindOfClass判斷指定的對象是否是指定的類型
            if item.isKindOfClass(YTTabBar.self) {
                continue
            }
            
            //將不是YTTabBar的從系統的tabBar上移除
            item.removeFromSuperview()
            
        }
        
    }
    
    
}

//MARK: - 添加子視圖控制器
extension YTTabBarController{
    
    ///添加子視圖控制器
    func addController(controller:UIViewController,title:String,imageName:String,selectImageName:String){
        
        //設置視圖控制器對一個的tabBarItem
        controller.tabBarItem.title = title
        controller.tabBarItem.image = UIImage.init(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        controller.tabBarItem.selectedImage = UIImage.init(named: selectImageName)?.imageWithRenderingMode(.AlwaysOriginal)
        //將視圖控制器添加到tabBarController中
        self.addChildViewController(controller)
        
        //在yt_tabBar上創建對應按鈕
        self.yt_tabBar.addButtonWithItem(controller.tabBarItem)
        
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,563評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,694評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,672評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,965評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,690評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,019評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,013評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,188評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,718評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,438評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,667評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,149評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,845評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,252評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,590評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,384評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容