// // MTActionself.m // Meet // // Created by ko1o, on 2018/10/16. // Copyright © 2018年 Food. All rights reserved. // #import "MTActionSheet.h" #import "zhPopupController.h" @interface MTActionSheetButton () @end @implementation MTActionSheetButton + (instancetype)buttonWithType:(MTActionSheetButtonType)type title:(NSString *)title { MTActionSheetButton *button = [[MTActionSheetButton alloc] init]; button.titleLabel.font = NORMAL_FONT; button.height = FIX_SIZE(50); button.width = SCREEN_WIDTH; button.backgroundColor = [UIColor clearColor]; switch (type) { case MTActionSheetButtonTypeNormal: case MTActionSheetButtonTypeCancel: { [button setTitleColor:TITLE_COLOR forState:UIControlStateNormal]; [button setTitleColor:RGBA(0, 0, 0, 0.5) forState:UIControlStateHighlighted]; break; } case MTActionSheetButtonTypeDestructive: { [button setTitleColor:COLOR_WITH_RGB(0xFF4C73) forState:UIControlStateNormal]; [button setTitleColor:COLOR_WITH_RGB_A(0xFF4C73, 0.5) forState:UIControlStateHighlighted]; break; } default: break; } button.titleLabel.adjustsFontSizeToFitWidth = YES; [button setTitle:title forState:UIControlStateNormal]; return button; } @end #define KMTActionSheetOtherButtonsMargin FIX_SIZE(1) #define KMTActionSheetCancelAndOtherButtonMargin FIX_SIZE(10) @interface MTActionSheet () @property (nonatomic, strong, readonly) zhPopupController * popupController; @property (nonatomic, strong) MTActionSheetButton *cancelButton; @property (nonatomic, strong) MTActionSheetButton *destructiveButton; @property (nonatomic, weak) UIView *maxBottomView; // 方便计算sheet高度 @property (nonatomic, strong) NSMutableArray *otherTitles; @property (nonatomic, copy) NSString *cancelButtonTitle; @end @implementation MTActionSheet - (void)setupUI { self.backgroundColor = COLOR_WITH_RGB(0x222436); self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0); self.otherTitles = [NSMutableArray array]; } - (instancetype)initWithTitle:(nullable NSString *)title delegate:(nullable id)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION { if (self = [super init]) { [self setupUI]; self.delegate = delegate; if (title) { UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, FIX_SIZE(63)); titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor = [UIColor whiteColor]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.text = title; [self addSubview:titleLabel]; self.titleLabel = titleLabel; self.maxBottomView = titleLabel; } if (destructiveButtonTitle) { [self addButtonWithTitle:destructiveButtonTitle type:MTActionSheetButtonTypeDestructive]; } self.cancelButtonTitle = cancelButtonTitle; if (otherButtonTitles) { va_list va_list; va_start(va_list, otherButtonTitles); [self.otherTitles addObject:otherButtonTitles]; //添加第一个参数 id temp; //while循环 当取到nil的时候就停止了 while ((temp = va_arg(va_list, id))) { [self.otherTitles addObject:temp]; } } } return self; } - (NSInteger)addButtonWithTitle:(nullable NSString *)title { if (!title) title = @""; [self.otherTitles addObject:title]; NSInteger begainIndex = 0; if (self.destructiveButton) { begainIndex = 1; } return [self.otherTitles indexOfObject:title] + begainIndex; } - (MTActionSheetButton *)addButtonWithTitle:(nullable NSString *)title type:(MTActionSheetButtonType)type { MTActionSheetButton *button = [MTActionSheetButton buttonWithType:type title:title]; [button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside]; CGFloat margin = 0.0; switch (type) { case MTActionSheetButtonTypeNormal: margin = KMTActionSheetOtherButtonsMargin; break; case MTActionSheetButtonTypeDestructive: margin = KMTActionSheetOtherButtonsMargin; button.tag = 0; self.destructiveButton = button; break; case MTActionSheetButtonTypeCancel: margin = KMTActionSheetCancelAndOtherButtonMargin; self.cancelButton = button; break; } button.y = self.maxBottomView.bottom + margin; [self addSubview:button]; self.maxBottomView = button; return button; } - (void)createPopController { _popupController = [[zhPopupController alloc] init]; self.popupController.slideStyle = zhPopupSlideStyleFromBottom; self.popupController.layoutType = zhPopupLayoutTypeBottom; self.popupController.dismissOnMaskTouched = YES; _popupController.delegate = self; } - (void)layoutWithButtons { NSInteger begainIndex = 0; if (self.destructiveButton) { begainIndex++; } for (NSString *title in self.otherTitles) { [self addButtonWithTitle:title type:MTActionSheetButtonTypeNormal].tag = begainIndex; begainIndex++; } if (self.cancelButtonTitle) { [self addButtonWithTitle:self.cancelButtonTitle type:MTActionSheetButtonTypeCancel].tag = begainIndex; } self.height = self.maxBottomView.bottom + HOME_INDICATOR_HEIHT; } - (void)show { if (!_popupController) { [self createPopController]; } [self layoutWithButtons]; [self.popupController presentContentView:self]; } - (void)showInView:(UIView *)view { if (!_popupController) { [self createPopController]; } [self layoutWithButtons]; [self.popupController presentContentView:self duration:0.25 springAnimated:NO inView:view]; } - (void)buttonDidClick:(MTActionSheetButton *)button { if (self.onClickAction) { self.onClickAction(self, button.tag); } if ([self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) { [self.delegate actionSheet:self didDismissWithButtonIndex:button.tag]; } [self.popupController dismiss]; } - (void)dismiss { [self.popupController dismiss]; } - (void)popupControllerDidDismiss:(nonnull zhPopupController *)popupController { if (self.onDidDismiss) { self.onDidDismiss(self); } if ([self.delegate respondsToSelector:@selector(actionSheetDidDismiss:)]) { [self.delegate actionSheetDidDismiss:self]; } } @end