美文网首页
Swift 自定义Tabbar

Swift 自定义Tabbar

作者: Lucifer_rcy | 来源:发表于2017-11-15 12:46 被阅读0次

仿映客tabbar


映客tabbar

创建一个继承自UIView的自定义类 CustomTabBar.swift

import UIKit

//设置代理 用来监测点击的按钮
protocol CustomTabBarDelegate {
    func tabbar(_ tabbar: CustomTabBar, _ clickButton: itemType)
}

enum itemType : Int {
//中间拍摄按钮
    case launch = 10 
//左边推荐直播按钮
    case live = 100
//右边我的按钮
    case me
}

class CustomTabBar: UIView {
    
    var delegate : CustomTabBarDelegate?
    
//懒加载tabbar 背景图
    lazy var tabBarBGView : UIImageView = {
        let tabBarBGView = UIImageView(image: UIImage(named: "global_tab_bg"))
        return tabBarBGView
    }()
    
//懒加载按钮图片名
    lazy var dataList : [String] = {
        let dataList = ["tab_live","tab_me"]
        return dataList
    }()
    
//记录最后一次点击的按钮
    var lastItem : UIButton?
    
//懒加载拍摄按钮
    lazy var cameraBtn : UIButton = {
        let cameraBtn = UIButton(type: .custom)
        cameraBtn.setImage(#imageLiteral(resourceName: "tab_launch"), for: .normal)
        cameraBtn.tag = itemType.launch.rawValue
        cameraBtn.clipsToBounds = false
        cameraBtn.addTarget(self, action: #selector(CustomTabBar.clickItem(_:)), for: .touchUpInside)
        return cameraBtn
    }()

//重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        for i in 0..<dataList.count {
            let item = UIButton(type: .custom)
            item.setImage(UIImage(named: self.dataList[i]), for: .normal)
            item.setImage(UIImage(named: "\(self.dataList[i])_p"), for: .selected)
            //不自适应高亮图片
            item.adjustsImageWhenHighlighted = false
            item.addTarget(self, action: #selector(CustomTabBar.clickItem(_:)), for: .touchUpInside)
            item.tag = itemType.live.rawValue + i
            if i == 0 {
                item.isSelected = true
                lastItem = item
            }
            self.addSubview(item)
        }
        self.addSubview(cameraBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//设置显示位置尺寸
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = self.bounds.size.width / CGFloat(dataList.count)
        let height = self.bounds.size.height
        for i in self.subviews {
            if i.isKind(of: UIButton.self) {
                i.frame = CGRect(x: CGFloat(i.tag - itemType.live.rawValue) * width, y: 0, width: width, height: height)
            }
        }
        cameraBtn.sizeToFit()
        cameraBtn.center = CGPoint(x: self.center.x, y: self.bounds.size.height - 50)
    }
    
}

// MARK:- 设置UI

extension CustomTabBar {
    func setupUI(){
        self.addSubview(tabBarBGView)
    }
}

// MARK:- 点击事件

extension CustomTabBar {
    
    //解决点击范围超出父控件无法响应的bug
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        var view = super.hitTest(point, with: event)
        if view == nil {
            let newPoint = cameraBtn.convert(point, to: self)
            RLog(newPoint.x,newPoint.y)
            if cameraBtn.bounds.contains(newPoint) {
                view = cameraBtn
            }
        }
        return view
    }
    
    @objc fileprivate func clickItem(_ sender: UIButton){
        guard let type = itemType(rawValue: sender.tag) else {
            RLog("未获取到type")
            return
        }
        self.delegate?.tabbar(self, type)
        if sender.tag == itemType.launch.rawValue {
            //RLog("启动直播")
        } else{
            lastItem?.isSelected = false
            sender.isSelected = true
            lastItem = sender
            //添加动画效果
            UIView.animate(withDuration: 0.2, animations: {
                sender.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
            }) { (finished) in
                UIView.animate(withDuration: 0.2, animations: {
                    //恢复原始状态
                    sender.transform = .identity
                })
            }
        }
    }
}

创建继承自UITabBarController的自定义类CustomTabBarController

import UIKit

class CustomTabBarController: UITabBarController {
    
    lazy var tabbar : CustomTabBar = {
        let tabbar = CustomTabBar(frame: CGRect(x: 0, y: 0, width: screenW, height: tabbarH))
        tabbar.delegate = self
        return tabbar
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        configViewControllers()        
        // Do any additional setup after loading the view.
    }
}

// MARK:- 实现tabbar代理
extension CustomTabBarController : CustomTabBarDelegate {
    func tabbar(_ tabbar: CustomTabBar, _ clickButton: itemType) {
        if clickButton != itemType.launch {
            self.selectedIndex = clickButton.rawValue - itemType.live.rawValue
            return
        }
        //启动直播
        let launchVc = LaunchViewController()
        self.present(launchVc, animated: true, completion: nil)
    }
}

extension CustomTabBarController {
    // MARK:- 加载控制器 加载tabbar
    fileprivate func configViewControllers(){
        
        self.viewControllers = [navigationController(MainViewController()), navigationController(MeViewController())]
        self.tabBar.addSubview(tabbar)
    }
    
    fileprivate func navigationController(_ vc: BaseViewController) -> BaseNavigationController {
        let navVc = BaseNavigationController(rootViewController: vc)
        return navVc
    }

}

最后在AppDelegate.swift中的将自定义的tabbarController设为根控制器即可

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let tabBarController = CustomTabBarController()
        self.window?.rootViewController = tabBarController
        self.window?.makeKeyAndVisible()
        return true
    }

相关文章

网友评论

      本文标题:Swift 自定义Tabbar

      本文链接:https://www.haomeiwen.com/subject/vwecvxtx.html