Skip to content

Latest commit

 

History

History
263 lines (197 loc) · 8.1 KB

README-CN.md

File metadata and controls

263 lines (197 loc) · 8.1 KB

HWPanModal 👍

Carthage compatible

HWPanModal 用于从底部弹出控制器(UIViewController),并用拖拽手势来关闭控制器。提供了自定义视图大小和位置,高度自定义弹出视图的各个属性。

APP中常见的从底部弹出视图,比如知乎APP的查看评论、抖音的评论查看、弹出分享等,可以通过该框架快速实现,只需专注于相应的视图编写。

截图

Basic Blur background Keyboard handle App demo

功能

  1. 支持任意类型的 UIViewController
  2. 平滑的转场动画
  3. 支持2种类型的手势操作
    1. UIPanGestureRecognizer, 上下拖拽视图
    2. UIScreenEdgePanGestureRecognizer, 侧滑关闭视图。
  4. 支持为presenting VC编写自定义动画。
  5. 支持配置动画时间,动画options,弹性spring值
  6. 支持配置背景alpha,或者高斯模糊背景。注意:动态调整模糊效果仅工作于iOS9.0+。
  7. 支持显示隐藏指示器,修改圆角
  8. 自动处理键盘弹出消失事件。
  9. 自定义指示器indicator view。

更多配置信息请参阅 HWPanModalPresentable.h 声明。

适配

iOS 8.0+, support Objective-C & Swift.

依赖

KVOController - facebook

安装

pod 'HWPanModal', '~> 0.3.4'

如何使用

如何从底部弹出控制器

只需要视图控制器适配 HWPanModalPresentable 协议即可. 默认情况下,不用重写适配的各个方法,如果需要自定义,请实现协议方法。

更多的自定义UI配置,请参见HWPanModalPresentable协议中每个方法的说明。

#import <HWPanModal/HWPanModal.h>
@interface HWBaseViewController () <HWPanModalPresentable>

@end

@implementation HWBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HWPanModalPresentable
- (PanModalHeight)longFormHeight {
    return PanModalHeightMake(PanModalHeightTypeMaxTopInset, 44);
}
@end

弹出控制器:

#import <HWPanModal/HWPanModal.h>
[self presentPanModal:[HWBaseViewController new]];

就是这么简单。

如何主动更新控制器UI。

请查阅 UIViewController+Presentation.h,里面有详细说明。

  • Change the state between short and long form. call - (void)hw_panModalTransitionTo:(PresentationState)state;
  • Change ScrollView ContentOffset. call - (void)hw_panModalSetContentOffset:(CGPoint)offset;
  • Reload layout. call - (void)hw_panModalSetNeedsLayoutUpdate;
    • 注意:如果scrollable view的contentSize改变了,你必须调用改reload方法来更新UI。

自定义presenting VC动画编写

  1. Create object conforms HWPresentingViewControllerAnimatedTransitioning .

    @interface HWMyCustomAnimation : NSObject <HWPresentingViewControllerAnimatedTransitioning>
    
    @end
    
    @implementation HWMyCustomAnimation
    
    
    - (void)presentAnimateTransition:(id<HWPresentingViewControllerContextTransitioning>)transitionContext {
        NSTimeInterval duration = [transitionContext mainTransitionDuration];
        UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        // replace it.
        [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            fromVC.view.transform = CGAffineTransformMakeScale(0.95, 0.95);
        } completion:^(BOOL finished) {
            
        }];
    }
    
    - (void)dismissAnimateTransition:(id<HWPresentingViewControllerContextTransitioning>)transitionContext {
        NSTimeInterval duration = [transitionContext mainTransitionDuration];
        UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        // replace it.
        [UIView animateWithDuration:duration animations:^{
            toVC.view.transform = CGAffineTransformIdentity;
        }];
    }
    
    @end
  2. Overwrite below two method.

    - (BOOL)shouldAnimatePresentingVC {
        return YES;
    }
    
    - (id<HWPresentingViewControllerAnimatedTransitioning>)customPresentingVCAnimation {
        return self.customAnimation;
    }
    
    - (HWMyCustomAnimation *)customAnimation {
        if (!_customAnimation) {
            _customAnimation = [HWMyCustomAnimation new];
        }
        return _customAnimation;
    }

自定义指示器indicator view

You just need to create your own UIView, then adopt HWPanModalIndicatorProtocol.

In your presented controller, return it:

- (nullable UIView <HWPanModalIndicatorProtocol> *)customIndicatorView {
    HWTextIndicatorView *textIndicatorView = [HWTextIndicatorView new];
    return textIndicatorView;
}

Here is HWTextIndicatorView code:

@interface HWTextIndicatorView : UIView <HWPanModalIndicatorProtocol>

@end

@interface HWTextIndicatorView ()
@property (nonatomic, strong) UILabel *stateLabel;
@end

@implementation HWTextIndicatorView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // init the _stateLabel
        [self addSubview:_stateLabel];
    }
    return self;
}


- (void)didChangeToState:(HWIndicatorState)state {
    switch (state) {
        case HWIndicatorStateNormal: {
            self.stateLabel.text = @"Please pull down to dismiss";
            self.stateLabel.textColor = [UIColor whiteColor];
        }
            break;
        case HWIndicatorStatePullDown: {
            self.stateLabel.text = @"Keep pull down to dismiss";
            self.stateLabel.textColor = [UIColor colorWithRed:1.000 green:0.200 blue:0.000 alpha:1.00];
        }
            break;
    }
}

- (CGSize)indicatorSize {
    return CGSizeMake(200, 18);
}

- (void)setupSubviews {
    self.stateLabel.frame = self.bounds;
}

@end

例子

  1. 克隆项目
  2. 然后执行 pod install
  3. 打开 HWPanModal.xcworkspace, 编译运行

联系我

Heath Wang [email protected]

License

HWPanModal is released under a MIT License. See LICENSE file for details.