Skip to content

Commit 4d4bf39

Browse files
committed
Add a dimming effect as the menu is shown and hidden
1 parent 9852374 commit 4d4bf39

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

Source/ALAnimationController.m

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "UIView+ALLayout.h"
1111

1212
static NSTimeInterval const kAnimationDuration = 0.3;
13+
static CGFloat const kDimmingViewMaxAlpha = 0.5f;
1314
static CGFloat const kExpandViewLayerScaleFactor = 1.5f;
1415
static CGFloat const kCASpringAnimationDamping = 15.f;
1516
static CGFloat const kCASpringAnimationStiffness = 115.f;
@@ -78,31 +79,39 @@ - (void)animatePresentationWithContainerView:(UIView *)containerView fromView:(U
7879
maskLayerAnimation.delegate = self;
7980
[maskLayer addAnimation:maskLayerAnimation forKey:@"maskLayer"];
8081

82+
// create the dimming effect as the menu is shown
83+
UIView *dimmingView = [self addDimmingViewToContainerView:containerView belowView:toView];
84+
dimmingView.alpha = 0.f;
85+
8186
// animate the outgoing view. we can animate these layer-backed views with
8287
// UIView animation methods.
8388
//
8489

85-
if (self.disappearingAnimation == ALNavigationCoordinatorAnimationNone)
90+
BOOL shouldAnimateTransform = self.disappearingAnimation != ALNavigationCoordinatorAnimationNone;
91+
92+
if (shouldAnimateTransform)
8693
{
87-
return;
94+
// set fromView's anchor point to be based on the location of the initial shape
95+
// layer, so the expansion animation will look like it's originating from the
96+
// location of the initial shape. note that since we're adjusting the anchor
97+
// point, we'll also have to adjust the position to keep the view from jumping
98+
// to a new location onscreen.
99+
//
100+
[self adjustAnchorPointAndPositionForView:fromView];
88101
}
89102

90-
// set fromView's anchor point to be based on the location of the initial shape
91-
// layer, so the expansion animation will look like it's originating from the
92-
// location of the initial shape. note that since we're adjusting the anchor
93-
// point, we'll also have to adjust the position to keep the view from jumping
94-
// to a new location onscreen.
95-
//
96-
[self adjustAnchorPointAndPositionForView:fromView];
97-
98103
// no springs here (not needed, the view will be out of sight before the spring
99104
// effect would be noticed)
100105
//
101106
[UIView animateWithDuration:maskLayerAnimation.duration
102107
delay:0.
103108
options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
104109
animations:^{
105-
fromView.transform = CGAffineTransformScale(CGAffineTransformIdentity, kExpandViewLayerScaleFactor, kExpandViewLayerScaleFactor);
110+
if (shouldAnimateTransform)
111+
{
112+
fromView.transform = CGAffineTransformScale(CGAffineTransformIdentity, kExpandViewLayerScaleFactor, kExpandViewLayerScaleFactor);
113+
}
114+
dimmingView.alpha = kDimmingViewMaxAlpha;
106115
}
107116
completion:nil];
108117

@@ -131,23 +140,31 @@ - (void)animateDismissalWithContainerView:(UIView *)containerView fromView:(UIVi
131140
maskLayerAnimation.delegate = self;
132141
[maskLayer addAnimation:maskLayerAnimation forKey:@"maskLayer"];
133142

134-
if (self.appearingAnimation == ALNavigationCoordinatorAnimationNone)
135-
{
136-
return;
137-
}
143+
// create the dimming effect as the menu is hidden
144+
UIView *dimmingView = [self addDimmingViewToContainerView:containerView belowView:fromView];
145+
dimmingView.alpha = kDimmingViewMaxAlpha;
138146

139-
// animate the incoming view
140-
[self adjustAnchorPointAndPositionForView:toView];
147+
BOOL shouldAnimateTransform = self.appearingAnimation != ALNavigationCoordinatorAnimationNone;
141148

142-
toView.layer.transform = CATransform3DScale(CATransform3DIdentity, kExpandViewLayerScaleFactor, kExpandViewLayerScaleFactor, 1.f);
149+
if (shouldAnimateTransform)
150+
{
151+
// animate the incoming view
152+
[self adjustAnchorPointAndPositionForView:toView];
153+
154+
toView.layer.transform = CATransform3DScale(CATransform3DIdentity, kExpandViewLayerScaleFactor, kExpandViewLayerScaleFactor, 1.f);
155+
}
143156

144157
[UIView animateWithDuration:maskLayerAnimation.duration
145158
delay:0.
146159
usingSpringWithDamping:kUIKSpringDamping
147160
initialSpringVelocity:kUIKSpringVelocity
148161
options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState
149162
animations:^{
150-
toView.transform = CGAffineTransformIdentity;
163+
if (shouldAnimateTransform)
164+
{
165+
toView.transform = CGAffineTransformIdentity;
166+
}
167+
dimmingView.alpha = 0.f;
151168
}
152169
completion:nil];
153170

@@ -156,6 +173,22 @@ - (void)animateDismissalWithContainerView:(UIView *)containerView fromView:(UIVi
156173
//
157174
}
158175

176+
#pragma mark - Helper methods
177+
178+
- (UIView *)addDimmingViewToContainerView:(UIView *)containerView belowView:(UIView *)view
179+
{
180+
// create the dimming view, which will fade in/out as the menu is shown/hidden
181+
UIView *dimmingView = [[UIView alloc] init];
182+
dimmingView.translatesAutoresizingMaskIntoConstraints = NO;
183+
dimmingView.backgroundColor = [UIColor blackColor];
184+
185+
// then add and constrain
186+
[containerView insertSubview:dimmingView belowSubview:view];
187+
[dimmingView al_pinToSuperview];
188+
189+
return dimmingView;
190+
}
191+
159192
- (CGFloat)scaleForView:(UIView *)view
160193
{
161194
CAShapeLayer *initialShapeLayer = self.initialShapeLayer;

0 commit comments

Comments
 (0)