Skip to content

Commit f876c1f

Browse files
authored
Merge pull request #643 from jsahoo/didTapDone
Add callbacks for when the Done button is tapped
2 parents b73eb2d + 5ad0ac7 commit f876c1f

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Objective-C/TOCropViewController/TOCropViewController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@
317317
*/
318318
@property (nullable, nonatomic, strong) NSArray<TOCropViewControllerAspectRatioPreset *> *allowedAspectRatios;
319319

320+
/**
321+
Called when the user hits the Done button.
322+
*/
323+
@property (nullable, nonatomic, copy) void (^onDidTapDone)(void);
324+
320325
/**
321326
When the user hits cancel, or completes a
322327
UIActivityViewController operation, this block will be called,

Objective-C/TOCropViewController/TOCropViewController.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,12 @@ - (void)doneButtonTapped {
894894
CGRect cropFrame = self.cropView.imageCropFrame;
895895
NSInteger angle = self.cropView.angle;
896896

897+
if (self.onDidTapDone) {
898+
dispatch_async(dispatch_get_main_queue(), ^{
899+
self.onDidTapDone();
900+
});
901+
}
902+
897903
// If desired, when the user taps done, show an activity sheet
898904
if (self.showActivitySheetOnDone) {
899905
TOActivityCroppedImageProvider *imageItem = [[TOActivityCroppedImageProvider alloc] initWithImage:self.image cropFrame:cropFrame angle:angle circular:(self.croppingStyle == TOCropViewCroppingStyleCircular)];

Swift/CropViewController/CropViewController.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public typealias CropViewCroppingStyle = TOCropViewCroppingStyle
4444
// ------------------------------------------------
4545

4646
@MainActor @objc public protocol CropViewControllerDelegate: NSObjectProtocol {
47+
/**
48+
Called when the user hits the Done button.
49+
*/
50+
@objc optional func cropViewControllerDidTapDone(_ cropViewController: CropViewController)
51+
4752
/**
4853
Called when the user has committed the crop action, and provides
4954
just the cropping rectangle.
@@ -319,7 +324,15 @@ open class CropViewController: UIViewController, TOCropViewControllerDelegate {
319324
set { toCropViewController.allowedAspectRatios = newValue }
320325
get { return toCropViewController.allowedAspectRatios }
321326
}
322-
327+
328+
/**
329+
Called when the user hits the Done button.
330+
*/
331+
public var onDidTapDone: (() -> Void)? {
332+
set { toCropViewController.onDidTapDone = newValue }
333+
get { return toCropViewController.onDidTapDone }
334+
}
335+
323336
/**
324337
When the user hits cancel, or completes a
325338
UIActivityViewController operation, this block will be called,
@@ -650,13 +663,21 @@ extension CropViewController {
650663

651664
fileprivate func setUpDelegateHandlers() {
652665
guard let delegate = self.delegate else {
666+
onDidTapDone = nil
653667
onDidCropToRect = nil
654668
onDidCropImageToRect = nil
655669
onDidCropToCircleImage = nil
656670
onDidFinishCancelled = nil
657671
return
658672
}
659-
673+
674+
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewControllerDidTapDone(_:))) {
675+
self.onDidTapDone = { [weak self] in
676+
guard let self else { return }
677+
delegate.cropViewControllerDidTapDone?(self)
678+
}
679+
}
680+
660681
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewController(_:didCropImageToRect:angle:))) {
661682
self.onDidCropImageToRect = {[weak self] rect, angle in
662683
guard let strongSelf = self else { return }

0 commit comments

Comments
 (0)