iOS 你需要的彈窗大全

在我們的實(shí)際開發(fā)項(xiàng)目中,彈窗是必不可少的,很多時(shí)候我們用的是系統(tǒng)的AlertViewController,但是實(shí)際情況中,并不能滿足我們的開發(fā)需求,這個(gè)時(shí)候我們需要的就是自定義自己的彈窗效果。接下來我會(huì)寫一些自己的所封裝的彈窗效果。包括代理delegate回調(diào),block 回調(diào),xib新建view來創(chuàng)建我們需要的彈窗效果。

官方思路

1.在我們自己動(dòng)手之前一定要先看看官方是怎么封裝的,這樣我們寫出來的代碼才接近蘋果語言,看起來高大上。好的代碼一定是見名知意的,別人一看這個(gè)方法就知道大概我們通過這個(gè)方法可以得到什么樣的效果。

// ios8.0 之后
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"確定");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
// ios8.0 之前
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Tittle" message:@"This is message" delegate:self 
cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alertView show];

因?yàn)樵诖a量風(fēng)格上,我還是比較喜歡老版本的彈窗,畢竟代碼上啊,一句話調(diào)用美滋滋。所以接下來我們封裝也是模仿官方開始.....

delegate

我們可以看到在蘋果官方中,我們需要通過識(shí)別用戶點(diǎn)擊某個(gè)按鈕來確定需要進(jìn)一步的操作事件,這個(gè)時(shí)候是通過代理來實(shí)現(xiàn)的。代理的話,我們?cè)谑煜げ贿^了。

  1. 首先申明協(xié)議
#pragma mark - 協(xié)議
@class HLAlertView;
@protocol HLAlertViewDelegate<NSObject>
- (void)alertViewDidClickButtonWithIndex:(NSInteger)index;
@end
  1. 在viewController中遵循代理,設(shè)置代理 , 實(shí)現(xiàn)方法即可
<HLAlertViewDelegate>
self.delegate = self;

#pragma mark --- HLAlertViewDelegate
-(void)alertViewDidClickButtonWithIndex:(NSInteger)index{
    if (index == AlertSureButtonClick) {
        [self alertSureButtonClick];
    }else{
        [self alertCauseButtonClick];
    }
}
  1. 接下來就是實(shí)現(xiàn)我們封裝類的.h文件方法申明,以及.m的實(shí)現(xiàn)方法
//.h 文件
#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    AlertCauseButtonClick = 0,
    AlertSureButtonClick
} AlertButtonClickIndex;

#pragma mark - 協(xié)議
@class HLAlertView;
@protocol HLAlertViewDelegate<NSObject>
- (void)alertViewDidClickButtonWithIndex:(NSInteger)index;
@end
@interface HLAlertView : UIView

@property(nonatomic, weak) id <HLAlertViewDelegate> delegate;

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message sureButton:(NSString *)sureBtn;
- (void)show;

@end
@interface HLAlertView()

/** 彈窗主內(nèi)容view */
@property (nonatomic,strong) UIView   *contentView;

/** 彈窗標(biāo)題 */
@property (nonatomic,copy)   NSString *title;

/** message */
@property (nonatomic,copy)   NSString *message;

/** 確認(rèn)按鈕 */
@property (nonatomic,copy)   UIButton *sureButton;

@end


@implementation HLAlertView

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message sureButton:(NSString *)sureBtn{
    
    if (self = [super init]) {
        self.title = tittle;
        self.message = message;
        
        [self sutUpView];
    }
    return self;
}

- (void)sutUpView{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.85];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    }];
    
    //------- 彈窗主內(nèi)容 -------//
    self.contentView = [[UIView alloc]init];
    self.contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 80, 150);
    self.contentView.center = self.center;
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 6;
    [self addSubview:self.contentView];
    
    // 標(biāo)題
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.contentView.width, 22)];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.title;
    [self.contentView addSubview:titleLabel];
    
    // message
    UILabel *messageLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.contentView.width, 22)];
    messageLable.font = [UIFont boldSystemFontOfSize:17];
    messageLable.textAlignment = NSTextAlignmentCenter;
    messageLable.text = self.message;
    [self.contentView addSubview:messageLable];
    
    
    // 取消按鈕
    UIButton * causeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    causeBtn.frame = CGRectMake(0, self.contentView.height - 40, self.contentView.width/2, 40);
    causeBtn.backgroundColor = [UIColor grayColor];
    [causeBtn setTitle:@"取消" forState:UIControlStateNormal];
    [causeBtn addTarget:self action:@selector(causeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:causeBtn];
    
    // 確認(rèn)按鈕
    UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.frame = CGRectMake(causeBtn.width, causeBtn.y, causeBtn.width, 40);
    sureButton.backgroundColor = [UIColor redColor];
    [sureButton setTitle:@"確定" forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(processSure:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:sureButton];
    
}

- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}


- (void)processSure:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(alertViewDidClickButtonWithIndex:)]) {
        [self.delegate alertViewDidClickButtonWithIndex:AlertSureButtonClick];
    }
    [self dismiss];
}

- (void)causeBtn:(UIButton *)sender{

    if ([self.delegate respondsToSelector:@selector(alertViewDidClickButtonWithIndex:)]) {
        [self.delegate alertViewDidClickButtonWithIndex:AlertCauseButtonClick];
    }
    [self dismiss];
}

#pragma mark - 移除此彈窗
/** 移除此彈窗 */
- (void)dismiss{
    [self removeFromSuperview];
}

通過代理的方式我們就完成了我們自己頁面的封裝了。

block彈窗

先看一下封裝之后我們的調(diào)用方式吧:

HLAlertViewBlock * alertView = [[HLAlertViewBlock alloc] initWithTittle:@"提示" message:@"通過Block彈窗回調(diào)的彈窗" block:^(NSInteger index) {
        if (index == AlertSureButtonClick) {
            [self alertSureButtonClick];
        }else{
            [self alertCauseButtonClick];
        }
    }];
    [alertView show];

相比代理的方式的話,我們還行喜歡這種block回調(diào)的,簡大氣接地氣啊。當(dāng)然在我們需要處理邏輯多的時(shí)候,還是代理會(huì)比較好一點(diǎn),具體環(huán)境下具體使用。
封裝成block的好處就是在我們構(gòu)造方法的時(shí)候就可以實(shí)現(xiàn)我們將來的點(diǎn)擊方法,所以在自定義彈窗類的.h文件中,我們要申明block屬性。代碼

//.h
@interface HLAlertViewBlock : UIView

@property(nonatomic, copy) void (^buttonBlock) (NSInteger index);

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message block:(void (^) (NSInteger index))block;

- (void)show;

@end

//.m
@interface HLAlertViewBlock()

/** 彈窗主內(nèi)容view */
@property (nonatomic,strong) UIView   *contentView;

/** 彈窗標(biāo)題 */
@property (nonatomic,copy)   NSString *title;

/** message */
@property (nonatomic,copy)   NSString *message;

/** 確認(rèn)按鈕 */
@property (nonatomic,copy)   UIButton *sureButton;

@end


@implementation HLAlertViewBlock

- (instancetype)initWithTittle:(NSString *)tittle message:(NSString *)message block:(void (^)(NSInteger))block{
    if (self = [super init]) {
        self.title = tittle;
        self.message = message;
        self.buttonBlock = block;
        [self sutUpView];
    }
    return self;
}

到此為止,我們的block彈窗申明方法也搞定了。

xib的封裝彈窗

圖片.png

好處就是不用寫界面代碼了。

殊途同歸

還有一種實(shí)現(xiàn)彈窗效果的方法,不通過新建view而是Controller來實(shí)現(xiàn)的,就是新建一個(gè)透明的控制器。代碼如下

    PopViewController * popVC = [[PopViewController alloc] init];
    UIColor * color = [UIColor blackColor];
    popVC.view.backgroundColor = [color colorWithAlphaComponent:0.85];
    popVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:popVC animated:NO completion:nil];

更加簡單,邏輯也更加好處理一些。
最后附上demo地址:gibHub地址:https://github.com/MrBMask

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AGI閱讀 16,008評(píng)論 3 119
  • 外面的世界在下雪,我望向窗外不見枯枝披上新衣。我在的地方為什么不下雪?可我的心里已經(jīng)飄滿了白雪,還好這里溫暖如春。
    葛志力閱讀 255評(píng)論 0 0
  • “自己真正想去做的時(shí)候,產(chǎn)生的力量才是無窮的。稻盛會(huì)長深知這一點(diǎn)。在開發(fā)新產(chǎn)品的時(shí)候,他曾召集所有事業(yè)部長問道:‘...
    拾貝坊坊主閱讀 284評(píng)論 0 0
  • 2018-2-1 晴 星期四 親子日記第130篇 今天是二月的第一天,是我們核算考勤的日子,今天又逢周四,公司每周...
    敏文媽咪閱讀 232評(píng)論 0 2
  • 文/陳安若 剛搬完家的小宇很累很累,躺在新屋子的床上沉沉睡去。 不知過了多久,聽到外面鳥叫聲聲,醒來,卻發(fā)現(xiàn)自己躺...
    陳安若閱讀 1,307評(píng)論 15 11