公共类(框架)
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)
}
}