Created
August 21, 2019 03:00
-
-
Save qy1010/43b6880821b89eff54db68d4e5e50826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface HTVActionSheet : UIView | |
@property (nonatomic, copy, readonly) NSArray<NSString *> *titles; | |
- (instancetype)initWithTitles:(NSArray<NSString *> *)titles | |
selectedBlock:(void(^)(NSInteger))selectedBlock; | |
- (void)show; | |
- (void)dismiss; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <YYCategories/YYCategories.h> | |
#import <Masonry/Masonry.h> | |
#import "HTVActionSheet.h" | |
#import "HTVActionSheetCell.h" | |
@interface HTVActionSheetController : UIViewController | |
@property (nonatomic, weak) HTVActionSheet *actionSheet; | |
@end | |
@implementation HTVActionSheetController | |
- (UIStatusBarStyle)preferredStatusBarStyle | |
{ | |
return UIStatusBarStyleLightContent; | |
} | |
- (UIInterfaceOrientationMask)supportedInterfaceOrientations | |
{ | |
return (UIInterfaceOrientationMask)(1 << [[UIApplication sharedApplication] statusBarOrientation]); | |
} | |
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event | |
{ | |
[super touchesBegan:touches withEvent:event]; | |
[self.actionSheet dismiss]; | |
} | |
@end | |
@interface HTVActionSheet () <UITableViewDataSource, UITableViewDelegate> | |
@property (nonatomic, strong) UIWindow *window; | |
@property (nonatomic, strong) UITableView *tableView; | |
@property (nonatomic, copy, readwrite) NSArray<NSString *> *titles; | |
@property (nonatomic, copy) void(^selectedBlock)(NSInteger); | |
@end | |
@implementation HTVActionSheet | |
- (instancetype)initWithTitles:(NSArray<NSString *> *)titles | |
selectedBlock:(void(^)(NSInteger))selectedBlock | |
{ | |
if (self = [super init]) { | |
_titles = titles; | |
_selectedBlock = selectedBlock; | |
[self setup]; | |
} | |
return self; | |
} | |
- (CGSize)intrinsicContentSize | |
{ | |
CGSize size = CGSizeMake(kScreenWidth, 50 * self.titles.count + 50); | |
if (@available(iOS 11.0, *)) { | |
size.height += self.safeAreaInsets.bottom; | |
} | |
return size; | |
} | |
- (void)safeAreaInsetsDidChange | |
{ | |
[super safeAreaInsetsDidChange]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)setup | |
{ | |
[self.tableView registerClass:[HTVActionSheetCell class] | |
forCellReuseIdentifier:@"HTVActionSheetCell"]; | |
[self addSubview:self.tableView]; | |
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.edges.equalTo(self); | |
}]; | |
} | |
- (UITableView *)tableView | |
{ | |
if (_tableView == nil) { | |
_tableView = [[UITableView alloc] init]; | |
_tableView.dataSource = self; | |
_tableView.delegate = self; | |
_tableView.scrollEnabled = NO; | |
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone; | |
} | |
return _tableView; | |
} | |
- (void)show | |
{ | |
HTVActionSheetController *controller = [[HTVActionSheetController alloc] init]; | |
controller.actionSheet = self; | |
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; | |
self.window.backgroundColor = [UIColor clearColor]; | |
self.window.windowLevel = UIWindowLevelAlert; | |
self.window.rootViewController = controller; | |
// [self.window makeKeyAndVisible]; | |
self.window.hidden = NO; | |
[self.window addSubview:self]; | |
[self mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.top.equalTo(self.window.mas_bottom); | |
make.leading.trailing.equalTo(self.window); | |
}]; | |
[self.window layoutIfNeeded]; | |
[UIView animateWithDuration:0.25 animations:^{ | |
[self mas_remakeConstraints:^(MASConstraintMaker *make) { | |
make.leading.trailing.bottom.equalTo(self.window); | |
}]; | |
[self.window layoutIfNeeded]; | |
self.window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; | |
}]; | |
} | |
- (void)dismiss | |
{ | |
[UIView animateWithDuration:0.25 animations:^{ | |
[self mas_remakeConstraints:^(MASConstraintMaker *make) { | |
make.top.equalTo(self.window.mas_bottom); | |
make.leading.trailing.equalTo(self.window); | |
}]; | |
[self.window layoutIfNeeded]; | |
self.window.backgroundColor = [UIColor clearColor]; | |
} completion:^(BOOL finished) { | |
[self removeFromSuperview]; | |
}]; | |
} | |
#pragma mark - UITableView | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 2; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
if (section == 1) { | |
return 1; | |
} | |
return self.titles.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
HTVActionSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HTVActionSheetCell" | |
forIndexPath:indexPath]; | |
if (indexPath.section == 1) { | |
cell.titleLabel.text = NSLocalizedString(@"cancel", @"取消"); | |
cell.titleLabel.textColor = [UIColor colorWithRGB:0xbbbbbb]; | |
cell.contentView.backgroundColor = [UIColor colorWithRGB:0xf5f5f5]; | |
cell.separatorView.hidden = YES; | |
return cell; | |
} | |
cell.titleLabel.text = self.titles[(NSUInteger)indexPath.row]; | |
cell.titleLabel.textColor = [UIColor colorWithRGB:0x1d1d1d]; | |
cell.contentView.backgroundColor = [UIColor whiteColor]; | |
cell.separatorView.hidden = indexPath.row == self.titles.count - 1; | |
return cell; | |
} | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (@available(iOS 11.0, *)) { | |
if (indexPath.section == 1) { | |
return 50 + self.safeAreaInsets.bottom; | |
} | |
} | |
return 50; | |
} | |
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { | |
// UIView *separator = [[UIView alloc] init]; | |
// separator.backgroundColor = [UIColor colorWithRGB:0x32323a]; | |
// | |
// UIView *footerView = [[UIView alloc] init]; | |
// footerView.backgroundColor = [UIColor colorWithRGB:0x1b1b24]; | |
// [footerView addSubview:separator]; | |
// [separator mas_makeConstraints:^(MASConstraintMaker *make) { | |
// make.edges.mas_equalTo(UIEdgeInsetsMake(0, 16, 0, 16)); | |
// }]; | |
// | |
// return footerView; | |
//} | |
// | |
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { | |
// return 1 / kScreenScale; | |
//} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
[self dismiss]; | |
if (self.selectedBlock) { | |
self.selectedBlock(indexPath.section == 0 ? indexPath.row : -1); | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface HTVActionSheetCell : UITableViewCell | |
@property (nonatomic, strong) UILabel *titleLabel; | |
@property (nonatomic, strong) UIView *separatorView; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <YYCategories/YYCategories.h> | |
#import <Masonry/Masonry.h> | |
#import "HTVActionSheetCell.h" | |
@implementation HTVActionSheetCell | |
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier | |
{ | |
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { | |
UIView *selectedBackgroundView = [[UIView alloc] init]; | |
selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1]; | |
self.selectedBackgroundView = selectedBackgroundView; | |
} | |
return self; | |
} | |
- (UILabel *)titleLabel | |
{ | |
if (_titleLabel == nil) { | |
_titleLabel = [[UILabel alloc] init]; | |
_titleLabel.font = [UIFont fontWithName:@"Roboto-Regular" size:16]; | |
_titleLabel.textAlignment = NSTextAlignmentCenter; | |
[self.contentView addSubview:_titleLabel]; | |
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { | |
if (@available(iOS 11.0, *)) { | |
make.centerX.equalTo(self.contentView); | |
make.top.equalTo(self.contentView.mas_safeAreaLayoutGuideTop); | |
make.bottom.equalTo(self.contentView.mas_safeAreaLayoutGuideBottom); | |
} else { | |
make.center.equalTo(self.contentView); | |
} | |
}]; | |
} | |
return _titleLabel; | |
} | |
- (UIView *)separatorView | |
{ | |
if (_separatorView == nil) { | |
_separatorView = [[UIView alloc] init]; | |
_separatorView.backgroundColor = [UIColor colorWithRGB:0xe3e4e6]; | |
[self addSubview:_separatorView]; | |
[_separatorView mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.left.right.bottom.equalTo(self); | |
make.height.equalTo(@(1 / kScreenScale)); | |
}]; | |
} | |
return _separatorView; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment