Skip to content

Commit

Permalink
Merge pull request #357 from JanGorman/image-analysis-interaction
Browse files Browse the repository at this point in the history
Implement delegate, return presenting controller
  • Loading branch information
JanGorman authored Nov 4, 2023
2 parents 210aed2 + 299e1b2 commit 3340023
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
11 changes: 9 additions & 2 deletions Agrume/Agrume.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ extension Agrume: UICollectionViewDataSource {
dataSource?.numberOfImages ?? 0
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
public func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
let cell: AgrumeCell = collectionView.dequeue(indexPath: indexPath)

cell.enableLiveText = enableLiveText
Expand Down Expand Up @@ -609,7 +612,11 @@ extension Agrume: AgrumeCellDelegate {
var isSingleImageMode: Bool {
dataSource?.numberOfImages == 1
}


var presentingController: UIViewController {
self
}

private func dismissCompletion(_ finished: Bool) {
presentingViewController?.dismiss(animated: false) { [unowned self] in
self.cleanup()
Expand Down
31 changes: 27 additions & 4 deletions Agrume/AgrumeCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import VisionKit
protocol AgrumeCellDelegate: AnyObject {

var isSingleImageMode: Bool { get }
var presentingController: UIViewController { get }

func dismissAfterFlick()
func dismissAfterTap()
Expand All @@ -35,14 +36,23 @@ final class AgrumeCell: UICollectionViewCell {
imageView.clipsToBounds = true
imageView.layer.allowsEdgeAntialiasing = true
}
private lazy var singleTapGesture = with(UITapGestureRecognizer(target: self, action: #selector(singleTap))) { gesture in
private lazy var singleTapGesture = with(
UITapGestureRecognizer(
target: self,
action: #selector(singleTap)
)
) { gesture in
gesture.require(toFail: doubleTapGesture)
gesture.delegate = self
}
private lazy var doubleTapGesture = with(UITapGestureRecognizer(target: self, action: #selector(doubleTap))) { gesture in
private lazy var doubleTapGesture = with(
UITapGestureRecognizer(target: self, action: #selector(doubleTap))
) { gesture in
gesture.numberOfTapsRequired = 2
}
private lazy var panGesture = with(UIPanGestureRecognizer(target: self, action: #selector(dismissPan))) { gesture in
private lazy var panGesture = with(
UIPanGestureRecognizer(target: self, action: #selector(dismissPan))
) { gesture in
gesture.maximumNumberOfTouches = 1
gesture.delegate = self
}
Expand Down Expand Up @@ -177,7 +187,12 @@ extension AgrumeCell: UIGestureRecognizerDelegate {

let width = scrollView.frame.width / scale
let height = scrollView.frame.height / scale
let destination = CGRect(x: translatedZoom.x - width / 2, y: translatedZoom.y - height / 2, width: width, height: height)
let destination = CGRect(
x: translatedZoom.x - width / 2,
y: translatedZoom.y - height / 2,
width: width,
height: height
)

contentView.isUserInteractionEnabled = false

Expand Down Expand Up @@ -497,6 +512,7 @@ extension AgrumeCell: UIScrollViewDelegate {
return
}
let interaction = ImageAnalysisInteraction()
interaction.delegate = self
imageView.addInteraction(interaction)

let analyzer = ImageAnalyzer()
Expand All @@ -514,3 +530,10 @@ extension AgrumeCell: UIScrollViewDelegate {
#endif
}
}

@available(iOS 16.0, *)
extension AgrumeCell: ImageAnalysisInteractionDelegate {
func presentingViewController(for interaction: ImageAnalysisInteraction) -> UIViewController? {
delegate?.presentingController
}
}
2 changes: 1 addition & 1 deletion Frameworks/SwiftyGif

0 comments on commit 3340023

Please sign in to comment.