GiGaMaskTime/GIGA/Common/Views/LXCustomActionsheet/LXCustomActionSheet.m

193 lines
6.6 KiB
Objective-C

//
// LXCustomActionSheet.m
// CustomUISwitch
//
// Created by lianxiang on 2018/10/11.
// Copyright © 2018年 lian. All rights reserved.
//
#import "LXCustomActionSheet.h"
#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Screen_height [UIScreen mainScreen].bounds.size.height
#define SPACE 10
@interface LXCustomActionSheet ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *optionsArr;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *cancelTitle;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, copy) void(^selectedBlock)(NSInteger);
@property (nonatomic, copy) void(^cancelBlock)(void);
@end
@implementation LXCustomActionSheet
- (instancetype)initWithTitle:(NSString*)title
optionsArr:(NSArray*)optionsArr
cancelTitle:(NSString*)cancelTitle
selectedBlock:(void(^)(NSInteger))selectedBlock
cancelBlock:(void(^)(void))cancelBlock{
if (self = [super init]) {
_title = title;
_optionsArr = optionsArr;
_cancelTitle = cancelTitle;
_selectedBlock = selectedBlock;
_cancelBlock = cancelBlock;
[self craetUI];
}
return self;
}
- (void)craetUI {
self.frame = [UIScreen mainScreen].bounds;
[self addSubview:self.maskView];
[self addSubview:self.tableView];
}
- (UIView*)maskView {
if (!_maskView) {
_maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_maskView.backgroundColor = [UIColor blackColor];
_maskView.alpha = .5;
_maskView.userInteractionEnabled = YES;
}
return _maskView;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 44.0;
_tableView.bounces = NO;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UIView *tabHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0,Screen_Width, 50)];
tabHeader.backgroundColor = [UIColor whiteColor];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:
CGRectMake(0, 0, Screen_Width, 50) byRoundingCorners:
UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:
CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = tabHeader.bounds;
maskLayer.path = maskPath.CGPath;
tabHeader.layer.mask = maskLayer;
UILabel *titleLabe = [[UILabel alloc] init];
titleLabe.frame = tabHeader.bounds;
titleLabe.textAlignment = NSTextAlignmentCenter;
titleLabe.text = self.title;
titleLabe.font = GIGA_TEXTFONTMEDIUM(15);
titleLabe.textColor = GIGA_MAIN_BGCOLOR;
[tabHeader addSubview:titleLabe];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, tabHeader.frame.size.height -0.5, Screen_Width, 0.5)];
lineView.backgroundColor = [UIColor colorWithRed:222/255.0f green:222/255.0f blue:222/255.0f alpha:1];
[tabHeader addSubview:lineView];
self.headView = tabHeader;
_tableView.tableHeaderView = self.headView;
// _tableView.separatorInset = UIEdgeInsetsMake(0, -50, 0, 0);
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Navi_Cell"];
}
return _tableView;
}
#pragma mark TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (section == 0)?_optionsArr.count:1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Navi_Cell"];
cell.textLabel.font = GIGA_TEXTFONTMEDIUM(18);
cell.textLabel.textColor = GIGA_MAIN_BGCOLOR;
if (indexPath.section == 0) {
cell.textLabel.text = _optionsArr[indexPath.row];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height - 0.5, Screen_Width, 0.5)];
lineView.backgroundColor = [UIColor colorWithRed:222/255.0f green:222/255.0f blue:222/255.0f alpha:1];
[cell.contentView addSubview:lineView];
} else {
cell.textLabel.text = _cancelTitle;
}
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (self.selectedBlock) {
self.selectedBlock(indexPath.row);
}
}else{
if (self.cancelBlock) {
self.cancelBlock();
}
}
[self dismiss];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section==0) {
return 3;
}
return 0;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] init];
footerView.backgroundColor = [UIColor colorWithRed:222/255.0f green:222/255.0f blue:222/255.0f alpha:1];
return footerView;
}
-(void)show{
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self showTab];
}
- (void)showTab{
_tableView.frame = CGRectMake(0,Screen_height, Screen_Width, _tableView.rowHeight * (_optionsArr.count + 1) + 50 + 3);
[UIView animateWithDuration:.5 animations:^{
CGRect rect = self->_tableView.frame;
rect.origin.y -= self->_tableView.bounds.size.height;
self->_tableView.frame = rect;
}];
}
- (void)dismiss {
[UIView animateWithDuration:.5 animations:^{
CGRect rect = self->_tableView.frame;
rect.origin.y += self->_tableView.bounds.size.height;
self->_tableView.frame = rect;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (self.cancelBlock) {
self.cancelBlock();
}
[self dismiss];
}
@end