123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // CustomActionSheet.m
- // MIT_Shop
- //
- // Created by 翟玉磊 on 2017/10/17.
- // Copyright © 2017年 翟玉磊. All rights reserved.
- //
- #import "CustomActionSheet.h"
- #define CancelButtonHeight BUTTON_HEIGHT //取消按钮的高度
- #define TitleViewHeight 50.0f //标题高度
- #define OtherButtonHeight 50.0f //其他按钮的高度(就是cell的高度)
- #define OtherButtonNumMax 4 //其他按钮最大数量4个 超过四个则开启滚动
- #define DefaultMarkImageName [UIImage imageNamed:@"goods_gou"] //默认标示图片
- @interface CustomActionSheet ()<UITableViewDelegate, UITableViewDataSource>
- {
- CGFloat _infoViewHeight;
-
- BOOL _isCanScroll; //是否可以滚动当otherbutto超过默认最大数量时,则可以滑动
-
- CGRect _showInfoViewRect; //显示infoView的rect
- CGRect _hiddenInfoViewRect; //隐藏infoView的rect
-
- NSInteger _selectedCellIndex; //选中的索引 默认第一个:0
- }
- @property (nonatomic, strong) UIButton *bgButton;
- @property (nonatomic, strong) UIView *infoView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UIButton *cancelButton;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) UIImageView *markImageView; //选中的标示
- @end
- static NSString * const cellId = @"UITableViewCellActionSheetId";
- @implementation CustomActionSheet
- + (instancetype)showActionSheetWithTitle:(NSString *)title cancelTitle:(NSString *)cancel otherButtonTitles:(NSArray *)titles {
- CustomActionSheet *sheet = [[CustomActionSheet alloc] initWithFrame:[UIScreen mainScreen].bounds];
-
- sheet.title = title;
- sheet.cancelTitle = cancel;
- sheet.otherButtonTitleArray = [NSMutableArray arrayWithArray:titles];
-
- [sheet initValue];
- [sheet buildView];
- [sheet addAction];
- return sheet;
- }
- - (void)initValue {
-
- //默认选中条数
- _selectedCellIndex = 0;
-
- //计算高度infoView的高度
- _infoViewHeight = 0.0f;
- if (_title.length > 0 && _title) {
- _infoViewHeight += TitleViewHeight;
- }
- if (_otherButtonTitleArray.count > 0 && _otherButtonTitleArray) {
- _infoViewHeight += ((_otherButtonTitleArray.count <= OtherButtonNumMax ? _otherButtonTitleArray.count : OtherButtonNumMax) * OtherButtonHeight);
- _infoViewHeight += (_otherButtonTitleArray.count <= OtherButtonNumMax ? 0 : 10);
- }
- if (_cancelTitle.length > 0 && _cancelTitle) {
- _infoViewHeight += CancelButtonHeight;
- }
-
- //计算infoView的rect
- _showInfoViewRect = CGRectMake(0, self.f_heigh - _infoViewHeight, self.f_width, _infoViewHeight);
- _hiddenInfoViewRect = CGRectMake(0, self.f_heigh, self.f_width, _infoViewHeight);
- }
- - (void)buildView {
-
- [self addSubview:self.bgButton];
- [self addSubview:self.infoView];
- [self.infoView addSubview:self.tableView];
- [self.infoView addSubview:self.cancelButton];
-
- [self.bgButton setFrame:self.bounds];
- [self.infoView setFrame:_hiddenInfoViewRect];
- [self.cancelButton setFrame:CGRectMake(0, _infoViewHeight - CancelButtonHeight, self.f_width, CancelButtonHeight)];
- [self.tableView setFrame:CGRectMake(0, 0, self.f_width, _infoViewHeight - CancelButtonHeight)];
-
- if (_title.length > 0 && _title) {
- self.tableView.tableHeaderView = [self createHeaderView];
- }
-
- //设置
- //1.当其他按钮的数量大于默认最大数量 则开启滑动
- _tableView.scrollEnabled = (_otherButtonTitleArray.count > OtherButtonNumMax);
-
- //2.填充取消按钮text
- [self.cancelButton setTitle:_cancelTitle forState:UIControlStateNormal];
-
- //3.标题
- self.titleLabel.text = _title;
-
- [self show];
- }
- - (UIView *)createHeaderView {
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.f_width, TitleViewHeight)];
- view.backgroundColor = [UIColor whiteColor];
-
- [view addSubview:self.titleLabel];
- self.titleLabel.frame = CGRectMake(0, 0, self.f_width, TitleViewHeight);
-
- [view addSubview:[UIView createLineRect:CGRectMake(0, TitleViewHeight - 0.5, self.f_width, 0.5)]];
- return view;
- }
- - (void)addAction {
- //确认 取消
- [self.cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self.bgButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)cancelButtonAction:(id)sender {
- [self dismiss];
- if (self.actionSheetDidSelectedAtIndex) {
- self.actionSheetDidSelectedAtIndex(self->_otherButtonTitleArray.count, self->_selectedCellIndex);
- }
- }
- - (void)bgButtonAction:(id)sender {
- [self dismiss];
- }
- #pragma mark - Public
- //替换选中标示image
- - (void)setMarkImage:(UIImage *)markImage {
-
- _markImage = markImage;
- self.markImageView.image = markImage;
- }
- /**
- 显示ActionSheet
- */
- - (void)show {
-
- //添加
- [[UIApplication sharedApplication].delegate.window addSubview:self];
-
- [UIView animateWithDuration:.3 animations:^{
- self.infoView.frame = self->_showInfoViewRect;
- }];
- }
- /**
- 隐藏ActionSheet
- */
- - (void)dismiss {
- [UIView animateWithDuration:.3 animations:^{
- self.infoView.frame = self->_hiddenInfoViewRect;
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }
- #pragma mark - UITableViewDelegate, UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _otherButtonTitleArray.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- return _otherButtonHeight <= 0 ? OtherButtonHeight : _otherButtonHeight;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
-
- cell.textLabel.text = _otherButtonTitleArray[indexPath.row];
- cell.textLabel.textColor = RGB(51, 51, 51);
- cell.textLabel.font = Font(16);
- if (_selectedCellIndex == indexPath.row) {
- [cell.contentView addSubview:self.markImageView];
- self.markImageView.frame = CGRectMake(self.f_width - 15 - 15, (OtherButtonHeight - 12) / 2, 15, 12);
- }
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- _selectedCellIndex = indexPath.row;
- [self.tableView reloadData];
- if (self.actionSheetDidSelectedAtIndex) {
- self.actionSheetDidSelectedAtIndex(indexPath.row, indexPath.row);
- }
- }
- #pragma mark - Getter
- - (UIButton *)bgButton {
- if (_bgButton == nil) {
- _bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _bgButton.backgroundColor = [UIColor blackColor];
- _bgButton.alpha = 0.4f;
- }
- return _bgButton;
- }
- - (UIView *)infoView {
- if (_infoView == nil) {
- _infoView = [UIView new];
- _infoView.backgroundColor = [UIColor whiteColor];
- }
- return _infoView;
- }
- - (UIButton *)cancelButton {
- if (_cancelButton == nil) {
- _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _cancelButton.backgroundColor = Color_Button_Select;
- [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- }
- return _cancelButton;
- }
- - (UILabel *)titleLabel {
- if (_titleLabel == nil) {
- _titleLabel = [UILabel new];
- _titleLabel.font = Font(16);
- _titleLabel.textColor = RGB(153, 153, 153);
- _titleLabel.backgroundColor = [UIColor clearColor];
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _titleLabel;
- }
- - (UITableView *)tableView {
- if (_tableView == nil) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.backgroundColor = Color_Background;
- _tableView.scrollEnabled = NO; //默认是不可以滚动
- [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
- }
- return _tableView;
- }
- - (UIImageView *)markImageView {
- if (_markImageView == nil) {
- _markImageView = [UIImageView new];
- _markImageView.image = DefaultMarkImageName;
- }
- return _markImageView;
- }
- @end
|