From cf1e7961f51f9f07a252343bf618a45b2a00d707 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 30 May 2017 13:56:59 -0400 Subject: [PATCH] Add support for customizing transition durations (#11) https://github.com/material-motion/transitioning-objc/issues/2 --- examples/FadeExample.m | 4 ++++ src/MDMTransition.h | 11 +++++++++++ src/private/MDMViewControllerTransitionContext.m | 6 ++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/examples/FadeExample.m b/examples/FadeExample.m index 26568c7..6bb8325 100644 --- a/examples/FadeExample.m +++ b/examples/FadeExample.m @@ -60,6 +60,10 @@ - (void)viewDidLoad { @implementation FadeTransition +- (NSTimeInterval)transitionDurationWithContext:(nonnull id)context { + return 0.3; +} + - (void)startWithContext:(id)context { [CATransaction begin]; [CATransaction setCompletionBlock:^{ diff --git a/src/MDMTransition.h b/src/MDMTransition.h index 13b72b1..ffee768 100644 --- a/src/MDMTransition.h +++ b/src/MDMTransition.h @@ -36,6 +36,17 @@ NS_SWIFT_NAME(Transition) @end +/** + A transition with custom duration is able to override the default transition duration. + */ +NS_SWIFT_NAME(TransitionWithCustomDuration) +@protocol MDMTransitionWithCustomDuration +/** + The desired duration of this transition in seconds. + */ +- (NSTimeInterval)transitionDurationWithContext:(nonnull id)context; +@end + /** A transition with presentation is able to customize the overall presentation of the transition, including adding temporary views and changing the destination frame of the presented view diff --git a/src/private/MDMViewControllerTransitionContext.m b/src/private/MDMViewControllerTransitionContext.m index c8c1076..6373ea4 100644 --- a/src/private/MDMViewControllerTransitionContext.m +++ b/src/private/MDMViewControllerTransitionContext.m @@ -50,8 +50,10 @@ - (nonnull instancetype)initWithTransition:(nonnull id)transition #pragma mark - UIViewControllerAnimatedTransitioning - (NSTimeInterval)transitionDuration:(id)transitionContext { - // TODO(featherless): Expose a TransitionWithTiming protocol that allows the transition to - // customize this value. + if ([_transition respondsToSelector:@selector(transitionDurationWithContext:)]) { + id withCustomDuration = (id)_transition; + return [withCustomDuration transitionDurationWithContext:self]; + } return 0.35; }