Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/Demo/BasicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class BasicViewController:UIViewController {

lazy var imageView:UIImageView = {
let iv = UIImageView()
iv.image = Data.images[0].resize(targetSize: .thumbnail)
iv.image = Data.images[0]

// Setup Image Viewer
iv.setupImageViewer()
Expand Down
17 changes: 16 additions & 1 deletion Example/Demo/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ struct Data {
"cat2",
"cat3",
"cat4",
"cat5"
"cat5",
"cat1",
"cat2",
"cat3",
"cat4",
"cat5",
"cat1",
"cat2",
"cat3",
"cat4",
"cat5",
"cat1",
"cat2",
"cat3",
"cat4",
"cat5",
]

static let images:[UIImage] = Self.imageNames.compactMap { UIImage(named: $0)! }
Expand Down
5 changes: 5 additions & 0 deletions Example/Demo/ExampleListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class ExampleListViewController:UITableViewController {

var items:[ExampleType] = ExampleType.allCases

override func viewDidLoad() {
super.viewDidLoad()
title = "ImageViewer.swift"
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
Expand Down
12 changes: 11 additions & 1 deletion Example/Demo/WithURLsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,18 @@ extension WithURLsViewController:UICollectionViewDataSource {
// Setup Image Viewer with [URL]
cell.imageView.setupImageViewer(
urls: Data.imageUrls,
initialIndex: indexPath.item)
initialIndex: indexPath.item,
options: [
.theme(.dark),
.rightNavItemTitle("Info", delegate: self)
])

return cell
}
}

extension WithURLsViewController:RightNavItemDelegate {
func imageViewer(_ imageViewer: ImageCarouselViewController, didTapRightNavItem index: Int) {
print("TAPPED", index)
}
}
52 changes: 40 additions & 12 deletions Sources/ImageCarouselViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public protocol ImageDataSource:class {
func imageItem(at index:Int) -> ImageItem
}

class ImageCarouselViewController:UIPageViewController {
public class ImageCarouselViewController:UIPageViewController {

weak var imageDatasource:ImageDataSource?
var initialIndex = 0
Expand All @@ -19,6 +19,8 @@ class ImageCarouselViewController:UIPageViewController {

var options:[ImageViewerOption] = []

weak var rightNavItemDelegate:RightNavItemDelegate?

private(set) lazy var navBar:UINavigationBar = {
let _navBar = UINavigationBar(frame: .zero)
_navBar.isTranslucent = true
Expand Down Expand Up @@ -61,15 +63,29 @@ class ImageCarouselViewController:UIPageViewController {

options.forEach {
switch $0 {
case .theme(let theme):
self.theme = theme
case .closeIcon(let icon):
navItem.leftBarButtonItem?.image = icon
case .theme(let theme):
self.theme = theme
case .closeIcon(let icon):
navItem.leftBarButtonItem?.image = icon
case .rightNavItemTitle(let title, let delegate):
navItem.rightBarButtonItem = UIBarButtonItem(
title: title,
style: .plain,
target: self,
action: #selector(diTapRightNavBarItem(_:)))
rightNavItemDelegate = delegate
case .rightNavItemIcon(let icon, let delegate):
navItem.rightBarButtonItem = UIBarButtonItem(
image: icon,
style: .plain,
target: self,
action: #selector(diTapRightNavBarItem(_:)))
rightNavItemDelegate = delegate
}
}
}

override func viewDidLoad() {
override public func viewDidLoad() {
super.viewDidLoad()

addBackgroundView()
Expand All @@ -92,24 +108,36 @@ class ImageCarouselViewController:UIPageViewController {
setViewControllers([initialVC], direction: .forward, animated: true, completion: nil)
}

override func viewDidAppear(_ animated: Bool) {
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.235) {
self.navBar.alpha = 1.0
}
}

@objc
func dismiss(_ sender:UIBarButtonItem?) {
private func dismiss(_ sender:UIBarButtonItem) {
dismissMe(completion: nil)
}

public func dismissMe(completion: (() -> Void)? = nil) {
sourceView.alpha = 1.0
UIView.animate(withDuration: 0.235, animations: {
self.view.alpha = 0.0
}) { _ in
self.dismiss(animated: false, completion: nil)
self.dismiss(animated: false, completion: completion)
}
}

override var preferredStatusBarStyle: UIStatusBarStyle {
@objc
func diTapRightNavBarItem(_ sender:UIBarButtonItem) {
guard let _delegate = rightNavItemDelegate,
let _firstVC = viewControllers?.first as? ImageViewerController
else { return }
_delegate.imageViewer(self, didTapRightNavItem: _firstVC.index)
}

override public var preferredStatusBarStyle: UIStatusBarStyle {
if theme == .dark {
return .lightContent
}
Expand All @@ -118,7 +146,7 @@ class ImageCarouselViewController:UIPageViewController {
}

extension ImageCarouselViewController:UIPageViewControllerDataSource {
func pageViewController(
public func pageViewController(
_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {

Expand All @@ -135,7 +163,7 @@ extension ImageCarouselViewController:UIPageViewControllerDataSource {
delegate: self)
}

func pageViewController(
public func pageViewController(
_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {

Expand Down
1 change: 1 addition & 0 deletions Sources/ImageViewerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class ImageViewerController:UIViewController, UIGestureRecognizerDelegate {

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
updateConstraintsForSize(view.bounds.size)
updateMinMaxZoomScaleForSize(view.bounds.size)
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/ImageViewerOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public enum ImageViewerOption {

case theme(ImageViewerTheme)
case closeIcon(UIImage)
case rightNavItemTitle(String, delegate: RightNavItemDelegate?)
case rightNavItemIcon(UIImage, delegate: RightNavItemDelegate?)
}
4 changes: 4 additions & 0 deletions Sources/RightNavItemDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public protocol RightNavItemDelegate:class {
func imageViewer(_ imageViewer: ImageCarouselViewController, didTapRightNavItem index:Int)
}
6 changes: 5 additions & 1 deletion Sources/UIImageView_Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ extension UIImageView {
@objc
private func showImageViewer(_ sender:TapWithDataRecognizer) {
guard let sourceView = sender.view as? UIImageView else { return }

let options = [UIPageViewController.OptionsKey.interPageSpacing: 20]

let imageCarousel = ImageCarouselViewController(
transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: nil)
options: options)

imageCarousel.sourceView = sourceView
imageCarousel.imageDatasource = sender.imageDatasource
imageCarousel.initialIndex = sender.initialIndex
Expand Down