Skip to content

Commit

Permalink
feat(EdgeInteractiion): Change screen edge interactive working for fu…
Browse files Browse the repository at this point in the history
…ll screen.
  • Loading branch information
HeathWang committed Sep 11, 2019
1 parent 4ff7c20 commit 8303965
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
Expand Up @@ -48,6 +48,10 @@ - (BOOL)allowScreenEdgeInteractive {
return YES;
}

- (CGFloat)maxAllowedDistanceToLeftScreenEdgeForPanInteraction {
return 64;
}

@end


Expand Down
63 changes: 45 additions & 18 deletions Sources/Controller/HWPanModalPresentationController.m
Expand Up @@ -56,7 +56,7 @@ @interface HWPanModalPresentationController () <UIGestureRecognizerDelegate>

// gesture
@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer *screenGestureRecognizer;
@property (nonatomic, strong) UIPanGestureRecognizer *screenEdgeGestureRecognizer;

// keyboard handle
@property (nonatomic, copy) NSDictionary *keyboardInfo;
Expand Down Expand Up @@ -283,7 +283,7 @@ - (void)layoutPresentedView:(UIView *)containerView {
[containerView addGestureRecognizer:self.panGestureRecognizer];

if ([self.presentable allowScreenEdgeInteractive]) {
[containerView addGestureRecognizer:self.screenGestureRecognizer];
[containerView addGestureRecognizer:self.screenEdgeGestureRecognizer];
}

if ([self.presentable shouldRoundTopCorners]) {
Expand Down Expand Up @@ -657,7 +657,7 @@ - (void)respondToPanGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecogn
}

- (BOOL)isVelocityWithinSensitivityRange:(CGFloat)velocity {
return (ABS(velocity) - (1000 * (1 - kSnapMovementSensitivity))) > 0;
return (fabsf(velocity) - (1000.00f * (1 - kSnapMovementSensitivity))) > 0;
}

- (CGFloat)nearestDistance:(CGFloat)position inDistances:(NSArray *)distances {
Expand All @@ -672,7 +672,7 @@ - (CGFloat)nearestDistance:(CGFloat)position inDistances:(NSArray *)distances {

[distances enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSNumber *number = obj;
NSNumber *absValue = @(fabs(number.floatValue - position));
NSNumber *absValue = @(fabsf(number.floatValue - position));
[tmpArr addObject:absValue];
[tmpDict setObject:number forKey:absValue];

Expand All @@ -695,10 +695,9 @@ - (void)dismissPresentedViewControllerIsInteractive:(BOOL)isInteractive mode:(Pa

#pragma mark - Screen Gesture enevt

- (void)screenEdgeInteractiveAction:(UIScreenEdgePanGestureRecognizer *)recognizer {
- (void)screenEdgeInteractiveAction:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:recognizer.view];
CGFloat percent = translation.x / CGRectGetWidth(recognizer.view.bounds);

switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
[self dismissPresentedViewControllerIsInteractive:YES mode:PanModalInteractiveModeSideslip];
Expand Down Expand Up @@ -729,12 +728,12 @@ - (void)checkEdgeInteractive {
UINavigationController *navigationController = (UINavigationController *) self.presentedViewController;
if ([[self presentable] allowScreenEdgeInteractive] &&
navigationController.topViewController == navigationController.viewControllers.firstObject) {
self.screenGestureRecognizer.enabled = YES;
self.screenEdgeGestureRecognizer.enabled = YES;
} else {
self.screenGestureRecognizer.enabled = NO;
self.screenEdgeGestureRecognizer.enabled = NO;
}
} else {
self.screenGestureRecognizer.enabled = [[self presentable] allowScreenEdgeInteractive];
self.screenEdgeGestureRecognizer.enabled = [[self presentable] allowScreenEdgeInteractive];
}
}

Expand Down Expand Up @@ -783,12 +782,41 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni
* 当前手势为screenGestureRecognizer时,其他pan recognizer都应该fail
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer == self.screenGestureRecognizer && [otherGestureRecognizer isKindOfClass:UIPanGestureRecognizer.class]) {
if (gestureRecognizer == self.screenEdgeGestureRecognizer && [otherGestureRecognizer isKindOfClass:UIPanGestureRecognizer.class]) {
return YES;
}
return NO;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.screenEdgeGestureRecognizer) {
CGPoint velocity = [self.screenEdgeGestureRecognizer velocityInView:self.screenEdgeGestureRecognizer.view];

if (velocity.x <= 0 || HW_TWO_FLOAT_IS_EQUAL(velocity.x, 0)) {
return NO;
}

// check the distance to left edge
CGPoint location = [self.screenEdgeGestureRecognizer locationInView:self.screenEdgeGestureRecognizer.view];
CGFloat thresholdDistance = [[self presentable] maxAllowedDistanceToLeftScreenEdgeForPanInteraction];
if (thresholdDistance > 0 && location.x > thresholdDistance) {
return NO;
}

if (velocity.x > 0 && HW_TWO_FLOAT_IS_EQUAL(velocity.y, 0)) {
return YES;
}

//TODO: this logic can be updated later.
if (velocity.x > 0 && velocity.x / fabsf(velocity.y) > 2) {
return YES;
}
return NO;
}

return YES;
}

#pragma mark - UIKeyboard Handle

- (void)addKeyboardObserver {
Expand Down Expand Up @@ -951,16 +979,15 @@ - (UIPanGestureRecognizer *)panGestureRecognizer {
return _panGestureRecognizer;
}

- (UIScreenEdgePanGestureRecognizer *)screenGestureRecognizer {
if (!_screenGestureRecognizer) {
_screenGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeInteractiveAction:)];
_screenGestureRecognizer.minimumNumberOfTouches = 1;
_screenGestureRecognizer.maximumNumberOfTouches = 1;
_screenGestureRecognizer.delegate = self;
_screenGestureRecognizer.edges = UIRectEdgeLeft;
- (UIPanGestureRecognizer *)screenEdgeGestureRecognizer {
if (!_screenEdgeGestureRecognizer) {
_screenEdgeGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeInteractiveAction:)];
_screenEdgeGestureRecognizer.minimumNumberOfTouches = 1;
_screenEdgeGestureRecognizer.maximumNumberOfTouches = 1;
_screenEdgeGestureRecognizer.delegate = self;
}

return _screenGestureRecognizer;
return _screenEdgeGestureRecognizer;
}

#pragma mark - dealloc
Expand Down
2 changes: 1 addition & 1 deletion Sources/Presentable/HWPanModalHeight.h
Expand Up @@ -38,7 +38,7 @@ static inline BOOL HW_FLOAT_IS_ZERO(CGFloat value) {
}

static inline BOOL HW_TWO_FLOAT_IS_EQUAL(CGFloat x, CGFloat y) {
CGFloat minusValue = fabs(x - y);
CGFloat minusValue = fabsf(x - y);
CGFloat criticalValue = 0.0001;
if (minusValue < criticalValue || minusValue < FLT_MIN) {
return YES;
Expand Down
31 changes: 22 additions & 9 deletions Sources/Presentable/HWPanModalPresentable.h
Expand Up @@ -31,8 +31,12 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {

/**
* HWPanModalPresentable为present配置协议
* 默认情况下无需实现,只需Controller conforms 该协议
* 通过category来默认实现以下所有方法。这样就不用通过继承来实现protocol
* 默认情况下无需实现,只需Controller适配该协议
* 通过category来默认实现以下所有方法,避免继承类
*
* This Protocol is the core of HWPanModal, we use it to config presentation.
* Default, you don't need to conform all of these methods, just implement what you want to customize.
* All the config has default value, we use a `UIViewController` category to conform `HWPanModalPresentable` protocol.
*/
@protocol HWPanModalPresentable <NSObject>

Expand Down Expand Up @@ -127,7 +131,7 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
*/
- (nonnull UIColor *)backgroundBlurColor;

#pragma mark - User operation
#pragma mark - User Interaction

/**
* 该bool值控制当pan View状态为long的情况下,是否可以继续拖拽到PanModalHeight = MAX的情况
Expand Down Expand Up @@ -160,6 +164,13 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
*/
- (BOOL)allowScreenEdgeInteractive;

/**
* Max allowed distance to screen left edge when you want to make screen edge pan interaction
* Default is 0, means it will ignore this limit, full screen left edge pan will work.
* @return distance to left screen edge
*/
- (CGFloat)maxAllowedDistanceToLeftScreenEdgeForPanInteraction;

/**
* 是否允许触觉反馈
* 默认为YES
Expand All @@ -175,12 +186,6 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
*/
- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle;

/**
* 是否对presentingViewController做动画效果,默认该效果类似淘宝/京东购物车凹陷效果
* 默认为NO
*/
- (BOOL)shouldAnimatePresentingVC DEPRECATED_MSG_ATTRIBUTE("This api has been marked as DEPRECATED on version 0.3.6, please use `- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle` replaced.");

/**
* 自定义presenting ViewController转场动画,默认为nil
* 注意:如果实现该方法并返回非空示例,要使该方法生效,`- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle`必须返回PresentingViewControllerAnimationStyleCustom
Expand Down Expand Up @@ -310,6 +315,14 @@ typedef NS_ENUM(NSInteger, PresentingViewControllerAnimationStyle) {
*/
- (void)panModalDidDismissed;

#pragma mark - DEPRECATED DECLARE

/**
* 是否对presentingViewController做动画效果,默认该效果类似淘宝/京东购物车凹陷效果
* 默认为NO
*/
- (BOOL)shouldAnimatePresentingVC DEPRECATED_MSG_ATTRIBUTE("This api has been marked as DEPRECATED on version 0.3.6, please use `- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle` replaced.");

@end

NS_ASSUME_NONNULL_END
Expand Down
4 changes: 4 additions & 0 deletions Sources/Presentable/UIViewController+PanModalDefault.m
Expand Up @@ -90,6 +90,10 @@ - (BOOL)allowScreenEdgeInteractive {
return NO;
}

- (CGFloat)maxAllowedDistanceToLeftScreenEdgeForPanInteraction {
return 0;
}

- (PresentingViewControllerAnimationStyle)presentingVCAnimationStyle {
return PresentingViewControllerAnimationStyleNone;
}
Expand Down

0 comments on commit 8303965

Please sign in to comment.