Skip to content

Commit 8174ef5

Browse files
committed
now we can dynamic update allowsTouchEventsPassingThroughTransitionView api.
1 parent 569aeb0 commit 8174ef5

10 files changed

+89
-19
lines changed

HWPanModal.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'HWPanModal'
11-
s.version = '0.8.5'
11+
s.version = '0.8.6'
1212
s.summary = 'HWPanModal is used to present controller and drag to dismiss.'
1313

1414
# This description is used to generate tags and improve search results.

Sources/Controller/HWPanModalPresentationController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ NS_ASSUME_NONNULL_BEGIN
1515
@interface HWPanModalPresentationController : UIPresentationController
1616

1717
@property (nonatomic, readonly) HWDimmedView *backgroundView;
18+
@property (nonatomic, readonly) PresentationState currentPresentationState;
1819

1920
- (void)setNeedsLayoutUpdate;
2021

22+
- (void)updateUserHitBehavior;
23+
2124
- (void)transitionToState:(PresentationState)state animated:(BOOL)animated;
2225

2326
- (void)setScrollableContentOffset:(CGPoint)offset animated:(BOOL)animated;

Sources/Controller/HWPanModalPresentationController.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ - (void)setScrollableContentOffset:(CGPoint)offset animated:(BOOL)animated {
194194
[self.handler setScrollableContentOffset:offset animated:animated];
195195
}
196196

197+
- (void)updateUserHitBehavior {
198+
[self checkVCContainerEventPass];
199+
[self checkBackgroundViewEventPass];
200+
}
201+
197202
#pragma mark - layout
198203

199204
- (void)adjustPresentedViewFrame {
@@ -327,6 +332,29 @@ - (void)configureViewLayout {
327332
self.containerView.userInteractionEnabled = [[self presentable] isUserInteractionEnabled];
328333
}
329334

335+
#pragma mark - event passing through
336+
337+
- (void)checkVCContainerEventPass {
338+
BOOL eventPassValue = [[self presentable] allowsTouchEventsPassingThroughTransitionView];
339+
// hack TransitionView
340+
[self.containerView setValue:@(eventPassValue) forKey:@"ignoreDirectTouchEvents"];
341+
}
342+
343+
- (void)checkBackgroundViewEventPass {
344+
if ([[self presentable] allowsTouchEventsPassingThroughTransitionView]) {
345+
self.backgroundView.userInteractionEnabled = NO;
346+
self.backgroundView.tapBlock = nil;
347+
} else {
348+
self.backgroundView.userInteractionEnabled = YES;
349+
__weak typeof(self) wkSelf = self;
350+
self.backgroundView.tapBlock = ^(UITapGestureRecognizer *recognizer) {
351+
if ([[wkSelf presentable] allowsTapBackgroundToDismiss]) {
352+
[wkSelf dismiss:NO mode:PanModalInteractiveModeNone];
353+
}
354+
};
355+
}
356+
}
357+
330358
#pragma mark - y position update
331359

332360
- (void)snapToYPos:(CGFloat)yPos animated:(BOOL)animated {

Sources/Presentable/HWPanModalPresentable.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,10 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
199199

200200
/**
201201
* 是否允许触摸事件透传到presenting ViewController/View。如果你有特殊需求的话(比如弹出一个底部视图,但是你想操作弹出视图下面的view,即presenting VC/View),可开启此功能
202-
* 注意开启此功能,拖拽指示器会默认隐藏,背景色alpha默认为0.
203202
*
204203
* Whether allows touch events passing through the transition container view.
205204
* 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.
206205
*
207-
* Note: When allows, the background view alpha = 0, and the drag indicator will be hidden.
208-
* @return Default is NO.
209-
*
210206
* Note: You SHOULD MUST dismiss the presented VC in the right time.
211207
*/
212208
- (BOOL)allowsTouchEventsPassingThroughTransitionView;

Sources/Presentable/HWPanModalPresentationUpdateProtocol.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
@property (nonatomic, readonly) UIView *hw_rootContainerView;
1616
/// which view that your presented viewController's view has been added.
1717
@property (nonatomic, readonly) UIView *hw_contentView;
18+
/// current presentation State
19+
@property (nonatomic, readonly) PresentationState hw_presentationState;
1820
/**
1921
* force update pan modal State, short/long
2022
*/
@@ -47,4 +49,9 @@
4749
*/
4850
- (void)hw_panModalSetNeedsLayoutUpdate NS_SWIFT_NAME(panModalSetNeedsLayoutUpdate());
4951

52+
/**
53+
* 更新用户行为,比如事件穿透
54+
*/
55+
- (void)hw_panModalUpdateUserHitBehavior NS_SWIFT_NAME(panModalUpdateUserHitBehavior());
56+
5057
@end

Sources/Presentable/UIViewController+PanModalDefault.m

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ - (UIViewAnimationOptions)transitionAnimationOptions {
5656
}
5757

5858
- (CGFloat)backgroundAlpha {
59-
// set the background alpha = 0 when allows touch events passing through
60-
if ([self allowsTouchEventsPassingThroughTransitionView]) {
61-
return 0;
62-
}
6359
return 0.7;
6460
}
6561

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

7470
- (HWBackgroundConfig *)backgroundConfig {
75-
// set the background alpha = 0 when allows touch events passing through
76-
if ([self allowsTouchEventsPassingThroughTransitionView]) {
77-
HWBackgroundConfig *config = [HWBackgroundConfig configWithBehavior:HWBackgroundBehaviorDefault];
78-
config.backgroundAlpha = 0;
79-
return config;
80-
}
81-
8271
return [HWBackgroundConfig configWithBehavior:HWBackgroundBehaviorDefault];
8372
}
8473

Sources/Presentable/UIViewController+Presentation.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ - (void)hw_panModalSetNeedsLayoutUpdate {
4141
[self.hw_presentedVC setNeedsLayoutUpdate];
4242
}
4343

44+
- (void)hw_panModalUpdateUserHitBehavior {
45+
if (!self.hw_presentedVC) return;
46+
[self.hw_presentedVC updateUserHitBehavior];
47+
}
48+
4449
- (HWDimmedView *)hw_dimmedView {
4550
return self.hw_presentedVC.backgroundView;
4651
}
@@ -53,4 +58,8 @@ - (UIView *)hw_contentView {
5358
return self.hw_presentedVC.presentedView;
5459
}
5560

61+
- (PresentationState)hw_presentationState {
62+
return self.hw_presentedVC.currentPresentationState;
63+
}
64+
5665
@end

Sources/View/PanModal/HWPanModalContainerView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
1818

1919
@property (nonatomic, readonly) HWDimmedView *backgroundView;
2020
@property (readonly) HWPanContainerView *panContainerView;
21+
@property (nonatomic, readonly) PresentationState currentPresentationState;
2122

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

@@ -27,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN
2728

2829
- (void)setNeedsLayoutUpdate;
2930

31+
- (void)updateUserHitBehavior;
32+
3033
- (void)transitionToState:(PresentationState)state animated:(BOOL)animated;
3134

3235
- (void)setScrollableContentOffset:(CGPoint)offset animated:(BOOL)animated;

Sources/View/PanModal/HWPanModalContainerView.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ - (void)setNeedsLayoutUpdate {
158158
[self updateRoundedCorners];
159159
}
160160

161+
- (void)updateUserHitBehavior {
162+
[self checkBackgroundViewEventPass];
163+
[self checkPanGestureRecognizer];
164+
}
165+
161166
- (void)transitionToState:(PresentationState)state animated:(BOOL)animated {
162167

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

436+
- (void)checkBackgroundViewEventPass {
437+
if ([[self presentable] allowsTouchEventsPassingThroughTransitionView]) {
438+
self.backgroundView.userInteractionEnabled = NO;
439+
self.backgroundView.tapBlock = nil;
440+
} else {
441+
self.backgroundView.userInteractionEnabled = YES;
442+
__weak typeof(self) wkSelf = self;
443+
self.backgroundView.tapBlock = ^(UITapGestureRecognizer *recognizer) {
444+
if ([[wkSelf presentable] allowsTapBackgroundToDismiss]) {
445+
[wkSelf dismiss:NO mode:PanModalInteractiveModeNone];
446+
}
447+
};
448+
}
449+
}
450+
451+
- (void)checkPanGestureRecognizer {
452+
if ([[self presentable] allowsTouchEventsPassingThroughTransitionView]) {
453+
[self removeGestureRecognizer:self.handler.panGestureRecognizer];
454+
[self.panContainerView addGestureRecognizer:self.handler.panGestureRecognizer];
455+
} else {
456+
[self.panContainerView removeGestureRecognizer:self.handler.panGestureRecognizer];
457+
[self addGestureRecognizer:self.handler.panGestureRecognizer];
458+
}
459+
}
460+
431461
#pragma mark - getter
432462

433463
- (id<HWPanModalPresentable>)presentable {

Sources/View/PanModal/HWPanModalContentView.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ - (void)hw_panModalSetNeedsLayoutUpdate {
4444
[self.containerView setNeedsLayoutUpdate];
4545
}
4646

47+
- (void)hw_panModalUpdateUserHitBehavior {
48+
[self.containerView updateUserHitBehavior];
49+
}
50+
4751
- (void)hw_panModalTransitionTo:(PresentationState)state animated:(BOOL)animated {
4852
[self.containerView transitionToState:state animated:animated];
4953
}
@@ -64,6 +68,10 @@ - (UIView *)hw_contentView {
6468
return (UIView *)self.containerView.panContainerView;
6569
}
6670

71+
- (PresentationState)hw_presentationState {
72+
return self.containerView.currentPresentationState;
73+
}
74+
6775
#pragma mark - HWPanModalPresentable
6876

6977
- (UIScrollView *)panScrollable {
@@ -112,9 +120,6 @@ - (UIViewAnimationOptions)transitionAnimationOptions {
112120
}
113121

114122
- (CGFloat)backgroundAlpha {
115-
if ([self allowsTouchEventsPassingThroughTransitionView]) {
116-
return 0;
117-
}
118123
return 0.7;
119124
}
120125

0 commit comments

Comments
 (0)