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

城市有我奮斗的青春.jpg

這波不多說廢話直接來干的:

最近心情比較差還好工作不太忙,微博微信刷多了,感覺他們的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的字體顏色千扔,字號,行高可以自定義(比如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字號/默認(rèn)16
@property(strong,nonatomic)UIFont  * titleTextFont;

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

// 取消按鈕字號大小/默認(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;
// 提示語
@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) {// 有提示語
        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ā)泄)
這次寫簡書的戰(zhàn)歌: 血腥愛情故事+魔鬼中的天使
其實(shí)你是一個(gè)有夢想的孩子
不用怕現(xiàn)在的百無聊賴。時(shí)間到了酬荞,所有的好都會(huì)前赴后繼的向你撲來搓劫,就怕你現(xiàn)在的努力太少駕馭不了,只能辜負(fù)混巧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枪向,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子咧党,更是在濱河造成了極大的恐慌秘蛔,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異缠犀,居然都是意外死亡数苫,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進(jìn)店門辨液,熙熙樓的掌柜王于貴愁眉苦臉地迎上來虐急,“玉大人,你說我怎么就攤上這事滔迈≈褂酰” “怎么了?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵燎悍,是天一觀的道長敬惦。 經(jīng)常有香客問我,道長谈山,這世上最難降的妖魔是什么俄删? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮奏路,結(jié)果婚禮上畴椰,老公的妹妹穿的比我還像新娘。我一直安慰自己鸽粉,他們只是感情好斜脂,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著触机,像睡著了一般帚戳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上儡首,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天片任,我揣著相機(jī)與錄音,去河邊找鬼蔬胯。 笑死对供,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的笔宿。 我是一名探鬼主播犁钟,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼泼橘!你這毒婦竟也來了涝动?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤炬灭,失蹤者是張志新(化名)和其女友劉穎醋粟,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡米愿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年厦凤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片育苟。...
    茶點(diǎn)故事閱讀 38,617評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡较鼓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出违柏,到底是詐尸還是另有隱情博烂,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布漱竖,位于F島的核電站禽篱,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏馍惹。R本人自食惡果不足惜躺率,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望万矾。 院中可真熱鬧悼吱,春花似錦、人聲如沸勤众。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽们颜。三九已至,卻和暖如春猎醇,著一層夾襖步出監(jiān)牢的瞬間窥突,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工硫嘶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留阻问,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓沦疾,卻偏偏與公主長得像称近,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子哮塞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內(nèi)容

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