Skip to content
Open
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
12 changes: 11 additions & 1 deletion Example/Optik/Images.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -35,4 +45,4 @@
"version" : 1,
"author" : "xcode"
}
}
}
6 changes: 6 additions & 0 deletions Example/Optik/Images.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
12 changes: 12 additions & 0 deletions Example/Optik/Images.xcassets/heart-empty.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "heart.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file not shown.
12 changes: 12 additions & 0 deletions Example/Optik/Images.xcassets/heart-full.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "heart-fill.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file not shown.
20 changes: 18 additions & 2 deletions Example/Optik/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal final class ViewController: UIViewController {
@IBAction private func presentLocalImageViewer(_ sender: UIButton) {
let viewController = Optik.imageViewer(withImages: localImages,
initialImageDisplayIndex: currentLocalImageIndex,
delegate: self)
delegate: self, enablePageControl: true)

present(viewController, animated: true, completion: nil)
}
Expand All @@ -62,7 +62,7 @@ internal final class ViewController: UIViewController {
let urls = [url1, url2, url3, url4]
let imageDownloader = AlamofireImageDownloader()

let viewController = Optik.imageViewer(withURLs: urls, imageDownloader: imageDownloader)
let viewController = Optik.imageViewer(withURLs: urls, imageDownloader: imageDownloader, enablePageControl: true)
present(viewController, animated: true, completion: nil)
}

Expand All @@ -82,4 +82,20 @@ extension ViewController: ImageViewerDelegate {
currentLocalImageIndex = index
}

func actionButtonTapped(button: UIButton, at index: Int) {
if button.currentImage == UIImage(named: "heart-full")! {
button.setImage(UIImage(named: "heart-empty"), for: .normal)
} else {
button.setImage(UIImage(named: "heart-full"), for: .normal)
}
}

func imageForActionButton (at index: Int) -> UIImage {
if index % 2 == 0 {
return UIImage(named: "heart-full")!
} else {
return UIImage(named: "heart-empty")!
}
}

}
34 changes: 34 additions & 0 deletions Optik/Classes/ActionButtonPosition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ActionButtonPosition.swift
// Pods
//
// Created by Rodrigo Leite on 28/11/16.
//
//

import Foundation

/**
Defines the position of the action button.

- topLeading: action button is constrained to the top and leading anchors of its superview.
- topTrailing: action button is constrained to the top and trailing anchors of its superview.
*/
public enum ActionButtonPosition {

case bottomLeading

func xAnchorAttribute() -> NSLayoutAttribute {
switch self {
case .bottomLeading:
return .leading
}
}

func yAnchorAttribute() -> NSLayoutAttribute {
switch self {
case .bottomLeading:
return .bottom
}
}
}
124 changes: 117 additions & 7 deletions Optik/Classes/AlbumViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,46 @@ internal final class AlbumViewController: UIViewController {
return viewControllers[0] as? ImageViewController
}

private var imageData: ImageData
fileprivate var imageData: ImageData
private var initialImageDisplayIndex: Int
private var activityIndicatorColor: UIColor?
private var dismissButtonImage: UIImage?
private var dismissButtonPosition: DismissButtonPosition

private var actionButtonPosition: ActionButtonPosition
private var cachedRemoteImages: [URL: UIImage] = [:]
private var viewDidAppear: Bool = false

private var transitionController: TransitionController = TransitionController()

fileprivate var pageControl: UIPageControl?

// MARK: - Init/Deinit

init(imageData: ImageData,
initialImageDisplayIndex: Int,
activityIndicatorColor: UIColor?,
dismissButtonImage: UIImage?,
dismissButtonPosition: DismissButtonPosition) {
dismissButtonPosition: DismissButtonPosition,
enablePageControl: Bool,
actionButtonPosition: ActionButtonPosition) {

self.imageData = imageData
self.initialImageDisplayIndex = initialImageDisplayIndex
self.activityIndicatorColor = activityIndicatorColor
self.dismissButtonImage = dismissButtonImage
self.dismissButtonPosition = dismissButtonPosition
self.actionButtonPosition = actionButtonPosition

pageViewController = UIPageViewController(transitionStyle: .scroll,
navigationOrientation: .horizontal,
if enablePageControl {
pageViewController = UIPageViewController(transitionStyle: .scroll,
navigationOrientation: .vertical,
options: [UIPageViewControllerOptionInterPageSpacingKey : Constants.SpacingBetweenImages])
pageControl = UIPageControl()
} else {
pageViewController = UIPageViewController(transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: [UIPageViewControllerOptionInterPageSpacingKey : Constants.SpacingBetweenImages])
}

super.init(nibName: nil, bundle: nil)

Expand Down Expand Up @@ -115,6 +127,8 @@ internal final class AlbumViewController: UIViewController {
self.setNeedsStatusBarAppearanceUpdate()
})
}
updateActionButtonImage(at: initialImageDisplayIndex)

}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
Expand Down Expand Up @@ -147,6 +161,7 @@ internal final class AlbumViewController: UIViewController {
didMove(toParentViewController: pageViewController)

setupDismissButton()
setupPageControl()
setupPanGestureRecognizer()
}

Expand All @@ -163,6 +178,56 @@ internal final class AlbumViewController: UIViewController {
}
}

private func setupPageControl() {

if let page = pageControl {
page.currentPage = 0
page.pageIndicatorTintColor = UIColor.red
page.translatesAutoresizingMaskIntoConstraints = false
page.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI/2))
view.addSubview(page)

view.addConstraint(
NSLayoutConstraint(item: page,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1,
constant: -10)
)
view.addConstraint(
NSLayoutConstraint(item: page,
attribute: .centerY,
relatedBy: .equal,
toItem: view,
attribute: .centerY,
multiplier: 1,
constant: 0)
)
view.addConstraint(
NSLayoutConstraint(item: page,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 30)
)
view.addConstraint(
NSLayoutConstraint(item: page,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 25)
)
}

}


private func setupDismissButton() {
let dismissButton = UIButton(type: .custom)
dismissButton.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -226,13 +291,18 @@ internal final class AlbumViewController: UIViewController {
return nil
}

return ImageViewController(image: images[index], index: index)
pageControl?.numberOfPages = images.count
let imageViewController = ImageViewController(image: images[index], index: index, actionButtonPosition: .bottomLeading)
imageViewController.actionButton?.addTarget(self, action: #selector(AlbumViewController.didSelectImage(_:)), for: .touchUpInside)
return imageViewController

case .remote(let urls, let imageDownloader):
guard index >= 0 && index < urls.count else {
return nil
}

let imageViewController = ImageViewController(activityIndicatorColor: activityIndicatorColor, index: index)
pageControl?.numberOfPages = urls.count
let imageViewController = ImageViewController(activityIndicatorColor: activityIndicatorColor, index: index, actionButtonPosition: actionButtonPosition)
let url = urls[index]

if let image = cachedRemoteImages[url] {
Expand All @@ -245,11 +315,22 @@ internal final class AlbumViewController: UIViewController {
imageViewController.image = image
})
}


imageViewController.actionButton?.addTarget(self, action: #selector(AlbumViewController.didSelectImage(_:)), for: .touchUpInside)
return imageViewController
}
}

fileprivate func updateActionButtonImage(at index: Int) {
if let imageViewController = pageViewController.viewControllers?.first as? ImageViewController{
let actionButton = imageViewController.actionButton
let image = imageViewerDelegate?.imageForActionButton(at: index)
actionButton?.setImage(image, for: .normal)
}
}


@objc private func didTapDismissButton(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
Expand All @@ -258,6 +339,11 @@ internal final class AlbumViewController: UIViewController {
transitionController.didPan(withGestureRecognizer: sender, sourceView: view)
}

@objc private func didSelectImage(_ sender: UIButton) {
if let currentImageIndex = currentImageViewController?.index {
imageViewerDelegate?.actionButtonTapped(button: sender, at: currentImageIndex)
}
}
}

// MARK: - Protocol conformance
Expand Down Expand Up @@ -306,6 +392,30 @@ extension AlbumViewController: UIPageViewControllerDelegate {

if let currentImageIndex = currentImageViewController?.index {
imageViewerDelegate?.imageViewerDidDisplayImage(at: currentImageIndex)
pageControl?.currentPage = currentImageIndex
self.updateActionButtonImage(at: currentImageIndex)

}
}

func pageViewController(pageViewController: UIPageViewController, spineLocationForInterfaceOrientation orientation: UIInterfaceOrientation) -> UIPageViewControllerSpineLocation {
return .max
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return numberOfImages()
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return currentImageViewController?.index ?? 0
}

private func numberOfImages() -> Int {
switch imageData {
case .local(images: let images):
return images.count
case .remote(urls: let urls, imageDownloader: _):
return urls.count
}
}

Expand Down
Loading