EZSwipeController 0.6.1

EZSwipeController 0.6.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2016年10月
SPM支持 SPM

goktugyil 维护。



  • 作者:
  • goktugyil

EZSwipeController


易于使用的 UIPageViewController,可以创建类似于 Snapchat/Tinder/iOS 主页的视图导航。

Demo

手动安装 (~10 秒钟)

  1. 下载并将‘EZSwipeController.swift’拖放到您的项目中。
  2. 恭喜!

设置

与 Storyboard 一起使用

您也可以像这样在您的 UIViewcontrollers(例如)中通过推送或显示使用 EZSwipeController

presentViewController(EZSwipeController(), animated: true, completion: nil)

不使用 Storyboard 使用

如果您想将 EZSwipe 作为 root viewcontroller(您的应用程序的起点)使用

请转到 Targets -> 您的目标 -> General -> Main Interface -> 删除它

将此内容添加到您的 AppDelegate 中

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.rootViewController = MySwipeVC()
    window!.makeKeyAndVisible()
    return true
}

使用

创建 EZSwipeController 的子类

import UIKit
// import EZSwipeController // if using CocoaPods
class MySwipeVC: EZSwipeController {
    override func setupView() {
        datasource = self
    }
}

extension MySwipeVC: EZSwipeControllerDataSource {
    func viewControllerData() -> [UIViewController] {
        let redVC = UIViewController()
        redVC.view.backgroundColor = UIColor.redColor()

        let blueVC = UIViewController()
        blueVC.view.backgroundColor = UIColor.blueColor()

        let greenVC = UIViewController()
        greenVC.view.backgroundColor = UIColor.greenColor()

        return [redVC, blueVC, greenVC]
    }
}

您应该有类似以下的内容

更改背景颜色

class MySwipeVC: EZSwipeController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.yellowColor()
    }
}

为页面提供标题

extension MySwipeVC: EZSwipeControllerDataSource {   
    func titlesForPages() -> [String] {
        return ["red", "blue", "green"]
    }
}

起始页面索引

extension MySwipeVC: EZSwipeControllerDataSource {
    func indexOfStartingPage() -> Int {
        return 2 // EZSwipeController starts from 2nd, green page
    }
}

页面索引更改时

extension MySwipeVC: EZSwipeControllerDataSource {
    func changedToPageIndex(index: Int) {
        // You can do anything from here, for now we'll just print the new index
        print(index)
    }
}

自定义导航栏

设置 naviagationBarDataForPageIndex 会覆盖 titlesForPages 中的效果。

extension MySwipeVC: EZSwipeControllerDataSource {
    func navigationBarDataForPageIndex(index: Int) -> UINavigationBar {
        var title = ""
        if index == 0 {
            title = "Charmander"
        } else if index == 1 {
            title = "Squirtle"
        } else if index == 2 {
            title = "Bulbasaur"
        }

        let navigationBar = UINavigationBar()
        navigationBar.barStyle = UIBarStyle.Default
//        navigationBar.barTintColor = QorumColors.WhiteLight
        print(navigationBar.barTintColor)
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()]

        let navigationItem = UINavigationItem(title: title)
        navigationItem.hidesBackButton = true

        if index == 0 {
            let rightButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "a")
            rightButtonItem.tintColor = UIColor.blackColor()

            navigationItem.leftBarButtonItem = nil
            navigationItem.rightBarButtonItem = rightButtonItem
        } else if index == 1 {
            let rightButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: self, action: "a")
            rightButtonItem.tintColor = UIColor.blackColor()

            let leftButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, target: self, action: "a")
            leftButtonItem.tintColor = UIColor.blackColor()

            navigationItem.leftBarButtonItem = leftButtonItem
            navigationItem.rightBarButtonItem = rightButtonItem
        } else if index == 2 {            
            let leftButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "a")
            leftButtonItem.tintColor = UIColor.blackColor()

            navigationItem.leftBarButtonItem = leftButtonItem
            navigationItem.rightBarButtonItem = nil
        } 
        navigationBar.pushNavigationItem(navigationItem, animated: false)
        return navigationBar
    }
}

您不需要设置按钮的操作,EZSwipeController 会自动覆盖它们并使它们生效。

向导航栏添加图片

extension MySwipeVC: EZSwipeControllerDataSource {
    func navigationBarDataForPageIndex(index: Int) -> UINavigationBar {
        var title = ""
        if index == 0 {
            title = "Charmander"
        } else if index == 1 {
            title = "Squirtle"
        } else if index == 2 {
            title = "Bulbasaur"
        }

        let navigationBar = UINavigationBar()
        navigationBar.barStyle = UIBarStyle.Default
        navigationBar.barTintColor = UIColor.purpleColor()
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()]

        let navigationItem = UINavigationItem(title: title)
        navigationItem.hidesBackButton = true

        if index == 0 {
            var sImage = UIImage(named: "squir")!
            sImage = scaleTo(image: sImage, w: 22, h: 22)
            let rightButtonItem = UIBarButtonItem(image: sImage, style: UIBarButtonItemStyle.Plain, target: self, action: "a")
            rightButtonItem.tintColor = UIColor.blueColor()

            navigationItem.leftBarButtonItem = nil
            navigationItem.rightBarButtonItem = rightButtonItem
        } else if index == 1 {
            var cImage = UIImage(named: "char")!
            cImage = scaleTo(image: cImage, w: 22, h: 22)
            let leftButtonItem = UIBarButtonItem(image: cImage, style: UIBarButtonItemStyle.Plain, target: self, action: "a")
            leftButtonItem.tintColor = UIColor.redColor()

            var bImage = UIImage(named: "bulb")!
            bImage = scaleTo(image: bImage, w: 22, h: 22)
            let rightButtonItem = UIBarButtonItem(image: bImage, style: UIBarButtonItemStyle.Plain, target: self, action: "a")
            rightButtonItem.tintColor = UIColor.greenColor()

            navigationItem.leftBarButtonItem = leftButtonItem
            navigationItem.rightBarButtonItem = rightButtonItem
        } else if index == 2 {
            var sImage = UIImage(named: "squir")!
            sImage = scaleTo(image: sImage, w: 22, h: 22)
            let leftButtonItem = UIBarButtonItem(image: sImage, style: UIBarButtonItemStyle.Plain, target: self, action: "a")
            leftButtonItem.tintColor = UIColor.blueColor()

            navigationItem.leftBarButtonItem = leftButtonItem
            navigationItem.rightBarButtonItem = nil
        }
        navigationBar.pushNavigationItem(navigationItem, animated: false)
        return navigationBar
    }
}

private func scaleTo(image image: UIImage, w: CGFloat, h: CGFloat) -> UIImage {
    let newSize = CGSize(width: w, height: h)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

自定义按钮设置

有时您可能想为按钮添加自己的操作,在这种情况下,您应该禁用默认按钮行为

extension MySwipeVC: EZSwipeControllerDataSource {    
    func disableSwipingForLeftButtonAtPageIndex(index: Int) -> Bool {
        if index == 1 {
            return true
        }
        return false
    }

    func clickedLeftButtonFromPageIndex(index: Int) {
        if index == 1 {
            print("What!?! Squirtle is evolving!!")
        }
    }
}

您还可以在这里添加您的分析和其他内容。

将导航栏移到底部

class MySwipeVC: EZSwipeController {
    override func setupView() {
        super.setupView()
        datasource = self
        navigationBarShouldBeOnBottom = true
    }
}

移至新页面

class MySwipeVC: EZSwipeController {
    override func setupView() {
        super.setupView()
        datasource = self

        self.moveToPage(0)
    }
}

隐藏导航栏

class MySwipeVC: EZSwipeController {
    override func setupView() {
        super.setupView()
        navigationBarShouldNotExist = true
    }
}

附加设置

override func setupView() {
    cancelStandardButtonEvents()
    // Use this setting if you are using custom button that  
    // has nothing to do with swiping the viewcontroller
}
self.currentVCIndex 
//Use this to get the current page index

需求

  • Swift 3或更高版本

可能的特性

  • 更完善的文档,更多精美的图片!
  • 在源文件中完成任务

沟通

  • 如果您需要帮助,请使用Stack Overflow。 (标签 'ezswipecontroller’)
  • 如果您发现了一个bug,请打开一个问题。
  • 如果您有功能需求,请打开一个问题。
  • 如果您想要贡献,提交一个拉取请求。

许可证

EZSwipeController是在MIT许可证下提供的。请查看LICENSE文件

关键词

swift, 扩展, pageviewcontroller, uipageviewcontroller, tinder, snapchat, navigation