Skip to content

Commit 0eb0e51

Browse files
committed
Add animation delegate for extra customization.
1 parent 4b5012f commit 0eb0e51

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

Source/JTSImageViewController.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
@protocol JTSImageViewControllerOptionsDelegate;
1919
@protocol JTSImageViewControllerInteractionsDelegate;
2020
@protocol JTSImageViewControllerAccessibilityDelegate;
21+
@protocol JTSImageViewControllerAnimationDelegate;
2122

2223
typedef NS_ENUM(NSInteger, JTSImageViewControllerMode) {
2324
JTSImageViewControllerMode_Image,
@@ -60,6 +61,8 @@ extern CGFloat const JTSImageViewController_DefaultBackgroundBlurRadius;
6061

6162
@property (weak, nonatomic, readwrite) id <JTSImageViewControllerAccessibilityDelegate> accessibilityDelegate;
6263

64+
@property (weak, nonatomic, readwrite) id <JTSImageViewControllerAnimationDelegate> animationDelegate;
65+
6366
/**
6467
Designated initializer.
6568
@@ -207,6 +210,25 @@ extern CGFloat const JTSImageViewController_DefaultBackgroundBlurRadius;
207210

208211
@end
209212

213+
///---------------------------------------------------------------------------------------------------
214+
/// Animation Delegate
215+
///---------------------------------------------------------------------------------------------------
216+
217+
@protocol JTSImageViewControllerAnimationDelegate <NSObject>
218+
@optional
219+
220+
- (void)imageViewerWillBeginPresentation:(JTSImageViewController *)imageViewer withContainerView:(UIView *)containerView;
221+
222+
- (void)imageViewerWillAnimatePresentation:(JTSImageViewController *)imageViewer withContainerView:(UIView *)containerView duration:(CGFloat)duration;
223+
224+
- (void)imageViewer:(JTSImageViewController *)imageViewer willAdjustInterfaceForZoomScale:(CGFloat)zoomScale withContainerView:(UIView *)containerView duration:(CGFloat)duration;
225+
226+
- (void)imageViewerWillBeginDismissal:(JTSImageViewController *)imageViewer withContainerView:(UIView *)containerView;
227+
228+
- (void)imageViewerWillAnimateDismissal:(JTSImageViewController *)imageViewer withContainerView:(UIView *)containerView duration:(CGFloat)duration;
229+
230+
@end
231+
210232

211233

212234

Source/JTSImageViewController.m

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ - (void)showImageViewerByExpandingFromOriginalPositionFromViewController:(UIView
585585

586586
__weak JTSImageViewController *weakSelf = self;
587587

588+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginPresentation:withContainerView:)]) {
589+
[weakSelf.animationDelegate imageViewerWillBeginPresentation:weakSelf withContainerView:weakSelf.view];
590+
}
591+
588592
// Have to dispatch ahead two runloops,
589593
// or else the image view changes above won't be
590594
// committed prior to the animations below.
@@ -614,6 +618,10 @@ - (void)showImageViewerByExpandingFromOriginalPositionFromViewController:(UIView
614618
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
615619
animations:^{
616620

621+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimatePresentation:withContainerView:duration:)]) {
622+
[weakSelf.animationDelegate imageViewerWillAnimatePresentation:weakSelf withContainerView:weakSelf.view duration:duration];
623+
}
624+
617625
_flags.isTransitioningFromInitialModalToInteractiveState = YES;
618626

619627
if ([UIApplication sharedApplication].jts_usesViewControllerBasedStatusBarAppearance) {
@@ -722,6 +730,10 @@ - (void)showImageViewerByScalingDownFromOffscreenPositionWithViewController:(UIV
722730

723731
__weak JTSImageViewController *weakSelf = self;
724732

733+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginPresentation:withContainerView:)]) {
734+
[weakSelf.animationDelegate imageViewerWillBeginPresentation:weakSelf withContainerView:weakSelf.view];
735+
}
736+
725737
// Have to dispatch to the next runloop,
726738
// or else the image view changes above won't be
727739
// committed prior to the animations below.
@@ -733,6 +745,10 @@ - (void)showImageViewerByScalingDownFromOffscreenPositionWithViewController:(UIV
733745
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
734746
animations:^{
735747

748+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimatePresentation:withContainerView:duration:)]) {
749+
[weakSelf.animationDelegate imageViewerWillAnimatePresentation:weakSelf withContainerView:weakSelf.view duration:duration];
750+
}
751+
736752
_flags.isTransitioningFromInitialModalToInteractiveState = YES;
737753

738754
if ([UIApplication sharedApplication].jts_usesViewControllerBasedStatusBarAppearance) {
@@ -822,6 +838,10 @@ - (void)showAltTextFromViewController:(UIViewController *)viewController {
822838
duration *= 4;
823839
}
824840

841+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginPresentation:withContainerView:)]) {
842+
[weakSelf.animationDelegate imageViewerWillBeginPresentation:weakSelf withContainerView:weakSelf.view];
843+
}
844+
825845
// Have to dispatch to the next runloop,
826846
// or else the image view changes above won't be
827847
// committed prior to the animations below.
@@ -833,6 +853,10 @@ - (void)showAltTextFromViewController:(UIViewController *)viewController {
833853
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
834854
animations:^{
835855

856+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimatePresentation:withContainerView:duration:)]) {
857+
[weakSelf.animationDelegate imageViewerWillAnimatePresentation:weakSelf withContainerView:weakSelf.view duration:duration];
858+
}
859+
836860
_flags.isTransitioningFromInitialModalToInteractiveState = YES;
837861

838862
if ([UIApplication sharedApplication].jts_usesViewControllerBasedStatusBarAppearance) {
@@ -944,6 +968,10 @@ - (void)dismissByCollapsingImageBackToOriginalPosition {
944968

945969
__weak JTSImageViewController *weakSelf = self;
946970

971+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginDismissal:withContainerView:)]) {
972+
[weakSelf.animationDelegate imageViewerWillBeginDismissal:weakSelf withContainerView:weakSelf.view];
973+
}
974+
947975
// Have to dispatch after or else the image view changes above won't be
948976
// committed prior to the animations below. A single dispatch_async(dispatch_get_main_queue()
949977
// wouldn't work under certain scrolling conditions, so it has to be an ugly
@@ -966,6 +994,10 @@ - (void)dismissByCollapsingImageBackToOriginalPosition {
966994

967995
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
968996

997+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimateDismissal:withContainerView:duration:)]) {
998+
[weakSelf.animationDelegate imageViewerWillAnimateDismissal:weakSelf withContainerView:weakSelf.view duration:duration];
999+
}
1000+
9691001
weakSelf.snapshotView.transform = weakSelf.currentSnapshotRotationTransform;
9701002
[weakSelf removeMotionEffectsFromSnapshotView];
9711003
weakSelf.blackBackdrop.alpha = 0;
@@ -1037,7 +1069,16 @@ - (void)dismissByCleaningUpAfterImageWasFlickedOffscreen {
10371069
duration *= 4;
10381070
}
10391071

1072+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginDismissal:withContainerView:)]) {
1073+
[weakSelf.animationDelegate imageViewerWillBeginDismissal:weakSelf withContainerView:weakSelf.view];
1074+
}
1075+
10401076
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
1077+
1078+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimateDismissal:withContainerView:duration:)]) {
1079+
[weakSelf.animationDelegate imageViewerWillAnimateDismissal:weakSelf withContainerView:weakSelf.view duration:duration];
1080+
}
1081+
10411082
weakSelf.snapshotView.transform = weakSelf.currentSnapshotRotationTransform;
10421083
[weakSelf removeMotionEffectsFromSnapshotView];
10431084
weakSelf.blackBackdrop.alpha = 0;
@@ -1071,7 +1112,16 @@ - (void)dismissByExpandingImageToOffscreenPosition {
10711112
duration *= 4;
10721113
}
10731114

1115+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginDismissal:withContainerView:)]) {
1116+
[weakSelf.animationDelegate imageViewerWillBeginDismissal:weakSelf withContainerView:weakSelf.view];
1117+
}
1118+
10741119
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
1120+
1121+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimateDismissal:withContainerView:duration:)]) {
1122+
[weakSelf.animationDelegate imageViewerWillAnimateDismissal:weakSelf withContainerView:weakSelf.view duration:duration];
1123+
}
1124+
10751125
weakSelf.snapshotView.transform = weakSelf.currentSnapshotRotationTransform;
10761126
[weakSelf removeMotionEffectsFromSnapshotView];
10771127
weakSelf.blackBackdrop.alpha = 0;
@@ -1116,7 +1166,16 @@ - (void)dismissByExpandingAltTextToOffscreenPosition {
11161166
self.textView.delegate = nil;
11171167
self.textView = nil;
11181168

1169+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillBeginDismissal:withContainerView:)]) {
1170+
[weakSelf.animationDelegate imageViewerWillBeginDismissal:weakSelf withContainerView:weakSelf.view];
1171+
}
1172+
11191173
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
1174+
1175+
if ([weakSelf.animationDelegate respondsToSelector:@selector(imageViewerWillAnimateDismissal:withContainerView:duration:)]) {
1176+
[weakSelf.animationDelegate imageViewerWillAnimateDismissal:weakSelf withContainerView:weakSelf.view duration:duration];
1177+
}
1178+
11201179
weakSelf.snapshotView.transform = weakSelf.currentSnapshotRotationTransform;
11211180
[weakSelf removeMotionEffectsFromSnapshotView];
11221181
weakSelf.blackBackdrop.alpha = 0;
@@ -1496,9 +1555,13 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
14961555
#pragma mark - Update Dimming View for Zoom Scale
14971556

14981557
- (void)updateDimmingViewForCurrentZoomScale:(BOOL)animated {
1499-
CGFloat targetAlpha = (self.scrollView.zoomScale > 1) ? 1.0f : self.alphaForBackgroundDimmingOverlay;
1558+
CGFloat zoomScale = self.scrollView.zoomScale;
1559+
CGFloat targetAlpha = (zoomScale > 1) ? 1.0f : self.alphaForBackgroundDimmingOverlay;
15001560
CGFloat duration = (animated) ? 0.35 : 0;
15011561
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState animations:^{
1562+
if ([self.animationDelegate respondsToSelector:@selector(imageViewer:willAdjustInterfaceForZoomScale:withContainerView:duration:)]) {
1563+
[self.animationDelegate imageViewer:self willAdjustInterfaceForZoomScale:zoomScale withContainerView:self.view duration:duration];
1564+
}
15021565
self.blackBackdrop.alpha = targetAlpha;
15031566
} completion:nil];
15041567
}

0 commit comments

Comments
 (0)