Swift PageViewController封装

如果自己可以写的,最好自己写,不要对第三方封装过于依赖。如果他人写的感觉可以,可以借鉴学习,然后自己尝试模仿着去写,如果真的要用,还望学习一下,免得忽然出来个坑,然而自己的一脸茫然。

苹果官方提供了UIPageViewController用于分页控制ViewController。让我们使用来封装一个顶部有选择菜单PageViewController控制器。

【顶部有选择菜单】

import UIKit
enum PageBarViewType {
    case None
    case TextColor    //切换时改变颜色
    case FontSize     //切换时改变字体大小
}
//使用协议进行回掉
protocol PageBarViewDelegate {
    func changeSelected(index:Int);
}

class JDPageBarView: UIView {
    
    //MARK: - View
    lazy var scrollView:UIScrollView = {[unowned self] in
        let sv = UIScrollView()
        sv.showsHorizontalScrollIndicator = false
        return sv
     }()
    lazy var viewLine:UIView = {[unowned self] in
        let lineV = UIView()
        return lineV
    }()

    //MARK: - Data
    var type: PageBarViewType = .None {
        willSet {
            if newValue == .FontSize {
                self.viewLine.backgroundColor = .white
                self.backgroundColor = color_nav_red
            }
            else if newValue == .TextColor {
                self.viewLine.backgroundColor = color_nav_red
            }
        }
    }
    
    var countOfPage:Int = 4
    var titles:Array<String> = [] {
        didSet {
            reloadData()
        }
    }
    var delegate:PageBarViewDelegate?
    
    //MARK - func
    override init(frame: CGRect) {
        super.init(frame: frame)
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: self.frame.size.height)
        self.addSubview(scrollView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
    }
    
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
}

extension JDPageBarView {
    func reloadData() {
        
        if titles.count < countOfPage {
            countOfPage = titles.count
        }
        
        let w:CGFloat = frame.size.width / CGFloat.init(countOfPage)
        scrollView.contentSize = CGSize.init(width: w * CGFloat.init(titles.count), height: frame.size.height)
        viewLine.frame = CGRect(x: 0, y: frame.size.height - 2, width: w, height: 2)
        for i in 0..<self.titles.count {
            let btn = JDButton.init(title:titles[i])
            btn.frame = CGRect(x: CGFloat.init(i) * w, y: 0, width: w, height: frame.size.height)
            btn.addTarget(self, action: #selector(btnAction(_:)), for: .touchUpInside)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            btn.tag = 10 + i
            btn.isSelected = false
            scrollView.addSubview(btn)
            scrollView.addSubview(viewLine)

            if self.type == .FontSize {
                btn.backgroundColor = color_nav_red
                btn.setTitleColor(.white, for: .selected)
                btn.setTitleColor(.white, for: .normal)
                if  i == 0 {
                    btn.isSelected = true
                    btn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
                }
            }
            else if self.type == .TextColor {
                btn.backgroundColor = .white
                btn.setTitleColor(color_nav_red, for: .selected)
                btn.setTitleColor(.black, for: .normal)
                if  i == 0 {
                    btn.isSelected = true
                }
            }
        }
    }
    
    @objc func btnAction(_ btn:UIButton) {
        scrollChangeSelected(index: btn.tag)
    }
    
    func scrollChangeSelected(index:Int) {
        for i in 0..<self.titles.count {
            let v = scrollView.viewWithTag(10 + i)
            let btn = v as! UIButton
            if btn.tag != index {
                btn.isSelected = false
                btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            } else {
                btn.isSelected = true
                if self.type == .FontSize {
                    btn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
                }
                
            }
        }
        
        UIView.animate(withDuration: 0.3) {
            let w:CGFloat = self.frame.size.width / CGFloat.init(self.countOfPage)
            var frameOfLine = self.viewLine.frame
            frameOfLine.origin.x = w * CGFloat.init(index - 10)
            self.viewLine.frame = frameOfLine
            if frameOfLine.origin.x >= screen_width {
                self.scrollView.contentOffset = CGPoint(x: frameOfLine.origin.x - screen_width + w, y: 0)
            }
            if frameOfLine.origin.x <= w {
                self.scrollView.contentOffset = CGPoint(x:0, y: 0)
            }
        }
        
        if delegate != nil {
            delegate?.changeSelected(index:index - 10)
        }
    }
}

【UIPageViewController】

import UIKit

class JDBasePageViewController: UIPageViewController {
    lazy var pageBarView:JDPageBarView = {[unowned self] in
        let v = JDPageBarView.init(frame: CGRect(x: 0, y: 0, width: screen_width, height: button_Height))
        v.delegate = self
        return v
    }()
    
    var allViewControllers: Array<UIViewController>? {
        didSet {
            self.showIndex(index: 0)
        }
    }
    var allTitles:Array<String>? {
        didSet {
            self.pageBarView.titles = allTitles!
        }
    }
     
    convenience  init() {
        self.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        self.view.addSubview(pageBarView)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.dataSource = self
        self.delegate = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
extension JDBasePageViewController {
    func showPage(index: Int) {
        let btn = self.pageBarView.scrollView.viewWithTag(index + 10) as? JDButton
        self.pageBarView.btnAction(btn!)
    }
    
    func showIndex(index:Int) {
        if self.viewControllers?.count == 0 {
            setViewControllers([(allViewControllers?[index])! ], direction:.forward, animated: true, completion: nil)
        } else {
            let page = allViewControllers?.index(of: self.viewControllers![0])
            if page! < index {
                setViewControllers([(allViewControllers?[index])! ], direction:.forward, animated: true, completion: nil)
            }
            else if page! > index {
                setViewControllers([(allViewControllers?[index])! ], direction:.reverse, animated: true, completion: nil)
            }
        }
    }
}

extension JDBasePageViewController:UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = allViewControllers?.index(of: viewController) else {
            return nil
        }
        
        let previousIndex = viewControllerIndex - 1
        
        guard previousIndex >= 0 else {
            return nil
        }
        
        guard (allViewControllers?.count)! > previousIndex else {
            return nil
        }
        return allViewControllers?[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        guard let viewControllerIndex = allViewControllers?.index(of: viewController) else {
            return nil
        }
        
        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = allViewControllers?.count
        
        guard orderedViewControllersCount != nextIndex else {
            return nil
        }
        
        guard orderedViewControllersCount! > nextIndex else {
            return nil
        }
        return allViewControllers?[nextIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        let page = allViewControllers?.index(of: self.viewControllers![0])
        pageBarView.scrollChangeSelected(index: page! + 10)
    }
}

extension JDBasePageViewController:PageBarViewDelegate {
    func changeSelected(index: Int) {
        showIndex(index: index)
    }
}

【使用简单实例】

class JDProductPageViewController: JDBasePageViewController {
    lazy var VCs:Array<ProductViewController> = {[unowned self] in
        let allVC = JDProductViewController()
        allVC.state = "-1"
        
        let onVC = JDProductViewController()
        onVC.state = "2"
        
        let hotVC = JDProductViewController()
        hotVC.state = "1"
        
        let overVC = JDProductViewController()
        overVC.state = "3"
        
        let vcs = [allVC, onVC, hotVC, overVC]
        return vcs
    }()

    override func viewDidLoad() {
        super.viewDidLoad()  
      
        self.pageBarView.type = .FontSize
        self.allViewControllers = VCs
        self.allTitles = ["全部","在售中","预热中","已售罄"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容