美文网首页
ScrollView

ScrollView

作者: 鱼得琴 | 来源:发表于2017-02-13 10:25 被阅读0次
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

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