对常见的顶部Tab页点击、滑动分页做了封装。它使用基于ViewController的container特性(而不是scrollview)来管理各个子页面,以支持无限分页
import UIKit
class TESTVC: BaseViewController {
private let tabTitleArray = ["111", "222", "333"]
private var slideView = DLCustomSlideView()
lazy var vcs: [UIViewController] = {
var arr = [UIViewController]()
for index in 0 ..< tabTitleArray.count {
switch index {
case 0:
let listVC = UIViewController()
arr.append(listVC)
break
case 1:
let listVC = UIViewController()
arr.append(listVC)
break
default:
let listVC = UIViewController()
arr.append(listVC)
break
}
}
return arr
}()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
private func setupUI() {
title = "TEST"
contentView.backgroundColor = COLOR_BACKGROUND
let tabbar = DLScrollTabbarView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 40))
contentView.addSubview(slideView)
slideView.snp.makeConstraints {
$0.left.equalToSuperview()
$0.right.equalToSuperview()
$0.top.bottom.equalToSuperview()
}
let cache = DLLRUCache(count: tabTitleArray.count)
tabbar.backgroundColor = COLOR_WHITE
tabbar.tabItemNormalColor = COLOR_MAJOR_FONT
tabbar.tabItemSelectedColor = COLOR_ORANGE
tabbar.trackColor = COLOR_ORANGE
tabbar.tabItemNormalFontSize = 14.0
var tabItemArray = [DLScrollTabbarItem]()
for title in tabTitleArray {
let item = DLScrollTabbarItem(title: title, width: SCREEN_WIDTH/CGFloat(tabTitleArray.count))
tabItemArray.append(item!)
}
tabbar.tabbarItems = tabItemArray
slideView.tabbar = tabbar
slideView.cache = cache
slideView.tabbarBottomSpacing = 0
slideView.baseViewController = self
slideView.delegate = self
slideView.setup()
slideView.selectedIndex = 0
}
}
extension TESTVC: DLCustomSlideViewDelegate {
func numberOfTabs(in sender: DLCustomSlideView?) -> Int {
return tabTitleArray.count
}
func dlCustomSlideView(_ sender: DLCustomSlideView?, controllerAt index: Int) -> UIViewController! {
return vcs[index]
}
}