gauravkatoch007 / banana

Swift Image Slider library for iOS
MIT License
17 stars 4 forks source link

Here is an updated version for swift 4 #10

Open rami965 opened 6 years ago

rami965 commented 6 years ago
//
//  banana.swift
//  banana
//
//  Created by cdp  on 11/9/15.
//  Copyright © 2015 Katoch. All rights reserved.
//

import Foundation
import UIKit

public class banana : UIViewController, UIScrollViewDelegate {

    var imagePageControl: UIPageControl?
    var imageScrollView: UIScrollView!
    var pageImages: [UIImage] = []
    var pageViews: [UIImageView?] = []
    var timer : Timer = Timer()
    var imagesLoaded = false
    public var autoScrollTime : Double = 8
    public var imagesToLoadInMemory = 4

    public convenience init ( imageScrollView : UIScrollView, imagePageControl : UIPageControl? = nil){
        self.init()
        self.imageScrollView = imageScrollView
        self.imageScrollView.delegate = self
        self.imagePageControl = imagePageControl

    }

//    required public init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }

    @nonobjc public func load(imagesArrayInput : [String]){
        DispatchQueue.global(qos: .default).async {
            // do some task
            self.getScrollViewImages(imagesArrayInput)
            DispatchQueue.main.async {
                // update some UI
                self.loadScrollViewImages()
            }
        }

    }

    @nonobjc public func load(imagesArrayInput : [UIImage]){
        DispatchQueue.global(qos: .default).async {
            // do some task
            self.getScrollViewImages(imagesArrayInput)
            DispatchQueue.main.async {
                // update some UI
                self.loadScrollViewImages()
            }
        }
    }

    public func startScroll(){
        timer = Timer.scheduledTimer(timeInterval: autoScrollTime, target: self, selector: #selector(autoScrollImage), userInfo: nil, repeats: true)
    }

    public func stopScroll(){
        timer.invalidate()
    }

    @nonobjc func getScrollViewImages(_ imagesArray : [String]){
        for image in imagesArray {
            if let url = NSURL(string: image) {
                if let data = NSData(contentsOf: url as URL){
                    pageImages.append(UIImage(data: data as Data)!)
                }
            }
        }
    }

    @nonobjc func getScrollViewImages(_ imagesArray : [UIImage]){
        for image in imagesArray {
            pageImages.append(image)
        }
    }

    func loadScrollViewImages(){
        let pageCount = pageImages.count

        // 2
        if imagePageControl != nil {
            imagePageControl!.currentPage = 1
            imagePageControl!.numberOfPages = pageCount
        }

        // 3
        for _ in 0..<pageCount {
            pageViews.append(nil)
        }

        // 4
        let pagesScrollViewSize = imageScrollView.frame.size
        imageScrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(pageImages.count),
            height: pagesScrollViewSize.height)

        // 5
        self.imagesLoaded = true
        loadVisiblePages()
    }

    func loadPage(page: Int) {
        if page < 0 || page >= pageImages.count {
            // If it's outside the range of what you have to display, then do nothing
            return
        }

        // 1
        if pageViews[page] != nil {
            // Do nothing. The view is already loaded.
        } else {
            // 2
            var frame = imageScrollView.bounds
            frame.origin.x = frame.size.width * CGFloat(page)
            frame.origin.y = 0.0
            //            frame = CGRectInset(frame, 10.0, 0.0)

            // 3
            let newPageView = UIImageView(image: pageImages[page])
            newPageView.contentMode = .scaleAspectFit
            newPageView.frame = frame
            imageScrollView.addSubview(newPageView)

            // 4
            pageViews[page] = newPageView
        }
    }

    func purgePage(page: Int) {
        if page < 0 || page >= pageImages.count {
            // If it's outside the range of what you have to display, then do nothing
            return
        }

        // Remove a page from the scroll view and reset the container array
        if let pageView = pageViews[page] {
            pageView.removeFromSuperview()
            pageViews[page] = nil
        }
    }

    public func loadVisiblePages() {
        // First, determine which page is currently visible
        let pageWidth = imageScrollView.frame.size.width
        var page = Int(floor((imageScrollView.contentOffset.x * 1.0 + pageWidth) / (pageWidth * 1.0))) - 1
        if page >= pageImages.count {
            page = 0
        }
        // Update the page control
        if imagePageControl != nil {
            imagePageControl!.currentPage = page
        }

        // Work out which pages you want to load
        let firstPage = page - ( imagesToLoadInMemory / 2 )
        let lastPage = page + ( imagesToLoadInMemory / 2 )

        print("First Page ="+String(firstPage))
        print("Last Page ="+String(lastPage))

        // Purge anything before the first page
        for index in stride(from: 0, to: firstPage, by: 1) {
            purgePage(page: index)

        }

        // Load pages in our range
        for index in firstPage...lastPage {
            loadPage(page: index)
        }

        // Purge anything after the last page
        for index in stride(from: 0, to: pageImages.count, by: 1) {
            purgePage(page: index)
        }

    }

    @objc public func autoScrollImage (){

        let pageWidth:CGFloat = self.imageScrollView.frame.width
        let maxWidth:CGFloat = pageWidth * CGFloat(pageImages.count)
        let contentOffset:CGFloat = self.imageScrollView.contentOffset.x

        var slideToX = contentOffset + pageWidth

        if  contentOffset + pageWidth == maxWidth{
            slideToX = 0
        }

        self.imageScrollView.scrollRectToVisible(CGRect(x: slideToX, y: 0, width: pageWidth, height: self.imageScrollView.frame.height), animated: true)
    }

    public func assignTouchGesture(){
//        let tapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewTapped:")
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(scrollViewTapped(recognizer:)))

        tapRecognizer.numberOfTapsRequired = 1
        //        tapRecognizer.numberOfTouchesRequired = 1
        self.imageScrollView.addGestureRecognizer(tapRecognizer)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
        swipeRight.direction = UISwipeGestureRecognizerDirection.right
        self.imageScrollView.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left
        self.imageScrollView.addGestureRecognizer(swipeLeft)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
        swipeUp.direction = UISwipeGestureRecognizerDirection.up
        self.imageScrollView.addGestureRecognizer(swipeUp)

        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
        swipeDown.direction = UISwipeGestureRecognizerDirection.down
        self.imageScrollView.addGestureRecognizer(swipeDown)
    }
    @objc public func scrollViewTapped(recognizer: UITapGestureRecognizer){
        timer.invalidate()
        if self.imagesLoaded == true {
            loadVisiblePages()
        }
    }

    @objc public func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        timer.invalidate()
        if self.imagesLoaded == true {
            loadVisiblePages()
        }
    }

    @objc public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if self.imagesLoaded == true {
            loadVisiblePages()
        }
    }

    @objc public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        if self.imagesLoaded == true {
            loadVisiblePages()
        }
    }

}
gauravkatoch007 commented 6 years ago

Please send a pull request.