Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to remove an item from image viewer #101

Merged
merged 2 commits into from
Jan 11, 2017
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
28 changes: 14 additions & 14 deletions Example/CounterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,47 @@
import UIKit

class CounterView: UIView {
let count: Int

var count: Int
let countLabel = UILabel()
var currentIndex: Int {
didSet {
updateLabel()
}
}

init(frame: CGRect, currentIndex: Int, count: Int) {

self.currentIndex = currentIndex
self.count = count

super.init(frame: frame)

configureLabel()
updateLabel()
}

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

func configureLabel() {

countLabel.textAlignment = .center
self.addSubview(countLabel)
}

func updateLabel() {

let stringTemplate = "%d of %d"
let countString = String(format: stringTemplate, arguments: [currentIndex + 1, count])

countLabel.attributedText = NSAttributedString(string: countString, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 17), NSForegroundColorAttributeName: UIColor.white])
}

override func layoutSubviews() {
super.layoutSubviews()

countLabel.frame = self.bounds
}
}
123 changes: 81 additions & 42 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import UIKit

extension UIImageView: DisplaceableView {}

class ViewController: UIViewController, GalleryItemsDataSource, GalleryDisplacedViewsDataSource {
struct DataItem {

let imageView: UIImageView
let galleryItem: GalleryItem
}

class ViewController: UIViewController {

@IBOutlet weak var image1: UIImageView!
@IBOutlet weak var image2: UIImageView!
Expand All @@ -20,25 +26,56 @@ class ViewController: UIViewController, GalleryItemsDataSource, GalleryDisplaced
@IBOutlet weak var image6: UIImageView!
@IBOutlet weak var image7: UIImageView!

var imageViews: [UIImageView] = []
var items: [DataItem] = []

override func viewDidLoad() {
super.viewDidLoad()

imageViews += [image1, image2, image3, image4, image5, image6, image7]
let imageViews = [image1, image2, image3, image4, image5, image6, image7]

for (index, imageView) in imageViews.enumerated() {

guard let imageView = imageView else { continue }
var galleryItem: GalleryItem!

switch index {

case 2:

galleryItem = GalleryItem.video(fetchPreviewImageBlock: { $0(UIImage(named: "2")!) }, videoURL: URL (string: "http://video.dailymail.co.uk/video/mol/test/2016/09/21/5739239377694275356/1024x576_MP4_5739239377694275356.mp4")!)

case 4:

let myFetchImageBlock: FetchImageBlock = { $0(imageView.image!) }

let itemViewControllerBlock: ItemViewControllerBlock = { index, itemCount, fetchImageBlock, configuration, isInitialController in

return AnimatedViewController(index: index, itemCount: itemCount, fetchImageBlock: myFetchImageBlock, configuration: configuration, isInitialController: isInitialController)
}

galleryItem = GalleryItem.custom(fetchImageBlock: myFetchImageBlock, itemViewControllerBlock: itemViewControllerBlock)

default:

let image = imageView.image ?? UIImage(named: "0")!
galleryItem = GalleryItem.image { $0(image) }
}

items.append(DataItem(imageView: imageView, galleryItem: galleryItem))
}
}

@IBAction func showGalleryImageViewer(_ sender: UITapGestureRecognizer) {

guard let displacedView = sender.view as? UIImageView else { return }

guard let displacedViewIndex = imageViews.index(of: displacedView) else { return }
guard let displacedViewIndex = items.index(where: { $0.imageView == displacedView }) else { return }

let frame = CGRect(x: 0, y: 0, width: 200, height: 24)
let headerView = CounterView(frame: frame, currentIndex: displacedViewIndex, count: imageViews.count)
let footerView = CounterView(frame: frame, currentIndex: displacedViewIndex, count: imageViews.count)
let headerView = CounterView(frame: frame, currentIndex: displacedViewIndex, count: items.count)
let footerView = CounterView(frame: frame, currentIndex: displacedViewIndex, count: items.count)

let galleryViewController = GalleryViewController(startIndex: displacedViewIndex, itemsDataSource: self, displacedViewsDataSource: self, configuration: galleryConfiguration())
let galleryViewController = GalleryViewController(startIndex: displacedViewIndex, itemsDataSource: self, itemsDelegate: self, displacedViewsDataSource: self, configuration: galleryConfiguration())
galleryViewController.headerView = headerView
galleryViewController.footerView = footerView

Expand All @@ -50,52 +87,21 @@ class ViewController: UIViewController, GalleryItemsDataSource, GalleryDisplaced

print("LANDED AT INDEX: \(index)")

headerView.count = self.items.count
headerView.currentIndex = index
footerView.count = self.items.count
footerView.currentIndex = index
}

self.presentImageGallery(galleryViewController)
}

func itemCount() -> Int {

return imageViews.count
}

func provideDisplacementItem(atIndex index: Int) -> DisplaceableView? {

return imageViews[index]
}

func provideGalleryItem(_ index: Int) -> GalleryItem {

if index == 2 {

return GalleryItem.video(fetchPreviewImageBlock: { $0(UIImage(named: "2")!)} , videoURL: URL(string: "http://video.dailymail.co.uk/video/mol/test/2016/09/21/5739239377694275356/1024x576_MP4_5739239377694275356.mp4")!)
}
if index == 4 {

let myFetchImageBlock: FetchImageBlock = { [weak self] in $0(self?.imageViews[index].image!) }

let itemViewControllerBlock: ItemViewControllerBlock = { index, itemCount, fetchImageBlock, configuration, isInitialController in

return AnimatedViewController(index: index, itemCount: itemCount, fetchImageBlock: myFetchImageBlock, configuration: configuration, isInitialController: isInitialController)
}

return GalleryItem.custom(fetchImageBlock: myFetchImageBlock, itemViewControllerBlock: itemViewControllerBlock)
}
else {

let image = imageViews[index].image ?? UIImage(named: "0")!

return GalleryItem.image { $0(image) }
}
}

func galleryConfiguration() -> GalleryConfiguration {

return [

GalleryConfigurationItem.closeButtonMode(.builtIn),

GalleryConfigurationItem.pagingMode(.standard),
GalleryConfigurationItem.presentationStyle(.displacement),
GalleryConfigurationItem.hideDecorationViewsOnLaunch(false),
Expand Down Expand Up @@ -139,6 +145,39 @@ class ViewController: UIViewController, GalleryItemsDataSource, GalleryDisplaced
}
}

extension ViewController: GalleryDisplacedViewsDataSource {

func provideDisplacementItem(atIndex index: Int) -> DisplaceableView? {

return index < items.count ? items[index].imageView : nil
}
}

extension ViewController: GalleryItemsDataSource {

func itemCount() -> Int {

return items.count
}

func provideGalleryItem(_ index: Int) -> GalleryItem {

return items[index].galleryItem
}
}

extension ViewController: GalleryItemsDelegate {

func removeGalleryItem(at index: Int) {

print("remove item at \(index)")

let imageView = items[index].imageView
imageView.removeFromSuperview()
items.remove(at: index)
}
}

// Some external custom UIImageView we want to show in the gallery
class FLSomeAnimatedImage: UIImageView {
}
Expand Down
6 changes: 6 additions & 0 deletions ImageViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
4CD1C49C1C16FF64008D5F84 /* CGPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CD1C49B1C16FF64008D5F84 /* CGPoint.swift */; };
4CD1C49D1C16FF64008D5F84 /* CGPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CD1C49B1C16FF64008D5F84 /* CGPoint.swift */; };
5AF0E6605E3B055280A902DF /* GalleryItemsDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AF0EF3CAFE8B7972354F05D /* GalleryItemsDelegate.swift */; };
5AF0EB5F09D47E20BE670D89 /* GalleryItemsDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AF0EF3CAFE8B7972354F05D /* GalleryItemsDelegate.swift */; };
A32D40041D45C4DE00AB5ADA /* Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = A32D40031D45C4DE00AB5ADA /* Bool.swift */; };
A32D40051D45C50800AB5ADA /* Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = A32D40031D45C4DE00AB5ADA /* Bool.swift */; };
C2573FA71DA6FED600DABB1E /* ThumbnailCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2573FA51DA6FED600DABB1E /* ThumbnailCell.swift */; };
Expand Down Expand Up @@ -89,6 +91,7 @@

/* Begin PBXFileReference section */
4CD1C49B1C16FF64008D5F84 /* CGPoint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPoint.swift; sourceTree = "<group>"; };
5AF0EF3CAFE8B7972354F05D /* GalleryItemsDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GalleryItemsDelegate.swift; sourceTree = "<group>"; };
A32D40031D45C4DE00AB5ADA /* Bool.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bool.swift; sourceTree = "<group>"; };
A393838A1D91EE7200AC9893 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C2573FA51DA6FED600DABB1E /* ThumbnailCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThumbnailCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -294,6 +297,7 @@
children = (
EB40C3761C9C5C48004EA215 /* GalleryItemsDataSource.swift */,
EBE6270E1D268A7F0095999A /* GalleryDisplacedViewsDataSource.swift */,
5AF0EF3CAFE8B7972354F05D /* GalleryItemsDelegate.swift */,
);
name = Protocols;
sourceTree = "<group>";
Expand Down Expand Up @@ -540,6 +544,7 @@
C2573FA91DA6FF3C00DABB1E /* ThumbnailsViewController.swift in Sources */,
EBE6270F1D268A7F0095999A /* GalleryDisplacedViewsDataSource.swift in Sources */,
C7897CE51C1350B7006447FB /* AppDelegate.swift in Sources */,
5AF0E6605E3B055280A902DF /* GalleryItemsDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -582,6 +587,7 @@
EB7B7E4E1C8F240D006834F7 /* LayoutEnums.swift in Sources */,
EBA90E441C875225000AA697 /* ImageFadeInHandler.swift in Sources */,
EB057A161D510264007565F1 /* AVPlayer.swift in Sources */,
5AF0EB5F09D47E20BE670D89 /* GalleryItemsDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 12 additions & 3 deletions ImageViewer.xcodeproj/xcshareddata/xcschemes/ImageViewer.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C7DABE471C12245B00F5BD7B"
BuildableName = "ImageViewer.framework"
BlueprintName = "ImageViewer"
BlueprintIdentifier = "C7897CE11C1350B7006447FB"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:ImageViewer.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -42,6 +42,15 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C7897CE11C1350B7006447FB"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:ImageViewer.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand Down
Loading