1. 程式人生 > >Swift 分頁控制器外加輪播圖簡便實用

Swift 分頁控制器外加輪播圖簡便實用

分頁控制器

1.分頁控制器

使用PagingMenuController

ViewController.m ViewController1,ViewController2,ViewController3,ViewController4,ViewController5是分頁 需要分幾頁建立幾個控制器 這裡需要五個所以建立了五個控制器


import UIKit
import PagingMenuController

class ViewController: UIViewController {

    override func viewDidLoad() {
        super
.viewDidLoad() //標題檢視 let tView = UIImageView.init(frame: CGRect(x: (SCR_W - 100) / 2, y: 0, width: 100, height: 50)) tView.image = UIImage.init(named: "1") self.navigationItem.titleView = tView //導航欄右按鈕 self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title:
"?", style: .plain, target: self, action: #selector(click)) let options = pagingMenuOptions() let pagingMenuController = PagingMenuController(options: options) //分頁選單控制器尺寸設定 pagingMenuController.view.frame.origin.y += 64 pagingMenuController.
view.frame.size.height -= 64 addChildViewController(pagingMenuController) view.addSubview(pagingMenuController.view) } private struct pagingMenuOptions:PagingMenuControllerCustomizable { private var v1 = ViewController1() private var v2 = ViewController2() private var v3 = ViewController3() private var v4 = ViewController4() private var v5 = ViewController5() fileprivate var componentType: ComponentType{ return .all(menuOptions: MenuOptions(), pagingControllers: pagingControllers) } fileprivate var pagingControllers : [UIViewController]{ return [v1,v2,v3,v4,v5] } fileprivate struct MenuOptions:MenuViewCustomizable { var displayMode: MenuDisplayMode{ return .segmentedControl } var itemsOptions: [MenuItemViewCustomizable] { return [MenuItem1(),MenuItem2(),MenuItem3(),MenuItem4(),MenuItem5()] } } //第1個選單項 fileprivate struct MenuItem1: MenuItemViewCustomizable { //自定義選單項名稱 var displayMode: MenuItemDisplayMode { return .text(title: MenuItemText(text: "推薦")) } } //第2個選單項 fileprivate struct MenuItem2: MenuItemViewCustomizable { //自定義選單項名稱 var displayMode: MenuItemDisplayMode { return .text(title: MenuItemText(text: "快訊")) } } fileprivate struct MenuItem3: MenuItemViewCustomizable { //自定義選單項名稱 var displayMode: MenuItemDisplayMode { return .text(title: MenuItemText(text: "深度")) } } fileprivate struct MenuItem4: MenuItemViewCustomizable { //自定義選單項名稱 var displayMode: MenuItemDisplayMode { return .text(title: MenuItemText(text: "時點對話")) } } fileprivate struct MenuItem5: MenuItemViewCustomizable { //自定義選單項名稱 var displayMode: MenuItemDisplayMode { return .text(title: MenuItemText(text: "行情分析")) } } } @objc func click() -> Void { let t = tViewController() self.navigationController?.pushViewController(t, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

2.輪播圖

import UIKit
import Alamofire

class ViewController1: UIViewController,SliderGalleryControllerDelegate,UITableViewDelegate,UITableViewDataSource {
    
    
    
    var table : UITableView?
    
    
    func galleryDataSource() -> [String] {
        return imgArr
    }
    
    func galleryScrollerViewSize() -> CGSize {
        return CGSize(width: SCR_W, height: 200)
    }
    

    var sdlier = SliderGalleryController()
    
    var imgArr = ["http://img4q.duitang.com/uploads/item/201503/18/20150318230437_Pxnk3.jpeg",
                  "http://img4.duitang.com/uploads/item/201501/31/20150131234424_WRJGa.jpeg",
                  "http://img5.duitang.com/uploads/item/201502/11/20150211095858_nmRV8.jpeg"]
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white
        
        sdlier = SliderGalleryController()
        sdlier.delegate = self
        sdlier.view.frame = CGRect(x: 0, y: 0, width: SCR_W, height: 200)
        sdlier.view.backgroundColor = UIColor.white
        addChildViewController(sdlier)
        view.addSubview(sdlier.view)
        
        let parameters1 = ["引數1":"引數值1","引數2":"引數值2"]
        Alamofire.request("圖片介面", parameters: parameters1).responseJSON { (response) in
            
            print(response.result.value)
//            if let JSON = response.result.value
        }
        
        table = UITableView.init(frame: CGRect(x: 0, y: 240, width: SCR_W, height: SCR_H - 240), style: .grouped)
        table?.register(MainTableViewCell.self, forCellReuseIdentifier: "cell")
        table?.contentInset = UIEdgeInsetsMake(-30, 0, 0, 0)
        table?.delegate = self
        table?.dataSource = self
        self.view.addSubview(table!)
        
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }else if section == 1{
            return 3
        }else if section == 2{
            return 1
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainTableViewCell
        if cell == nil {
            cell = MainTableViewCell.init(style: .default, reuseIdentifier: "cell")
        }
        if indexPath.section == 0 {
            cell.textLabel?.text = "創業黑馬董事長:區塊鏈是機會,但不是每個人的機會"
            cell.imageView?.image = UIImage.init(named: "2")
        }else if indexPath.section == 1{
            cell.title?.text = "創業黑馬董事長:區塊鏈是機會,但不是每個人的機會"
            cell.name?.text = "嶽恆月"
            cell.time?.text = "6月11日"
            cell.imag?.image = UIImage.init(named: "2")
        }else if indexPath.section == 2{
            let imgV = UIImageView.init(frame: CGRect(x: 0, y: 0, width: SCR_W, height: 140))
            imgV.image = UIImage.init(named: "3")
            cell.addSubview(imgV)
        }else{
            cell.title?.text = "創業黑馬董事長:區塊鏈是機會,但不是每個人的機會"
            cell.name?.text = "嶽恆月"
            cell.time?.text = "6月11日"
            cell.imag?.image = UIImage.init(named: "2")
        }
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 2{
            return 140
        }
        return 70
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}