Skip to content

Commit

Permalink
feat: add shadow config for present content view
Browse files Browse the repository at this point in the history
  • Loading branch information
HeathWang committed Oct 24, 2019
1 parent 6eddb2f commit d71cd23
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Demo/PanControllerExample/Base/HWBaseViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ - (BOOL)anchorModalToLongForm {
return NO;
}

- (HWPanModalShadow)contentShadow {
return PanModalShadowMake([UIColor yellowColor], 10, CGSizeMake(0, 2), 1);
}

- (UIViewAnimationOptions)transitionAnimationOptions {
return UIViewAnimationOptionCurveLinear;
}
Expand Down
4 changes: 4 additions & 0 deletions Demo/PanControllerExample/Base/HWGroupViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ - (UIScrollView *)panScrollable {
return self.tableView;
}

- (BOOL)showsScrollableVerticalScrollIndicator {
return NO;
}

#pragma mark - Getter

- (NSArray<UIColor *> *)colors {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ - (PanModalHeight)shortFormHeight {
return PanModalHeightMake(PanModalHeightTypeContent, 200);
}

- (CGFloat)topOffset {
return [UIApplication sharedApplication].keyWindow.rootViewController.topLayoutGuide.length + 21;
}

- (BOOL)shouldRoundTopCorners {
return YES;
}
Expand Down
2 changes: 1 addition & 1 deletion HWPanModal.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'HWPanModal'
s.version = '0.6.0'
s.version = '0.6.1'
s.summary = 'HWPanModal is used to present controller and drag to dismiss.'

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Latest</string>
<string>Original</string>
<key>PreviewsEnabled</key>
<false/>
</dict>
</plist>
13 changes: 12 additions & 1 deletion Sources/Animator/HWPanModalPresentationAnimator.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,20 @@ - (void)animateDismissal:(id<UIViewControllerContextTransitioning>)context {
[context completeTransition:finished];
}];
} else {

CGFloat offsetY = 0;
HWPanModalShadow shadowConfig = [presentable contentShadow];
if (shadowConfig.shadowColor) {
// we should make the panView move further to hide the shadow effect.
offsetY = offsetY + shadowConfig.shadowRadius + shadowConfig.shadowOffset.height;
if ([presentable showDragIndicator]) {
offsetY += [presentable customIndicatorView] ? [presentable customIndicatorView].indicatorSize.height : 13;
}
}

[HWPanModalAnimator animate:^{
[self dismissAnimationForPresentingVC:presentable];
panView.hw_top = context.containerView.frame.size.height;
panView.hw_top = (context.containerView.frame.size.height + offsetY);
} config:presentable completion:^(BOOL completion) {
[fromVC.view removeFromSuperview];
[self endAppearanceTransitionForController:fromVC];
Expand Down
9 changes: 9 additions & 0 deletions Sources/Controller/HWPanModalPresentationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ - (void)layoutPresentedView:(UIView *)containerView {
if ([self.presentable showDragIndicator]) {
[self addDragIndicatorViewToView:self.panContainerView];
}

[self addShadowToContainerView];

[self setNeedsLayoutUpdate];
[self adjustPanContainerBackgroundColor];
Expand Down Expand Up @@ -268,6 +270,13 @@ - (void)addRoundedCornersToView:(UIView *)view {
view.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

- (void)addShadowToContainerView {
HWPanModalShadow shadow = [[self presentable] contentShadow];
if (shadow.shadowColor) {
[self.panContainerView updateShadow:shadow.shadowColor shadowRadius:shadow.shadowRadius shadowOffset:shadow.shadowOffset shadowOpacity:shadow.shadowOpacity];
}
}

- (void)snapToYPos:(CGFloat)yPos {
[HWPanModalAnimator animate:^{
self.isPresentedViewAnimating = YES;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mediator/HWPanModalPresentableHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ - (void)observeScrollable {
*/
- (void)trackScrolling:(UIScrollView *)scrollView {
self.scrollViewYOffset = MAX(scrollView.contentOffset.y, -(MAX(scrollView.contentInset.top, 0)));
scrollView.showsVerticalScrollIndicator = [[self presentable] showsScrollableHorizontalScrollIndicator];
scrollView.showsVerticalScrollIndicator = [[self presentable] showsScrollableVerticalScrollIndicator];
}

/**
Expand Down
18 changes: 18 additions & 0 deletions Sources/Presentable/HWPanModalHeight.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ CG_INLINE PanModalHeight PanModalHeightMake(PanModalHeightType heightType, CGFlo
return modalHeight;
}

struct HWPanModalShadow {
UIColor *shadowColor;
CGFloat shadowRadius;
CGSize shadowOffset;
float shadowOpacity;
};

typedef struct HWPanModalShadow HWPanModalShadow;

CG_INLINE HWPanModalShadow PanModalShadowMake(UIColor *shadowColor, CGFloat shadowRadius, CGSize shadowOffset, float shadowOpacity) {
HWPanModalShadow shadowConfig;
shadowConfig.shadowColor = shadowColor;
shadowConfig.shadowRadius = shadowRadius;
shadowConfig.shadowOffset = shadowOffset;
shadowConfig.shadowOpacity = shadowOpacity;
return shadowConfig;
}

static inline BOOL HW_FLOAT_IS_ZERO(CGFloat value) {
return (value > -FLT_EPSILON) && (value < FLT_EPSILON);
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/Presentable/HWPanModalPresentable.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
- (UIEdgeInsets)scrollIndicatorInsets;

/**
* A Boolean value that controls whether the scrollable horizontal scroll indicator is visible.
* A Boolean value that controls whether the scrollable vertical scroll indicator is visible.
* default is YES.
*/
- (BOOL)showsScrollableHorizontalScrollIndicator;
- (BOOL)showsScrollableVerticalScrollIndicator;

/**
* 是否允许拖动额外拖动,如果panScrollable存在,且scrollView contentSize > (size + bottomLayoutOffset),返回YES
Expand Down Expand Up @@ -244,6 +244,12 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
*/
- (CGFloat)cornerRadius;

/**
* presented content shadow
* Default is None config
*/
- (HWPanModalShadow)contentShadow;

#pragma mark - Indicator config

/**
Expand Down
6 changes: 5 additions & 1 deletion Sources/Presentable/UIViewController+PanModalDefault.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ - (UIEdgeInsets)scrollIndicatorInsets {
return UIEdgeInsetsMake(top, 0, self.bottomLayoutOffset, 0);
}

- (BOOL)showsScrollableHorizontalScrollIndicator {
- (BOOL)showsScrollableVerticalScrollIndicator {
return YES;
}

Expand Down Expand Up @@ -134,6 +134,10 @@ - (CGFloat)cornerRadius {
return 8;
}

- (HWPanModalShadow)contentShadow {
return PanModalShadowMake(nil, 0, CGSizeZero, 0);
}

- (BOOL)showDragIndicator {
if ([self allowsTouchEventsPassingThroughTransitionView]) {
return NO;
Expand Down
6 changes: 6 additions & 0 deletions Sources/View/HWPanContainerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong, readonly) UIView *contentView;

- (instancetype)initWithPresentedView:(UIView *)presentedView frame:(CGRect)frame;

- (void)updateShadow:(UIColor *)shadowColor
shadowRadius:(CGFloat)shadowRadius
shadowOffset:(CGSize)shadowOffset
shadowOpacity:(float)shadowOpacity;

@end

@interface UIView (PanContainer)
Expand Down
11 changes: 10 additions & 1 deletion Sources/View/HWPanContainerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@interface HWPanContainerView ()

@property (nonatomic, strong) UIView *contentView;

@end

@implementation HWPanContainerView
Expand All @@ -18,15 +19,23 @@ - (instancetype)initWithPresentedView:(UIView *)presentedView frame:(CGRect)fram
self = [super initWithFrame:frame];
if (self) {
_contentView = [UIView new];

_contentView.frame = self.bounds;
[self addSubview:_contentView];

[_contentView addSubview:presentedView];
}

return self;
}

- (void)updateShadow:(UIColor *)shadowColor shadowRadius:(CGFloat)shadowRadius shadowOffset:(CGSize)shadowOffset shadowOpacity:(float)shadowOpacity {

self.layer.shadowColor = shadowColor.CGColor;
self.layer.shadowRadius = shadowRadius;
self.layer.shadowOffset = shadowOffset;
self.layer.shadowOpacity = shadowOpacity;
}

@end

@implementation UIView (PanContainer)
Expand Down
2 changes: 2 additions & 0 deletions Sources/View/PanModal/HWPanModalContainerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype)initWithPresentingView:(UIView *)presentingView contentView:(HWPanModalContentView<HWPanModalPresentable> *)contentView;

- (void)show;

- (void)setNeedsLayoutUpdate;

- (void)transitionToState:(PresentationState)state;
Expand Down
24 changes: 8 additions & 16 deletions Sources/View/PanModal/HWPanModalContainerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ - (instancetype)initWithPresentingView:(UIView *)presentingView contentView:(HWP
if (self) {
_presentingView = presentingView;
_contentView = contentView;

[self prepare];
[self presentAnimationWillBegin];
[self beginPresentAnimation];
}

return self;
}

- (void)show {
[self prepare];
[self presentAnimationWillBegin];
[self beginPresentAnimation];
}

- (void)prepare {
[self.presentingView addSubview:self];
self.frame = self.presentingView.bounds;
Expand Down Expand Up @@ -290,20 +292,10 @@ - (void)dismiss:(BOOL)isInteractive mode:(PanModalInteractiveMode)mode {

[[self presentable] panModalWillDismiss];

CGFloat indicatorHeight = 0;
if ([[self presentable] showDragIndicator]) {
indicatorHeight += kIndicatorYOffset;
if ([[self presentable] customIndicatorView]) {
UIView<HWPanModalIndicatorProtocol> *customIndicator = [[self presentable] customIndicatorView];
indicatorHeight += [customIndicator indicatorSize].height;
} else {
indicatorHeight += 13;
}
}

[HWPanModalAnimator animate:^{
self.panContainerView.hw_top = CGRectGetHeight(self.bounds) + indicatorHeight;
self.panContainerView.hw_top = CGRectGetHeight(self.bounds);
self.backgroundView.dimState = DimStateOff;
self.dragIndicatorView.alpha = 0;
} config:[self presentable] completion:^(BOOL completion) {
[self removeFromSuperview];
[[self presentable] panModalDidDismissed];
Expand Down
1 change: 1 addition & 0 deletions Sources/View/PanModal/HWPanModalContentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

NS_ASSUME_NONNULL_BEGIN

/// when use `HWPanModalContentView`, you should take care of the safe area by yourself.
@interface HWPanModalContentView : UIView <HWPanModalPresentable, HWPanModalPresentationUpdateProtocol, HWPanModalPresentableLayoutProtocol>

/**
Expand Down
13 changes: 9 additions & 4 deletions Sources/View/PanModal/HWPanModalContentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ - (void)presentInView:(UIView *)view {
view = [UIApplication sharedApplication].keyWindow;
}
HWPanModalContainerView *containerView = [[HWPanModalContainerView alloc] initWithPresentingView:view contentView:self];
[containerView show];
}

#pragma mark - HWPanModalPresentationUpdateProtocol
Expand Down Expand Up @@ -95,7 +96,7 @@ - (UIEdgeInsets)scrollIndicatorInsets {
return UIEdgeInsetsMake(top, 0, self.bottomLayoutOffset, 0);
}

- (BOOL)showsScrollableHorizontalScrollIndicator {
- (BOOL)showsScrollableVerticalScrollIndicator {
return YES;
}

Expand Down Expand Up @@ -165,6 +166,10 @@ - (CGFloat)cornerRadius {
return 8;
}

- (HWPanModalShadow)contentShadow {
return PanModalShadowMake(nil, 0, CGSizeZero, 0);
}

- (BOOL)showDragIndicator {
if ([self allowsTouchEventsPassingThroughTransitionView]) {
return NO;
Expand Down Expand Up @@ -219,11 +224,11 @@ - (void)panModalDidDismissed {
#pragma mark - HWPanModalPresentableLayoutProtocol

- (CGFloat)topLayoutOffset {
return [UIApplication sharedApplication].keyWindow.rootViewController.topLayoutGuide.length;
return 0;
}

- (CGFloat)bottomLayoutOffset {
return [UIApplication sharedApplication].keyWindow.rootViewController.bottomLayoutGuide.length;
return 0;
}

- (CGFloat)shortFormYPos {
Expand Down Expand Up @@ -271,7 +276,7 @@ - (HWPanModalContainerView *)containerView {
UIView *fatherView = self.superview;
while (fatherView) {
if ([fatherView isKindOfClass:HWPanModalContainerView.class]) {
return (HWPanModalContentView *) fatherView;
return (HWPanModalContainerView *) fatherView;
}
fatherView = fatherView.superview;
}
Expand Down

0 comments on commit d71cd23

Please sign in to comment.