Giter Club home page Giter Club logo

ezswipecontroller's Introduction

EZSwipeController

Cocoapods Compatible

Easy to use UIPageViewController to create a view navigation like Snapchat/Tinder/iOS Main Pages.

Demo

##Manual Install (~10 seconds)

  1. Download and drop 'EZSwipeController.swift' in your project.
  2. Congratulations!

Install via CocoaPods (~10 seconds)

You can use Cocoapods to install EZSwipeController by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!
pod 'EZSwipeController'
import EZSwipeController

##Setup

####Use Normally

Instead, you can also use EZSwipeController via push or present like:

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

####Use as RootViewController If you want to use EZSwipe as root viewcontroller (Your apps starting point):

Go to Targets -> Your Target -> General -> Main Interface -> Delete it

Add this to your AppDelegate:

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

####Create a subclass of 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]
    }
}

You should have something like this:

####Change Background Color

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

####Gives Titles To Pages

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

####Starting Page Index

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

####Custom Navigation Bar

Setting up navigationBarDataForPageIndex overrides effects in 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
    }
}

You don't need to set actions to buttons, EZSwipeController automatically overrides them and makes them work.

####Add Images To Navigation Bar

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
}

####Custom Button Settings

Sometimes you may want to add your own actions to buttons, in that case you should disable the default button behaviour:

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!!")
        }
    }
}

You can also add your analytics and other stuff in here.

####Move Navigation Bar to Bottom

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

##Requirements

  • Swift 2 or later

##Possible features

  • Better documentation with more fancy pictures!
  • Completing TODOs inside source files

##License EZSwipeController is available under the MIT license. See the LICENSE file.

##Keywords swift, extension, pageviewcontroller, uipageviewcontroller, tinder, snapchat, navigation

ezswipecontroller's People

Contributors

esqarrouth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.