import UIKit
class ViewController: UIViewController {
let kMargin: CGFloat = 10.0
/// 圆角
let kCornerRadius: CGFloat = 5.0
/// 线宽
let klineWidth: CGFloat = 1.0
/// 首页顶部标签指示条的高度
let kIndicatorViewH: CGFloat = 2.0
/// 新特性界面图片数量
let kNewFeatureCount = 4
// 顶部标题的高度
let kTitlesViewH: CGFloat = 35
/// 顶部标题的y
let kTitlesViewY: CGFloat = 64
var selectedBtn = UIButton()
var indicatorView = UIView()
var titlesView = UIView()
var contentView = UIScrollView()
let titleArr = ["消息","好友","群组","动态"]
let colorArr = [UIColor.blue,UIColor.brown,UIColor.cyan,UIColor.yellow]
override func viewDidLoad() {
super.viewDidLoad()
for i in 0 ..< titleArr.count {
let vc = UIViewController()
vc.view.backgroundColor = colorArr[i]
self.addChildViewController(vc)
}
self.setupTitleView()
self.setupContentView()
}
func setupTitleView(){
let bgView = UIView()
bgView.frame = CGRect(x: 0, y: kTitlesViewY, width: SCREENW, height: kTitlesViewH)
view.addSubview(bgView)
//标签
let titlesView = UIView()
titlesView.backgroundColor = DTGlobalColor()
titlesView.frame = CGRect(x: 0, y: 0, width: SCREENW, height: kTitlesViewH)
bgView.addSubview(titlesView)
//底部红色指示器
let indicatorView = UIView()
indicatorView.backgroundColor = DTGlobalRedColor()
indicatorView.height = kIndicatorViewH
indicatorView.frame.origin.y = kTitlesViewH - kIndicatorViewH
self.indicatorView = indicatorView
//内部子标签
let count = childViewControllers.count
let width = titlesView.width / CGFloat(count)
let height = titlesView.height
for index in 0..<count{
let btn = UIButton()
btn.height = height
btn.width = width
btn.frame.origin.x = CGFloat(index) * width
btn.tag = index
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
btn.setTitle(titleArr[index], for: UIControlState())
btn.setTitleColor(UIColor.gray, for: UIControlState())
btn.setTitleColor(DTGlobalRedColor(), for: .disabled)
btn.addTarget(self, action: #selector(titlesClick(_:)), for: .touchUpInside)
titlesView.addSubview(btn)
//默认点击第一个按钮
if index == 0 {
btn.isEnabled = false
selectedBtn = btn
//让按钮内部的Label根据文字来计算内容, 否则得不到titleLabel的width
btn.titleLabel?.sizeToFit()
indicatorView.width = btn.titleLabel!.width
indicatorView.center.x = btn.center.x
}
}
titlesView.addSubview(indicatorView)
self.titlesView = titlesView
}
func setupContentView(){
automaticallyAdjustsScrollViewInsets = false
let contentView = UIScrollView()
contentView.frame = view.bounds
contentView.y = kTitlesViewY + kTitlesViewH
contentView.delegate = self
contentView.contentSize = CGSize(width: contentView.width * CGFloat(childViewControllers.count), height: contentView.height)
view.insertSubview(contentView, at: 0)
contentView.isPagingEnabled = true
self.contentView = contentView
scrollViewDidEndScrollingAnimation(contentView)
}
func titlesClick(_ btn:UIButton) {
selectedBtn.isEnabled = true
btn.isEnabled = false
selectedBtn = btn
UIView.animate(withDuration: 0.25, animations: {
self.indicatorView.width = self.selectedBtn.titleLabel!.width
self.indicatorView.center.x = self.selectedBtn.center.x
})
contentView.setContentOffset(CGPoint(x: CGFloat(btn.tag) * contentView.width, y: 0), animated: true)
}
func DTColor(_ r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
func DTGlobalColor() -> UIColor {
return DTColor(240, g: 240, b: 240, a: 1)
}
func DTGlobalRedColor() -> UIColor {
return DTColor(245, g: 80, b: 83, a: 1.0)
}
}
extension ViewController:UIScrollViewDelegate {
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
let index = Int(scrollView.contentOffset.x / scrollView.width)
let vc = childViewControllers[index]
vc.view.frame.origin.x = scrollView.contentOffset.x
//设置控制器的Y为0,默认为20
vc.view.frame.origin.y = 0
scrollView.addSubview(vc.view)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollViewDidEndScrollingAnimation(scrollView)
// 当前索引
let index = Int(scrollView.contentOffset.x / scrollView.width)
// 点击 Button
let button = titlesView.subviews[index] as! UIButton
titlesClick(button)
}
}
ScrollView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 嵌套的GrdiView和ListView显示不全,直接重写onMessure()方法解决 ScrollView 弹...
- 在一些初学者中很多的同学就会遇到这样的问题像标题所说的那样的问题。 什么废话我就不说了 (解释上面的代码)写个自定...
- cg```swiftclass ViewController: UIViewController, UIScrol...
- 前言 本篇我们的要学习的内容如下: 组件ScrollView 使用ScrollView实现轮播图 一、组件Scro...