Skip to content

Commit

Permalink
add setCurrentIndexPath method
Browse files Browse the repository at this point in the history
  • Loading branch information
younatics committed Sep 11, 2017
1 parent e317885 commit 6fd9d15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
34 changes: 30 additions & 4 deletions MediaBrowser/MediaBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ public class MediaBrowser: UIViewController, UIScrollViewDelegate, UIActionSheet
/// Caching image count both side (e.g. when index 1, caching 0 and 2)
public var cachingImageCount = 1

/// Caching before MediaBrowser comes up
public var precachingEnabled = false {
/// Caching before MediaBrowser comes up, set
public var preCachingEnabled = false {
didSet {
if precachingEnabled {
if preCachingEnabled {
startPreCaching()
}
}
Expand Down Expand Up @@ -1037,7 +1037,7 @@ public class MediaBrowser: UIViewController, UIScrollViewDelegate, UIActionSheet

func startPreCaching() {
if let d = delegate {
let media = d.media(for: self, at: 0)
let media = d.media(for: self, at: currentPageIndex)
media.loadUnderlyingImageAndNotify()

} else {
Expand All @@ -1064,6 +1064,32 @@ public class MediaBrowser: UIViewController, UIScrollViewDelegate, UIActionSheet
}

//MARK: - Paging

/**
setCurrentIndex to show first.
When precaching, set this method first.

- Parameter index: Int
*/
public func setCurrentIndex(at index: Int) {
var internalIndex = 0
let mediaCount = self.numberOfMedias
if mediaCount != 0 {
if index >= mediaCount {
internalIndex = self.numberOfMedias - 1
} else {
internalIndex = index
}

currentPageIndex = internalIndex
if self.isViewLoaded {
self.jumpToPageAtIndex(index: internalIndex, animated: false)
if !viewIsActive {
self.tilePages()
}
}
}
}

func tilePages() {
// Calculate which pages should be visible
Expand Down
5 changes: 4 additions & 1 deletion MediaBrowserDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class ViewController: UITableViewController {
super.viewDidLoad()


// Set delegate and pre-cahing when you need
// Set delegate and pre-cahing when you need - add current index when you need to caching index
// browser = MediaBrowser(delegate: self)
// browser.setCurrentIndex(at: 1)
//
// browser.precachingEnabled = true

let font = UIFont.systemFont(ofSize: 12)
Expand Down Expand Up @@ -192,6 +194,7 @@ extension ViewController {
browser.startOnGrid = startOnGrid
browser.enableSwipeToDismiss = true
browser.autoPlayOnAppear = autoPlayOnAppear
// browser.setCurrentIndex(at: 1)


if displaySelectionButtons {
Expand Down

0 comments on commit 6fd9d15

Please sign in to comment.