链接:iOS自定义ActionSheet
https://www.jianshu.com/p/76db3e8e0573
实现.h文件
#pragma mark ---- 使用方法
/*
用法: 创建Alert :initWithTitle:(NSString * )title style:(AlertStyle)style itemTitles:(NSArray*)items;
调用showAlert方法弹出alert
调用此方法消失dismissSheetView移除alert
处理点击事件:block 代理两种方法
代理处理:设置代理 实现代理方法,可以根据传出去的index或者title做逻辑判断
block: 创建alert 直接调用:didSelectAlertBlock功能同代理
自定义: 字号/颜色/item高度
*/
#import <UIKit/UIKit.h>
@class Dz_mostBeautifulAlert;
//******************************************************************
typedef NS_ENUM(NSUInteger, AlertStyle)
{ // 暂时没有投入使用
AlertPositionInTop = 0,
AlertPositionInBottom
};
//******************************************************************
typedef void(^DzAlertSelectBlock)(NSInteger index,NSString * title,Dz_mostBeautifulAlert * sender);
//******************************************************************
@protocol Dz_mostBeautifulAlertDelegate <NSObject>
// 代理方法,可以根据参数判断点击哪个
- (void)sheetViewDidSelectIndex:(NSInteger)index
title:(NSString *)title
sender:(id)sender;
// 点击取消按钮
- (void)cancelClickAction;
@end
@interface Dz_mostBeautifulAlert : UIView
// block
@property(copy,nonatomic)DzAlertSelectBlock block;
-(void)didSelectAlertBlock:(DzAlertSelectBlock)block;
// 代理
@property(weak,nonatomic)id<Dz_mostBeautifulAlertDelegate>delegate;
//******************************************************************
// 取消按钮字符串/默认取消
@property(strong,nonatomic)NSString* cancelItemStr;
//******************************************************************
// title字号/默认16
@property(strong,nonatomic)UIFont * titleTextFont;
// item文字字号/默认16
@property(strong,nonatomic)UIFont * itemTextFont;
// 取消按钮字号大小/默认16
@property(strong,nonatomic)UIFont * cancelFont;
//******************************************************************
// title颜色 /默认黑色
@property(strong,nonatomic)UIColor * titleTextColor;
// 取消按钮字符颜色/默认红色
@property(strong,nonatomic)UIColor * cancelItemColor;
// item文字颜色/默认黑色
@property(strong,nonatomic)UIColor * itemTextColor;
// 分割线颜色/默认浅灰0.2透明度
@property(strong,nonatomic)UIColor * lineColor;
@property(assign,nonatomic)CGFloat itemHeight;
//******************************************************************
// 初始化方法
-(instancetype)initWithTitle:(NSString *)title style:(AlertStyle)style itemTitles:(NSArray *)items;//style暂时没有传nil就行
// 单例
+(id)sharedDzCustomAlert;
-(void)ceartAlertWithTitle:(NSString * )title style:(AlertStyle)style itemTitles:(NSArray*)items;
#pragma mark ---- 弹出和移除
//******************************************************************
// 调用此方法消失alert
-(void)dismissSheetView;
// 调用此方法推出alert
-(void)showAlert;
@end
实现.m文件
#import "Dz_mostBeautifulAlert.h"
#define kW [UIScreen mainScreen].bounds.size.width
#define kH [UIScreen mainScreen].bounds.size.height
#define CH 40
#define HeaderH 15
#define FooterH 10
@interface Dz_mostBeautifulAlert ()<UITableViewDelegate,UITableViewDataSource>
{
CGFloat tH; //tableView高度
}
@property(strong,nonatomic) UIView * view;
@property(strong,nonatomic) UIView * contentView;
@property(strong,nonatomic) UITableView * tableView;
// alert类型
@property(assign,nonatomic)AlertStyle alertStyle;
// 内容数组
@property(strong,nonatomic) NSArray * items;
// 提示语
@property(strong,nonatomic) NSString * title;
@end
static NSString * const tableViewCellIdenter = @"tableViewCellIdenter";
@implementation Dz_mostBeautifulAlert
+(id)sharedDzCustomAlert
{
static Dz_mostBeautifulAlert * dzCustomAlert = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
dzCustomAlert = [[self.class alloc]init];
});
return dzCustomAlert;
}
-(void)ceartAlertWithTitle:(NSString *)title style:(AlertStyle)style itemTitles:(NSArray *)items
{
self.backgroundColor = [UIColor lightGrayColor];
_title = title;
_items = items;
_alertStyle = style;
}
-(instancetype)initWithTitle:(NSString *)title style:(AlertStyle)style itemTitles:(NSArray *)items
{
if (self = [super init]) {
self.backgroundColor = [UIColor lightGrayColor];
_title = title;
_items = items;
_alertStyle = style;
}
return self;
}
#pragma mark -- tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:{
if (self.title.length == 0) {
return _items.count;
}else return _items.count+1;
}
break;
case 1:{
return 1;
}
break;
default:{
return 0;
}
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdenter forIndexPath:indexPath];
[cell addSubview:[self topLineView:CGRectMake(0, cell.frame.size.height-1, kW,1)]];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.numberOfLines = 0;
cell.backgroundView = [self returnTheToolbar];
UIView * selectView =[UIView new];
selectView.backgroundColor=[[UIColor lightGrayColor]colorWithAlphaComponent:0.1];
cell.selectedBackgroundView =selectView;
switch (indexPath.section) {
case 0:{
if (_title.length != 0) {
if (indexPath.row ==0) {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textLabel.text = self.title;
if (self.textColor) {
cell.textLabel.textColor = self.textColor;
}
if (self.titleTextFont) {
cell.textLabel.font = self.titleTextFont;
}
}else{
cell.textLabel.text = self.items[indexPath.row-1];
if (self.itemTextFont) {
cell.textLabel.font = self.itemTextFont;
}
if (self.itemTextColor) {
cell.textLabel.textColor = self.itemTextColor;
}
}
}else{
cell.textLabel.text = self.items[indexPath.row];
if (self.itemTextFont){
cell.textLabel.font = self.itemTextFont;
}
if (self.itemTextColor){
cell.textLabel.textColor = self.itemTextColor;
}
}
}
break;
case 1:{
cell.textLabel.text = self.cancelItemStr;
if (self.cancelFont) {
cell.textLabel.font = self.cancelFont;
}else cell.textLabel.font = [UIFont systemFontOfSize:14];
if (self.cancelItemColor) {
cell.textLabel.textColor =self.cancelItemColor;
}else cell.textLabel.textColor = [UIColor redColor];
}
break;
default:
break;
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_title != nil) {// 有提示语
if (indexPath.section==0&&indexPath.row==0) {
if ([self cellHeightWithModel:self.title] > CH) {
return [self cellHeightWithModel:self.title]+CH;
}else return CH+15;
}else{
if (_itemHeight != 0) {
return _itemHeight;
}else return CH;
}
}else{
if (_itemHeight != 0) {
return _itemHeight;
}else return CH;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==1) {
return HeaderH;
}else return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section==1) {
return FooterH;
}else return 0;
}
// block处理点击事件
-(void)didSelectAlertBlock:(DzAlertSelectBlock)block
{
_block=block;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"12312312313123");
// if (_delegate &&[_delegate respondsToSelector:@selector(sheetViewDidSelectIndex:title:sender:)]){
// [_delegate sheetViewDidSelectIndex:indexPath.row title:_items[indexPath.row] sender:self];
// }
if (_title.length != 0) {
if (indexPath.section==0&&indexPath.row!=0) {
if (_block) {
self.block(indexPath.row-1,_items[indexPath.row-1],self);
}
if (_delegate &&[_delegate respondsToSelector:@selector(sheetViewDidSelectIndex:title:sender:)]){
[_delegate sheetViewDidSelectIndex:indexPath.row-1 title:_items[indexPath.row-1] sender:self];
}
}
if (indexPath.section == 1) {
if (_block) {
self.block(indexPath.row,self.cancelItemStr,self);
}
if (_delegate &&[_delegate respondsToSelector:@selector(cancelClickAction)]){
if (self.cancelItemStr.length!=0) {
[_delegate cancelClickAction];
}
}
}
}else
{
if (indexPath.section==0) {
if (_block) {
self.block(indexPath.row,_items[indexPath.row],self);
}
if (_delegate &&[_delegate respondsToSelector:@selector(sheetViewDidSelectIndex:title:sender:)]){
[_delegate sheetViewDidSelectIndex:indexPath.row title:_items[indexPath.row] sender:self];
}
}
if (indexPath.section==1) {
if (_block) {
self.block(indexPath.row,_cancelItemStr,self);
}
if (_delegate &&[_delegate respondsToSelector:@selector(cancelClickAction)]){
if (self.cancelItemStr.length!=0) {
[_delegate cancelClickAction];
}
}
}
}
}
#pragma mark -- 交互
-(void)showAlert
{
[[[UIApplication sharedApplication].delegate window].rootViewController.view addSubview:self];
self.view = [[UIApplication sharedApplication].delegate window].rootViewController.view;
[self.view addSubview:self.contentView];
[self.contentView addSubview:self.tableView];
[self pushSheetView];
}
// 弹出
-(void)pushSheetView
{
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3 animations:^{
weakSelf.contentView.frame = CGRectMake(0, kH - tH, kW, tH);
}];
}
// 移除
-(void)dismissSheetView
{
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.2 animations:^{
weakSelf.contentView.frame = CGRectMake(0, kH-FooterH, kW, tH);
} completion:^(BOOL finished) {
[weakSelf.contentView removeFromSuperview];
}];
}
// 线条View
-(UIView *)topLineView:(CGRect)frame
{
UIView * view = [[UIView alloc]init];
if (_lineColor) {
view.backgroundColor = _lineColor;
}else view.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.2];
view.frame = frame;
return view;
}
// 计算高度
-(CGFloat)cellHeightWithModel:(NSString *)title
{
// 不固定高度
CGFloat dynamicHeight = [self getLabelHeightByWidth:kW - 60 Title:title font:[UIFont systemFontOfSize:13]];
if (_itemHeight != 0) {
if (dynamicHeight > _itemHeight) {
return dynamicHeight+15;
}else return _itemHeight;
}else{
if (dynamicHeight > CH) {
return dynamicHeight+15;
}else return CH;
}
}
-(CGFloat)getLabelHeightByWidth:(CGFloat)width Title:(NSString *)title font:(UIFont *)font {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
label.text = title;
label.font = font;
// label.lineBreakMode = NSLineBreakByWordWrapping;
// label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width;
// label.numberOfLines = 0;
[label sizeToFit];
CGFloat height = label.frame.size.height;
NSLog(@"%f",height);
return height;
}
#pragma mark -- setter getter
-(NSArray *)items{
if (!_items) {
_items = [NSArray array];
}
return _items;
}
-(UIView *)contentView
{
if (!_contentView) {
_contentView = [[UIView alloc]initWithFrame:CGRectMake(0,kH, kW, tH)];
}
return _contentView;
}
-(UIView *)returnTheToolbar
{
UIView * view = [UIView new];
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,kH, kW, tH)];
toolbar.alpha = 0.8;
toolbar.backgroundColor = [UIColor whiteColor];
[view addSubview:toolbar];
return view;
}
-(UITableView *)tableView{
if (!_tableView) {
NSLog(@"item的高度是什么:%f",_itemHeight);
if (_title.length==0){
if (_itemHeight != 0) {
tH=_items.count * _itemHeight + _itemHeight+HeaderH+FooterH;
}else tH= _items.count*CH+CH+HeaderH+FooterH;
}else
{
if (_itemHeight!=0) {
tH=(_items.count+1)*_itemHeight+ _itemHeight+HeaderH+FooterH+15;//分区1高度+分区2高度+区头+区尾+第一行cell加的15
}else tH=(_items.count+1)*CH+ CH+HeaderH+FooterH+15;//分区1高度+分区2高度+区头+区尾+第一行cell加的15
}
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kW,tH) style:(UITableViewStylePlain)];
[_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
_tableView.backgroundView = [self returnTheToolbar];
_tableView.scrollEnabled = NO;
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdenter];
}
return _tableView;
}
-(NSString *)cancelItemStr
{
if (!_cancelItemStr) {
_cancelItemStr = @"取消";
}
return _cancelItemStr;
}
-(UIColor *)lineColor{
if (!_lineColor) {
_lineColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.1];
}
return _lineColor;
}
-(UIColor *)itemTextColor{
if (!_itemTextColor) {
_itemTextColor = [UIColor blackColor];
}
return _itemTextColor;
}
-(UIColor *)cancelItemColor{
if (!_cancelItemColor) {
_cancelItemColor = [UIColor redColor];
}
return _cancelItemColor;
}
-(UIColor *)textColor{
if (!_titleTextColor) {
_titleTextColor = [UIColor lightGrayColor];
}
return _titleTextColor;
}
-(UIFont *)textFont{
if (!_titleTextFont) {
_titleTextFont = [UIFont systemFontOfSize:14];
}
return _titleTextFont;
}
-(UIFont *)itemTextFont{
if (!_itemTextFont) {
_itemTextFont = [UIFont systemFontOfSize:14];
}
return _itemTextFont;
}
-(UIFont *)cancelFont
{
if (!_cancelFont){
_cancelFont = [UIFont systemFontOfSize:14];
}
return _cancelFont;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
使用:
#import "ViewController.h"
#import "Dz_mostBeautifulAlert.h"
@interface ViewController ()<Dz_mostBeautifulAlertDelegate> {
Dz_mostBeautifulAlert *_alertVC;
}
@property(strong,nonatomic)Dz_mostBeautifulAlert * alert;
@property(strong,nonatomic)Dz_mostBeautifulAlert * alert1;
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[_alertVC dismissSheetView];
_alertVC = [[Dz_mostBeautifulAlert alloc]initWithTitle:nil style:AlertPositionInTop itemTitles:@[@"我不是黄蓉",@"我不会武功",@"我只想要靖哥哥",@"的bigduangduang是打发打发似懂非懂地方第大幅度发阿道夫阿诗丹顿撒 阿萨德 三方第三方都是"]];
_alertVC.cancelItemColor = [self returnCustomColor];
_alertVC.delegate = self;
[_alertVC showAlert]; //调用此方法弹出alert
}
#pragma mark --- 代理处理alert点击事件以及弹出和移除
// 根据参数可以判断点击事件
-(void)sheetViewDidSelectIndex:(NSInteger)index title:(NSString *)title sender:(id)sender
{
// NSLog(@"index=%ld,title=%@,seder=%@",(long)index,title,sender);
NSLog(@"%@",title);
}
//// 移除alert
-(void)cancelClickAction{
[self.alert dismissSheetView];
}
-(UIColor *)returnCustomColor
{
int R = (arc4random() % 256) ;
int G = (arc4random() % 256) ;
int B = (arc4random() % 256) ;
UIColor * color = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1];
return color;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论