Skip to content

Commit

Permalink
Merge pull request #284 from makenotion/master
Browse files Browse the repository at this point in the history
Allow replacing an image while maintaining the zoom scale and position
  • Loading branch information
JanGorman authored Sep 23, 2021
2 parents bda398a + 5ecca67 commit 9f0e898
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 47 deletions.
35 changes: 31 additions & 4 deletions Agrume/Agrume.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,18 @@ public final class Agrume: UIViewController {
/// - Parameters:
/// - index: The target index
/// - image: The replacement UIImage
public func updateImage(at index: Int, with image: UIImage) {
/// - newTitle: The new title, if nil then no change
public func updateImage(at index: Int, with image: UIImage, newTitle: NSAttributedString? = nil) {
assert(images.count > index)
let replacement = with(images[index]) { $0.image = image }
let replacement = with(images[index]) {
$0.url = nil
$0.image = image
if let newTitle = newTitle {
$0.title = newTitle
}
}

markAsUpdatingSameCell(at: index)
images[index] = replacement
reload()
}
Expand All @@ -253,13 +262,30 @@ public final class Agrume: UIViewController {
/// - Parameters:
/// - index: The target index
/// - url: The replacement URL
public func updateImage(at index: Int, with url: URL) {
/// - newTitle: The new title, if nil then no change
public func updateImage(at index: Int, with url: URL, newTitle: NSAttributedString? = nil) {
assert(images.count > index)
let replacement = with(images[index]) { $0.url = url }
let replacement = with(images[index]) {
$0.image = nil
$0.url = url
if let newTitle = newTitle {
$0.title = newTitle
}
}

markAsUpdatingSameCell(at: index)
images[index] = replacement
reload()
}

private func markAsUpdatingSameCell(at index: Int) {
collectionView.visibleCells.forEach { cell in
if let cell = cell as? AgrumeCell, cell.index == index {
cell.updatingImageOnSameCell = true
}
}
}

override public func viewDidLoad() {
super.viewDidLoad()
addSubviews()
Expand Down Expand Up @@ -452,6 +478,7 @@ extension Agrume: UICollectionViewDataSource {

spinner.alpha = 1
fetchImage(forIndex: indexPath.item) { [weak self] image in
cell.index = indexPath.item
cell.image = image
self?.spinner.alpha = 0
}
Expand Down
21 changes: 17 additions & 4 deletions Agrume/AgrumeCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@ final class AgrumeCell: UICollectionViewCell {
private var imageDragOffsetFromActualTranslation: UIOffset!
private var imageDragOffsetFromImageCenter: UIOffset!
private var attachmentBehavior: UIAttachmentBehavior?


// index of the cell in the collection view
var index: Int?

// if set to true, it means we are updating image on the same cell, so we want to reserve the zoom level & position
var updatingImageOnSameCell = false

var image: UIImage? {
didSet {
if image?.imageData != nil, let image = image {
imageView.setGifImage(image)
} else {
imageView.image = image
}
updateScrollViewAndImageViewForCurrentMetrics()
if !updatingImageOnSameCell {
updateScrollViewAndImageViewForCurrentMetrics()
}
updatingImageOnSameCell = false
}
}
weak var delegate: AgrumeCellDelegate?
Expand Down Expand Up @@ -88,9 +98,12 @@ final class AgrumeCell: UICollectionViewCell {

override func prepareForReuse() {
super.prepareForReuse()
imageView.image = nil
scrollView.zoomScale = 1
updateScrollViewAndImageViewForCurrentMetrics()

if !updatingImageOnSameCell {
imageView.image = nil
scrollView.zoomScale = 1
updateScrollViewAndImageViewForCurrentMetrics()
}
}

private func setupGestureRecognizers() {
Expand Down
4 changes: 4 additions & 0 deletions Example/Agrume Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
39CA658926EFFC5700A5A910 /* URLUpdatedToImageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39CA658826EFFC5700A5A910 /* URLUpdatedToImageViewController.swift */; };
771DA7342179EF1800541206 /* SwiftyGif.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 771DA7332179EF1800541206 /* SwiftyGif.framework */; };
9464AFE923C692C7006ADEBD /* OverlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9464AFE823C692C7006ADEBD /* OverlayView.swift */; };
948117D723C7A83600AE200D /* MultipleImagesCustomOverlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 948117D623C7A83600AE200D /* MultipleImagesCustomOverlayView.swift */; };
Expand Down Expand Up @@ -85,6 +86,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
39CA658826EFFC5700A5A910 /* URLUpdatedToImageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLUpdatedToImageViewController.swift; sourceTree = "<group>"; };
771DA7332179EF1800541206 /* SwiftyGif.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftyGif.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9464AFE823C692C7006ADEBD /* OverlayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverlayView.swift; sourceTree = "<group>"; };
948117D623C7A83600AE200D /* MultipleImagesCustomOverlayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultipleImagesCustomOverlayView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -183,6 +185,7 @@
E77809E21D17821400CC60F1 /* SingleImageModalViewController.swift */,
F2D9598D1B1A133800073772 /* SingleImageViewController.swift */,
F2D959901B1A140200073772 /* SingleURLViewController.swift */,
39CA658826EFFC5700A5A910 /* URLUpdatedToImageViewController.swift */,
F2A520181B130C7E00924912 /* Supporting Files */,
);
path = "Agrume Example";
Expand Down Expand Up @@ -354,6 +357,7 @@
buildActionMask = 2147483647;
files = (
E77809E31D17821400CC60F1 /* SingleImageModalViewController.swift in Sources */,
39CA658926EFFC5700A5A910 /* URLUpdatedToImageViewController.swift in Sources */,
F2D959911B1A140200073772 /* SingleURLViewController.swift in Sources */,
F2D7BA1F20A47FB500D5EE66 /* AnimatedGifViewController.swift in Sources */,
948117D723C7A83600AE200D /* MultipleImagesCustomOverlayView.swift in Sources */,
Expand Down
Loading

0 comments on commit 9f0e898

Please sign in to comment.