GithubHelp home page GithubHelp logo

isabella232 / prslideview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wiredcraft/prslideview

0.0 0.0 0.0 30 KB

Slide view with gracefully written UIKit-like methods, delegate and data source protocol.

License: MIT License

Swift 100.00%

prslideview's Introduction

PRSlideView Carthage compatible Language License

Slide view with gracefully written UIKit-like methods, delegate and data source protocol.

Features

Horizontal or vertical scrolling

The direction is horizontal by default.

enum ScrollDirection {
    case horizontal
    case vertical
}

open private(set) var scrollDirection: PRSlideView.ScrollDirection = .horizontal

Infinite scrolling

Infinite scrolling is disabled by default.

open private(set) var infiniteScrollingEnabled: Bool = false

Automatical scrolling

When automatical scrolling is enabled:

  • Slide view scrolls to next page every 5 seconds by default
  • It runs on default runloop mode by default, which means when main runloop enters tracking mode, the timer stops
  • When user starts to scroll manually, the timer resets; when user stops scrolling, the timer starts again
  • Slide view remembers time interval and runloop mode you set last time until you change them
open private(set) var isAutoScrollingEnabled: Bool = false
open var autoScrollingTimeInterval: TimeInterval = 5.0
open var autoScrollingRunLoopMode: RunLoop.Mode = .default
open func startAutoScrolling()
open func startAutoScrolling(withTimeInterval interval: TimeInterval)
open func startAutoScrolling(withRunLoopMode mode: RunLoop.Mode)
open func startAutoScrolling(withTimeInterval interval: TimeInterval, withRunLoopMode mode: RunLoop.Mode)
open func stopAutoScrolling()

Page control (horizontal mode only)

The page control is enabled by default when scroll direction is horizontal.

However, when there are too many pages that there is not enough space to show page control, it will be hidden.

open var showsPageControl: Bool = true

Scroll manually

You can use these methods to scroll manually. Animation is enabled by default.

When infinite scrolling is enabled, calling scrollToPage(at:animated:) will cause it to scroll to the target page in current loop by default. However, you can assign a direction by calling scrollToPage(at:forward:animated:).

open func scrollToPage(at index: Int)
open func scrollToPage(at index: Int, animated: Bool)
open func scrollToPage(at index: Int, forward: Bool, animated: Bool)
open func scrollToPreviousPage()
open func scrollToNextPage()
open func scrollToPreviousPage(animated: Bool)
open func scrollToNextPage(animated: Bool)

Installation

Carthage

Add to your Cartfile:

github "Wiredcraft/PRSlideView" ~> 1.0

Usage

Create a Slide View

private lazy var slideView: PRSlideView = {
    let view = PRSlideView(direction: .horizontal,
                           infiniteScrolling: true)
    view.dataSource = self
    view.delegate = self
    view.register(AlbumPage.self,
                  forPageReuseIdentifier: String(describing: type(of: AlbumPage.self)))
    return view
}()

Create a Subclass of Slide View Page

import UIKit
import PRSlideView

class AlbumPage: PRSlideViewPage {
    
    lazy var coverImageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        view.translatesAutoresizingMaskIntoConstraints = false
        addSubview(view)
        let attributes: [NSLayoutConstraint.Attribute] = [.top, .bottom, .leading, .trailing]
        NSLayoutConstraint.activate(attributes.map {
            return NSLayoutConstraint(item: view,
                                      attribute: $0,
                                      relatedBy: .equal,
                                      toItem: self,
                                      attribute: $0,
                                      multiplier: 1,
                                      constant: 0)
        })
        return view
    }()
    
}

Use Data Source

extension AlbumViewController: PRSlideViewDataSource {
    
    func numberOfPagesInSlideView(_ slideView: PRSlideView) -> Int {
        return albumData.count
    }
    
    func slideView(_ slideView: PRSlideView, pageAt index: Int) -> PRSlideViewPage {
        let page = slideView.dequeueReusablePage(withIdentifier: String(describing: type(of: AlbumPage.self)),
                                                 for: index) as! AlbumPage
        page.coverImageView.image = UIImage(named: albumData[index] + ".jpg")
        return page
    }
    
}

Use Delegate

extension AlbumViewController: PRSlideViewDelegate {
    
    func slideView(_ slideView: PRSlideView, didScrollToPageAt index: Int) {
        titleLabel.text = albumData[index]
    }
    
    func slideView(_ slideView: PRSlideView, didClickPageAt index: Int) {
        let alert = UIAlertController(title: "You clicked on an album",
                                      message: albumData[index],
                                      preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK",
                                      style: .default,
                                      handler: nil))
        self.present(alert,
                     animated: true,
                     completion: nil)
    }
    
}

License

This project is released under the terms and conditions of the MIT license. See LICENSE for details.

Contact

This project is designed and developed by Elethom @ Wiredcraft. You can reach me via:

prslideview's People

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.