123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- //
- // SPSBActionSheetViewController.m
- // 我的社保
- //
- // Created by shanp on 2021/5/13.
- //
- #import "SPSBActionSheetViewController.h"
- #import "SPSBUIGeneralHeader.h"
- @interface SPSBActionSheetViewItem : NSObject
- @property (nonatomic, strong) NSString *spsb_title;
- @property (nonatomic, strong, nullable) UIColor *spsb_titleColor;
- @property (nonatomic, strong, nullable) UIFont *spsb_font;
- @property (nonatomic, strong) void(^spsb_action)(void);
- @end
- @implementation SPSBActionSheetViewItem
- @end
- @interface SPSBActionSheetViewController () {
- NSMutableArray<SPSBActionSheetViewItem *> *_items;
- UIView *_Nullable _topView;
- CGFloat _topViewHeight;
-
- UIView *_contentView;
- UIView *_cancelView;
- }
- @end
- @implementation SPSBActionSheetViewController
- - (instancetype)init {
- self = [super init];
- if (!self) return nil;
- _items = NSMutableArray.new;
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setupUI];
- }
- #define spsb_itemTag 4000
- #pragma mark - Action
- - (void)chosAction:(UIButton *)sender {
- if (sender.tag == 3000) {
- [self closeAction];
- return;
- }
- NSInteger index = sender.tag - spsb_itemTag;
- if (index >= _items.count) return;
- [self closeActionCompletion:^{
- self->_items[index].spsb_action();
- }];
- }
- - (void)closeAction {
- [self closeActionCompletion:nil];
- }
- - (void)closeActionCompletion: (void (^_Nullable)(void))completion {
- [_contentView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.update(0);
- }];
-
- [UIView animateWithDuration:0.3 animations:^{
- [self.view layoutSubviews];
- self.view.backgroundColor = spsb_000000_color(0);
- } completion:^(BOOL finished) {
- [self dismissViewControllerAnimated:false completion:completion];
- }];
- }
- - (void)addTopView:(UIView *)view height:(CGFloat)height {
- _topView = view;
- _topViewHeight = height;
- }
- - (void)addActionWithTitle:(NSString *)title action:(void(^)(void))action {
- [self addActionWithTitle:title titleColor:nil font:nil action:action];
- }
- - (void)addActionWithTitle:(NSString *)title titleColor:(UIColor *) titleColor action:(void(^)(void))action {
- [self addActionWithTitle:title titleColor:titleColor font:nil action:action];
- }
- - (void)addActionWithTitle:(NSString *)title titleColor:(nullable UIColor *) titleColor font:(nullable UIFont *)font action:(void(^)(void))action {
- SPSBActionSheetViewItem *item = SPSBActionSheetViewItem.new;
- item.spsb_title = title;
- item.spsb_titleColor = titleColor;
- item.spsb_font = font;
- item.spsb_action = action;
- [_items addObject:item];
- }
- - (void)viewDidLayoutSubviews {
- if (jxh_safeInsets(self.view).bottom == 0) {
- [_cancelView makeConstraints:^(JXHConstraintMaker *make) {
- make.bottom.equalTo(self->_contentView.safebottom).update(-16);
- }];
- }
- }
- #pragma mark - Overwrite
- - (void)showAnimation {
- [_contentView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.update(-jxh_viewHeight(self->_contentView));
- }];
- [UIView animateWithDuration:0.3 animations:^{
- [self.view layoutSubviews];
- self.view.backgroundColor = spsb_000000_color(SPSBPopupViewBackgroundAlpha);
- }];
- }
- - (void)setupAnimation {
- self.view.backgroundColor = spsb_000000_color(0.f);
- }
- #define spsb_buttonHeight 50
- #pragma mark - UI
- - (void)setupUI {
- [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction)]];
-
- _contentView = UIView.new;
- [self.view addSubview:_contentView];
-
- UIView *itemView = UIView.new;
- itemView.backgroundColor = spsb_FFFFFF_color(1.f);
- [_contentView addSubview:itemView];
- [itemView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.and.leading.and.trailing.equalTo(0);
- }];
-
- UIView *tempView = nil;
- if (_topView) {
- _contentView.backgroundColor = _topView.backgroundColor;
- [itemView addSubview:_topView];
- [_topView makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.and.trailing.and.top.equalTo(0);
- if (self->_topViewHeight > 0) {
- make.height.equalTo(self->_topViewHeight);
- }
- }];
- tempView = _topView;
- } else {
- _contentView.backgroundColor = spsb_F5F5F5_color();
- }
- NSInteger i = 0;
- for (SPSBActionSheetViewItem *item in _items) {
- UIButton *button = [self createButtonWithTitle:item.spsb_title titleColor:item.spsb_titleColor ?: spsb_000000_color(0.8) font:item.spsb_font ?: spsb_font(17) tag:spsb_itemTag + i];
- [itemView addSubview:button];
- [button makeConstraints:^(JXHConstraintMaker *make) {
- if (tempView) {
- make.top.equalTo(tempView.bottom);
- } else {
- make.top.equalTo(0);
- }
- make.leading.and.trailing.equalTo(0);
- make.height.equalTo(spsb_buttonHeight);
- }];
-
- if (i != _items.count - 1) {
- [button createLineWithLocation:JXHLineLocationBottom];
- }
- i ++;
- tempView = button;
- }
-
- if (tempView) {
- [tempView makeConstraints:^(JXHConstraintMaker *make) {
- make.bottom.equalTo(0);
- }];
- } else {
- [self dismissViewControllerAnimated:false completion:nil];
- }
-
- _cancelView = UIView.new;
- _cancelView.backgroundColor = spsb_FFFFFF_color(1.f);
- [_contentView addSubview:_cancelView];
- [_cancelView makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.and.trailing.equalTo(itemView);
- make.bottom.equalTo(self->_contentView.safebottom);
- make.top.equalTo(itemView.bottom).offset(10);
- }];
-
- UIButton *cancelButton = [self createButtonWithTitle:@"取消" titleColor:spsb_000000_color(0.8) font:spsb_font(17) tag:3000];
- [_cancelView addSubview:cancelButton];
- [cancelButton makeConstraints:^(JXHConstraintMaker *make) {
- make.edges.equalTo(0);
- make.height.equalTo(spsb_buttonHeight);
- }];
-
- [_contentView makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.and.trailing.equalTo(0);
- make.top.equalTo(self.view.bottom);
- }];
-
- }
- - (UIButton *)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font tag:(NSInteger)tag {
- UIButton *button = [UIButton convenienceWithFont:font target:self action:@selector(chosAction:)];
- button.tag = tag;
- [button setTitle:title titleColor:titleColor image:nil backgroundImage:jxh_createImage(spsb_clear_color(), (CGSize){spsb_buttonHeight, spsb_buttonHeight}) state:JXHButtonControlStateNormal];
- [button setBackgroundImage:jxh_createImage(spsb_000000_color(0.08), (CGSize){spsb_buttonHeight, spsb_buttonHeight}) state:JXHButtonControlStateHighlighted];
- return button;
- }
- @end
- #undef spsb_itemTag
- #undef spsb_buttonHeight
|