Skip to content

Commit

Permalink
now we can dynamic update allowsTouchEventsPassingThroughTransitionVi…
Browse files Browse the repository at this point in the history
…ew api.
  • Loading branch information
HeathWang committed Jul 12, 2021
1 parent 569aeb0 commit 8174ef5
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 19 deletions.
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.8.5'
s.version = '0.8.6'
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
3 changes: 3 additions & 0 deletions Sources/Controller/HWPanModalPresentationController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ NS_ASSUME_NONNULL_BEGIN
@interface HWPanModalPresentationController : UIPresentationController

@property (nonatomic, readonly) HWDimmedView *backgroundView;
@property (nonatomic, readonly) PresentationState currentPresentationState;

- (void)setNeedsLayoutUpdate;

- (void)updateUserHitBehavior;

- (void)transitionToState:(PresentationState)state animated:(BOOL)animated;

- (void)setScrollableContentOffset:(CGPoint)offset animated:(BOOL)animated;
Expand Down
28 changes: 28 additions & 0 deletions Sources/Controller/HWPanModalPresentationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ - (void)setScrollableContentOffset:(CGPoint)offset animated:(BOOL)animated {
[self.handler setScrollableContentOffset:offset animated:animated];
}

- (void)updateUserHitBehavior {
[self checkVCContainerEventPass];
[self checkBackgroundViewEventPass];
}

#pragma mark - layout

- (void)adjustPresentedViewFrame {
Expand Down Expand Up @@ -327,6 +332,29 @@ - (void)configureViewLayout {
self.containerView.userInteractionEnabled = [[self presentable] isUserInteractionEnabled];
}

#pragma mark - event passing through

- (void)checkVCContainerEventPass {
BOOL eventPassValue = [[self presentable] allowsTouchEventsPassingThroughTransitionView];
// hack TransitionView
[self.containerView setValue:@(eventPassValue) forKey:@"ignoreDirectTouchEvents"];
}

- (void)checkBackgroundViewEventPass {
if ([[self presentable] allowsTouchEventsPassingThroughTransitionView]) {
self.backgroundView.userInteractionEnabled = NO;
self.backgroundView.tapBlock = nil;
} else {
self.backgroundView.userInteractionEnabled = YES;
__weak typeof(self) wkSelf = self;
self.backgroundView.tapBlock = ^(UITapGestureRecognizer *recognizer) {
if ([[wkSelf presentable] allowsTapBackgroundToDismiss]) {
[wkSelf dismiss:NO mode:PanModalInteractiveModeNone];
}
};
}
}

#pragma mark - y position update

- (void)snapToYPos:(CGFloat)yPos animated:(BOOL)animated {
Expand Down
4 changes: 0 additions & 4 deletions Sources/Presentable/HWPanModalPresentable.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,10 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {

/**
* 是否允许触摸事件透传到presenting ViewController/View。如果你有特殊需求的话(比如弹出一个底部视图,但是你想操作弹出视图下面的view,即presenting VC/View),可开启此功能
* 注意开启此功能,拖拽指示器会默认隐藏,背景色alpha默认为0.
*
* Whether allows touch events passing through the transition container view.
* In some situations, you present the bottom VC/View, and you want to operate the presenting VC/View(mapView, scrollView and etc), enable this func.
*
* Note: When allows, the background view alpha = 0, and the drag indicator will be hidden.
* @return Default is NO.
*
* Note: You SHOULD MUST dismiss the presented VC in the right time.
*/
- (BOOL)allowsTouchEventsPassingThroughTransitionView;
Expand Down
7 changes: 7 additions & 0 deletions Sources/Presentable/HWPanModalPresentationUpdateProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@property (nonatomic, readonly) UIView *hw_rootContainerView;
/// which view that your presented viewController's view has been added.
@property (nonatomic, readonly) UIView *hw_contentView;
/// current presentation State
@property (nonatomic, readonly) PresentationState hw_presentationState;
/**
* force update pan modal State, short/long
*/
Expand Down Expand Up @@ -47,4 +49,9 @@
*/
- (void)hw_panModalSetNeedsLayoutUpdate NS_SWIFT_NAME(panModalSetNeedsLayoutUpdate());

/**
* 更新用户行为,比如事件穿透
*/
- (void)hw_panModalUpdateUserHitBehavior NS_SWIFT_NAME(panModalUpdateUserHitBehavior());

@end
11 changes: 0 additions & 11 deletions Sources/Presentable/UIViewController+PanModalDefault.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ - (UIViewAnimationOptions)transitionAnimationOptions {
}

- (CGFloat)backgroundAlpha {
// set the background alpha = 0 when allows touch events passing through
if ([self allowsTouchEventsPassingThroughTransitionView]) {
return 0;
}
return 0.7;
}

Expand All @@ -72,13 +68,6 @@ - (nonnull UIColor *)backgroundBlurColor {
}

- (HWBackgroundConfig *)backgroundConfig {
// set the background alpha = 0 when allows touch events passing through
if ([self allowsTouchEventsPassingThroughTransitionView]) {
HWBackgroundConfig *config = [HWBackgroundConfig configWithBehavior:HWBackgroundBehaviorDefault];
config.backgroundAlpha = 0;
return config;
}

return [HWBackgroundConfig configWithBehavior:HWBackgroundBehaviorDefault];
}

Expand Down
9 changes: 9 additions & 0 deletions Sources/Presentable/UIViewController+Presentation.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ - (void)hw_panModalSetNeedsLayoutUpdate {
[self.hw_presentedVC setNeedsLayoutUpdate];
}

- (void)hw_panModalUpdateUserHitBehavior {
if (!self.hw_presentedVC) return;
[self.hw_presentedVC updateUserHitBehavior];
}

- (HWDimmedView *)hw_dimmedView {
return self.hw_presentedVC.backgroundView;
}
Expand All @@ -53,4 +58,8 @@ - (UIView *)hw_contentView {
return self.hw_presentedVC.presentedView;
}

- (PresentationState)hw_presentationState {
return self.hw_presentedVC.currentPresentationState;
}

@end
3 changes: 3 additions & 0 deletions Sources/View/PanModal/HWPanModalContainerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly) HWDimmedView *backgroundView;
@property (readonly) HWPanContainerView *panContainerView;
@property (nonatomic, readonly) PresentationState currentPresentationState;

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

Expand All @@ -27,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)setNeedsLayoutUpdate;

- (void)updateUserHitBehavior;

- (void)transitionToState:(PresentationState)state animated:(BOOL)animated;

- (void)setScrollableContentOffset:(CGPoint)offset animated:(BOOL)animated;
Expand Down
30 changes: 30 additions & 0 deletions Sources/View/PanModal/HWPanModalContainerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ - (void)setNeedsLayoutUpdate {
[self updateRoundedCorners];
}

- (void)updateUserHitBehavior {
[self checkBackgroundViewEventPass];
[self checkPanGestureRecognizer];
}

- (void)transitionToState:(PresentationState)state animated:(BOOL)animated {

if (![self.presentable shouldTransitionToState:state]) return;
Expand Down Expand Up @@ -428,6 +433,31 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
}
}

- (void)checkBackgroundViewEventPass {
if ([[self presentable] allowsTouchEventsPassingThroughTransitionView]) {
self.backgroundView.userInteractionEnabled = NO;
self.backgroundView.tapBlock = nil;
} else {
self.backgroundView.userInteractionEnabled = YES;
__weak typeof(self) wkSelf = self;
self.backgroundView.tapBlock = ^(UITapGestureRecognizer *recognizer) {
if ([[wkSelf presentable] allowsTapBackgroundToDismiss]) {
[wkSelf dismiss:NO mode:PanModalInteractiveModeNone];
}
};
}
}

- (void)checkPanGestureRecognizer {
if ([[self presentable] allowsTouchEventsPassingThroughTransitionView]) {
[self removeGestureRecognizer:self.handler.panGestureRecognizer];
[self.panContainerView addGestureRecognizer:self.handler.panGestureRecognizer];
} else {
[self.panContainerView removeGestureRecognizer:self.handler.panGestureRecognizer];
[self addGestureRecognizer:self.handler.panGestureRecognizer];
}
}

#pragma mark - getter

- (id<HWPanModalPresentable>)presentable {
Expand Down
11 changes: 8 additions & 3 deletions Sources/View/PanModal/HWPanModalContentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ - (void)hw_panModalSetNeedsLayoutUpdate {
[self.containerView setNeedsLayoutUpdate];
}

- (void)hw_panModalUpdateUserHitBehavior {
[self.containerView updateUserHitBehavior];
}

- (void)hw_panModalTransitionTo:(PresentationState)state animated:(BOOL)animated {
[self.containerView transitionToState:state animated:animated];
}
Expand All @@ -64,6 +68,10 @@ - (UIView *)hw_contentView {
return (UIView *)self.containerView.panContainerView;
}

- (PresentationState)hw_presentationState {
return self.containerView.currentPresentationState;
}

#pragma mark - HWPanModalPresentable

- (UIScrollView *)panScrollable {
Expand Down Expand Up @@ -112,9 +120,6 @@ - (UIViewAnimationOptions)transitionAnimationOptions {
}

- (CGFloat)backgroundAlpha {
if ([self allowsTouchEventsPassingThroughTransitionView]) {
return 0;
}
return 0.7;
}

Expand Down

0 comments on commit 8174ef5

Please sign in to comment.