iOS自定義ActionSheet(輕模仿微信)

城市有我奮斗的青春.jpg

這波不多說廢話直接來(lái)干的:

最近心情比較差還好工作不太忙,微博微信刷多了,感覺他們的Alert很漂亮模仿一下

ActionSheet使用說明:

初始化方法:

  • 初始化方法創(chuàng)建,之后調(diào)用響應(yīng)的彈出和移除方法
  • 單例創(chuàng)建方式,調(diào)用創(chuàng)建Alert再調(diào)用彈出和移除就可以

點(diǎn)擊事件處理

  • 使用代理帶出相關(guān)index下標(biāo),title內(nèi)容以及視圖本身self(需要遵循代理實(shí)現(xiàn)代理方法)
  • 使用Block代碼塊傳出響應(yīng)的數(shù)據(jù),以供使用(需要調(diào)用didSelectAlertBlock:方法)

自定義部分

  • ActionSheet的每個(gè)item的字體顏色,字號(hào),行高可以自定義(比如alert.titleTextColor=[UIColor redColr])

效果圖:

帶內(nèi)容提示.png
不帶內(nèi)容提示.png
樓底有demo地址

下面直接貼代碼:

.h文件:
#import <UIKit/UIKit.h>
@class Dz_mostBeautifulAlert;

//******************************************************************
typedef NS_ENUM(NSUInteger, AlertStyle)
{   // 暫時(shí)沒有投入使用
    AlertPositionInTop = 0,
    AlertPositionInBottom
};

//******************************************************************

typedef void(^DzAlertSelectBlock)(NSInteger index,NSString * title,Dz_mostBeautifulAlert * sender);

//******************************************************************

@protocol Dz_mostBeautifulAlertDelegate <NSObject>
// 代理方法,可以根據(jù)參數(shù)判斷點(diǎn)擊哪個(gè)
- (void)sheetViewDidSelectIndex:(NSInteger)index
                          title:(NSString *)title
                         sender:(id)sender;
// 點(diǎn)擊取消按鈕
- (void)cancelClickAction;
@end
@interface Dz_mostBeautifulAlert : UIView
// block
@property(copy,nonatomic)DzAlertSelectBlock block;
-(void)didSelectAlertBlock:(DzAlertSelectBlock)block;

// 代理
@property(weak,nonatomic)id<Dz_mostBeautifulAlertDelegate>delegate;

//******************************************************************
// 取消按鈕字符串/默認(rèn)取消
@property(strong,nonatomic)NSString* cancelItemStr;

//******************************************************************
// title字號(hào)/默認(rèn)16
@property(strong,nonatomic)UIFont  * titleTextFont;

// item文字字號(hào)/默認(rèn)16
@property(strong,nonatomic)UIFont  * itemTextFont;

// 取消按鈕字號(hào)大小/默認(rèn)16
@property(strong,nonatomic)UIFont  * cancelFont;

//******************************************************************
// title顏色 /默認(rèn)黑色
@property(strong,nonatomic)UIColor * titleTextColor;

// 取消按鈕字符顏色/默認(rèn)紅色
@property(strong,nonatomic)UIColor * cancelItemColor;

// item文字顏色/默認(rèn)黑色
@property(strong,nonatomic)UIColor * itemTextColor;

// 分割線顏色/默認(rèn)淺灰0.2透明度
@property(strong,nonatomic)UIColor * lineColor;

@property(assign,nonatomic)CGFloat itemHeight;

//******************************************************************
// 初始化方法
-(instancetype)initWithTitle:(NSString *)title style:(AlertStyle)style itemTitles:(NSArray *)items;//style暫時(shí)沒有傳nil就行

// 單例
+(id)sharedDzCustomAlert;
-(void)ceartAlertWithTitle:(NSString * )title style:(AlertStyle)style itemTitles:(NSArray*)items;
#pragma mark ---- 彈出和移除
//******************************************************************
// 調(diào)用此方法消失alert
-(void)dismissSheetView;
// 調(diào)用此方法推出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;
// 內(nèi)容數(shù)組
@property(strong,nonatomic) NSArray * items;
// 提示語(yǔ)
@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:16];
            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) {// 有提示語(yǔ)
        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處理點(diǎn)擊事件
-(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;
}
// 計(jì)算高度
-(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.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.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;//分區(qū)1高度+分區(qū)2高度+區(qū)頭+區(qū)尾+第一行cell加的15
            }else tH=(_items.count+1)*CH+ CH+HeaderH+FooterH+15;//分區(qū)1高度+分區(qū)2高度+區(qū)頭+區(qū)尾+第一行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:16];
    }
    return _titleTextFont;
}
-(UIFont *)itemTextFont{
    if (!_itemTextFont) {
        _itemTextFont = [UIFont systemFontOfSize:16];
    }
    return _itemTextFont;
}
-(UIFont *)cancelFont
{
    if (!_cancelFont){
        _cancelFont = [UIFont systemFontOfSize:16];
    }
    return _cancelFont;
}

demo下載地址:

https://pan.baidu.com/s/1kVBczQZ

結(jié)束
(寫這個(gè)Alert很大成分是為了發(fā)泄,心情不好就不說話戴耳機(jī),使勁敲鍵盤,使勁發(fā)泄)
這次寫簡(jiǎn)書的戰(zhàn)歌: 血腥愛情故事+魔鬼中的天使
其實(shí)你是一個(gè)有夢(mèng)想的孩子
不用怕現(xiàn)在的百無(wú)聊賴。時(shí)間到了,所有的好都會(huì)前赴后繼的向你撲來(lái),就怕你現(xiàn)在的努力太少駕馭不了,只能辜負(fù)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,190評(píng)論 4 61
  • 昨天晚上從八點(diǎn)開始和小伙伴們打撲克,打到12點(diǎn),室友還以為我倆發(fā)生了什么,打了好幾個(gè)電話,這個(gè)東西還是真上癮,這還...
    花兒的博文閱讀 574評(píng)論 0 1
  • 喜歡周國(guó)平的思想文字,他的哲學(xué)觀有很多讓我產(chǎn)生共鳴,也有很多讓我開始思考。既簡(jiǎn)單純粹,又有深度。 讀完周國(guó)平的《人...
    一只早鳥閱讀 4,585評(píng)論 0 5

友情鏈接更多精彩內(nèi)容