158 lines
5.8 KiB
Objective-C
158 lines
5.8 KiB
Objective-C
|
|
|
|
// Created by lee on 2019/5/22.
|
|
// Copyright © 2023 YUMI. All rights reserved.
|
|
|
|
|
|
#import "TTActionSheetView.h"
|
|
#import "TTActionSheetConfig.h"
|
|
#import "DJDKMIMOMColor.h"
|
|
#import <Masonry/Masonry.h>
|
|
|
|
static CGFloat const kSheetViewCellHeight = 51.f;
|
|
static CGFloat const kSheetViewCornerRadius = 14.f;
|
|
static NSString *const kSheetViewCellConst = @"kSheetViewCellConst";
|
|
|
|
@interface TTActionSheetView ()<UITableViewDelegate, UITableViewDataSource>
|
|
|
|
|
|
@property (nonatomic, strong) UITableView *tableView;
|
|
|
|
@property (nonatomic, strong) NSArray<TTActionSheetConfig *> *items;
|
|
|
|
@property (nonatomic, assign) BOOL needCancel;
|
|
|
|
@property (nonatomic, strong) UIButton *cancelButton;
|
|
|
|
@end
|
|
|
|
@implementation TTActionSheetView
|
|
|
|
#pragma mark -
|
|
#pragma mark lifeCycle
|
|
- (instancetype)initWithFrame:(CGRect)frame needCancel:(BOOL)needCancel items:(NSArray<TTActionSheetConfig *> *)items {
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
_items = items;
|
|
_needCancel = needCancel;
|
|
[self initViews];
|
|
[self initConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)initViews {
|
|
[self addSubview:self.tableView];
|
|
[self addSubview:self.cancelButton];
|
|
}
|
|
|
|
- (void)initConstraints {
|
|
|
|
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.top.mas_equalTo(self);
|
|
make.height.mas_equalTo(self.items.count * kSheetViewCellHeight);
|
|
}];
|
|
|
|
if (_needCancel) {
|
|
|
|
self.cancelButton.hidden = NO;
|
|
[self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.mas_equalTo(self);
|
|
make.height.mas_equalTo(kSheetViewCellHeight);
|
|
make.top.mas_equalTo(self.tableView.mas_bottom).offset(15);
|
|
}];
|
|
}
|
|
|
|
}
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
return 1;
|
|
}
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
return self.items.count;
|
|
}
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSheetViewCellConst];
|
|
cell.backgroundColor = UIColor.clearColor;
|
|
cell.textLabel.textAlignment = NSTextAlignmentCenter;
|
|
cell.textLabel.text = _items[indexPath.row].title;
|
|
cell.textLabel.textColor = _items[indexPath.row].titleColor;
|
|
|
|
if ([_items[indexPath.row] displayMoliCoin]) {
|
|
NSTextAttachment *coinAttachment = [[NSTextAttachment alloc] init];
|
|
coinAttachment.image = kImage(@"moli_money_icon");
|
|
coinAttachment.bounds = CGRectMake(0, -3.5, 20, 20);
|
|
NSAttributedString *coinAttributedString = [NSAttributedString attributedStringWithAttachment:coinAttachment];
|
|
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:coinAttributedString];
|
|
NSAttributedString *titleAttributedString = [[NSAttributedString alloc] initWithString:_items[indexPath.row].title
|
|
attributes:@{
|
|
NSForegroundColorAttributeName: _items[indexPath.row].titleColor,
|
|
NSFontAttributeName: cell.textLabel.font
|
|
}];
|
|
[string insertAttributedString:titleAttributedString atIndex:0];
|
|
cell.textLabel.attributedText = string.copy;
|
|
|
|
UIImageView *questionMark = [[UIImageView alloc] initWithImage:kImage(@"question_mark")];
|
|
[cell.contentView addSubview:questionMark];
|
|
[questionMark mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerY.mas_equalTo(cell.textLabel);
|
|
make.trailing.mas_equalTo(-20);
|
|
make.size.mas_equalTo(CGSizeMake(22, 22));
|
|
}];
|
|
}
|
|
|
|
return cell;
|
|
}
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
|
TTActionSheetConfig *config = _items[indexPath.row];
|
|
!config.clickAction ?: config.clickAction();
|
|
|
|
!_dismissAction ?: _dismissAction();
|
|
}
|
|
|
|
- (void)onClickCancelButtonAction:(UIButton *)cancelButton {
|
|
!_cancelAction ?: _cancelAction();
|
|
!_dismissAction ?: _dismissAction();
|
|
}
|
|
|
|
- (UITableView *)tableView {
|
|
if (!_tableView) {
|
|
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
|
|
_tableView.delegate = self;
|
|
_tableView.dataSource = self;
|
|
_tableView.separatorColor = [DJDKMIMOMColor actionSeparatorColor];
|
|
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
|
|
_tableView.rowHeight = kSheetViewCellHeight;
|
|
_tableView.tableFooterView = [[UIView alloc] init];
|
|
_tableView.backgroundColor = [UIColor whiteColor];
|
|
_tableView.layer.cornerRadius = kSheetViewCornerRadius;
|
|
_tableView.layer.masksToBounds = YES;
|
|
_tableView.bounces = NO;
|
|
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSheetViewCellConst];
|
|
}
|
|
return _tableView;
|
|
}
|
|
|
|
- (UIButton *)cancelButton {
|
|
if (!_cancelButton) {
|
|
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_cancelButton setTitle:YMLocalizedString(@"TTActionSheetView0") forState:UIControlStateNormal];
|
|
[_cancelButton setBackgroundColor:UIColor.whiteColor];
|
|
[_cancelButton setTitleColor:[DJDKMIMOMColor alertMessageColor] forState:UIControlStateNormal];
|
|
[_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
|
|
_cancelButton.layer.cornerRadius = kSheetViewCornerRadius;
|
|
_cancelButton.layer.masksToBounds = YES;
|
|
[_cancelButton addTarget:self action:@selector(onClickCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
_cancelButton.hidden = YES;
|
|
}
|
|
return _cancelButton;
|
|
}
|
|
|
|
@end
|