封装的一个ActionSheetView

代码如下:
GQActionSheetView.h
#import <UIKit/UIKit.h>
@interface GQChooseCell : UITableViewCell
@property (strong, nonatomic) UILabel * title;
@property (strong, nonatomic) UIView * separtline;
@end
@interface GQActionSheetView : UIView
@property (copy, nonatomic) void(^choosedBlock)(NSString * ,NSInteger);
//初始化
/**
* 创建一个底部选择view
*
* @param titles 选项数组
* @param cancle 取消的标题--默认取消
*
* @return 返回一个底部选择view
*/
- (instancetype)initWithTitles:(NSArray*)titles withCancleTitle:(NSString*)cancle;
- (instancetype)initWithTitles:(NSArray*)titles withCancleTitle:(NSString*)cancle cancelColor:(UIColor *)color;
- (instancetype)initWithTitles:(NSArray*)titles titleColors:(NSArray<UIColor *> *)colors withCancleTitle:(NSString*)cancle;
- (instancetype)initWithTitles:(NSArray*)titles titleColors:(NSArray<UIColor *> *)colors withCancleTitle:(NSString*)cancle cancelColor:(UIColor *)color;
- (void)show;
- (void)dismiss;
@end
GQActionSheetView.m
#import "GQActionSheetView.h"
@implementation GQChooseCell
- (instancetype)init
{
self = [super init];
if (self) {
[self addTitleView];
}
return self;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self addTitleView];
}
return self;
}
- (void)addTitleView
{
self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
self.title.textAlignment = NSTextAlignmentCenter;
self.title.textColor = [UIColor blackColor];
self.title.font = [UIFont systemFontOfSize:18];
self.title.center = self.center;
[self addSubview:self.title];
UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, 1)];
line.backgroundColor = [UIColor lightGrayColor];
[self addSubview:line];
self.separtline = line;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.title mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
}];
[self.separtline mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.height.offset(1);
make.bottom.equalTo(self);
}];
}
@end
#define KCell_H 45
#define KSection_H 10
@interface GQActionSheetView ()<UITableViewDelegate,UITableViewDataSource>
{
CGFloat tableView_H;//高度
}
@property (strong, nonatomic) UITableView * TableView;
@property (strong, nonatomic) NSMutableArray * dataArrys;
@property (strong, nonatomic) NSString * cancleTitle;
@property (strong, nonatomic) UIColor * cancleColor;
@property (strong, nonatomic) NSArray<UIColor *> *titleColors;
@end
static NSString * cellId = @"GQChooseCell";
@implementation GQActionSheetView
- (instancetype)initWithTitles:(NSArray*)titles withCancleTitle:(NSString*)cancle
{
return [self initWithTitles:titles titleColors:nil withCancleTitle:cancle cancelColor:nil];
}
- (instancetype)initWithTitles:(NSArray*)titles withCancleTitle:(NSString*)cancle cancelColor:(UIColor *)color
{
return [self initWithTitles:titles titleColors:nil withCancleTitle:cancle cancelColor:color];
}
- (instancetype)initWithTitles:(NSArray*)titles titleColors:(NSArray<UIColor *> *)colors withCancleTitle:(NSString*)cancle
{
return [self initWithTitles:titles titleColors:colors withCancleTitle:cancle cancelColor:nil];
}
- (instancetype)initWithTitles:(NSArray*)titles titleColors:(NSArray<UIColor *> *)colors withCancleTitle:(NSString*)cancle cancelColor:(UIColor *)color
{
if (self = [super init]) {
[self initTableViewWith:titles];
self.cancleTitle = cancle ? cancle : @"取消";
self.titleColors = colors;
self.cancleColor = color?:[UIColor redColor];
[self.TableView reloadData];
}
return self;
}
- (void)show
{
UIWindow * keyWind = [[[UIApplication sharedApplication] delegate] window];
[keyWind addSubview:self];
self.alpha = 0;
[UIView animateWithDuration:0.20 animations:^{
self.alpha = 1;
self.TableView.frame = CGRectMake(0, WINDOW_height - tableView_H, WINDOW_width, tableView_H);
}];
}
-(void)dismiss
{
self.alpha = 1;
[UIView animateWithDuration:0.20 animations:^{
self.TableView.frame = CGRectMake(0, WINDOW_height , WINDOW_width, tableView_H);
self.alpha = 0;
} completion:^(BOOL finished) {
if (self.superview) {
[self removeFromSuperview];
}
}];
}
- (void)initTableViewWith:(NSArray*)titles
{
self.frame = [UIScreen mainScreen].bounds;
tableView_H = (titles.count +1)*KCell_H + KSection_H;
self.backgroundColor = RGBA_COLOR(1, 1, 1, 0.3);
UITableView * table = [[UITableView alloc]init];
table.frame = CGRectMake(0, WINDOW_height, WINDOW_width, tableView_H);
table.separatorStyle = UITableViewCellSeparatorStyleNone;
table.scrollEnabled = NO;
table.delegate = self;
table.dataSource = self;
self.TableView = table;
[self addSubview:self.TableView];
self.dataArrys = [NSMutableArray arrayWithArray:titles];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return self.dataArrys.count;
}else{
return 1;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GQChooseCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[GQChooseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.section == 0) {
cell.title.text = self.dataArrys[indexPath.row];
cell.title.textColor = [UIColor blackColor];
if (self.titleColors.count > indexPath.row) {
cell.title.textColor = self.titleColors[indexPath.row];
}
} else {
cell.title.text = _cancleTitle;
cell.title.textColor = self.cancleColor;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.choosedBlock && indexPath.section == 0) {
NSString * title = self.dataArrys[indexPath.row];
self.choosedBlock (title, indexPath.row);
}
[self dismiss];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return KCell_H ;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section != 0) {
return KSection_H;
}
return 0;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self dismiss];
}
@end
网友评论