欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中你需要的彈窗效果總結(jié)大全

 更新時(shí)間:2018年09月27日 08:36:55   作者:MrBMask  
彈窗是app中常見(jiàn)控件之一,一般由于項(xiàng)目需求,我們很少能直接使用系統(tǒng)提供的彈窗,這個(gè)時(shí)候就需要我們根據(jù)產(chǎn)品需求封裝自定義彈窗了。下面這篇文章主要給大家介紹了關(guān)于iOS中你需要的彈窗效果的相關(guān)資料,需要的朋友可以參考下

前言

彈框是人機(jī)交互中常見(jiàn)的方式,常常出現(xiàn)于詢(xún)問(wèn)、警示以及完成某個(gè)插入任務(wù),常見(jiàn)于網(wǎng)頁(yè)端及移動(dòng)端。彈框能使用戶有效聚焦于當(dāng)前最緊急的信息,也可以在不用離開(kāi)當(dāng)前頁(yè)面的前提下,完成一些輕量的任務(wù)。

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

下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

官方思路

1.在我們自己動(dòng)手之前一定要先看看官方是怎么封裝的,這樣我們寫(xiě)出來(lái)的代碼才接近蘋(píng)果語(yǔ)言,看起來(lái)高大上。好的代碼一定是見(jiàn)名知意的,別人一看這個(gè)方法就知道大概我們通過(guò)這個(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)用美滋滋。所以接下來(lái)我們封裝也是模仿官方開(kāi)始.....

delegate

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

1、首先申明協(xié)議

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

2、在viewController中遵循代理,設(shè)置代理 , 實(shí)現(xiàn)方法即可

<HLAlertViewDelegate>
self.delegate = self;

#pragma mark --- HLAlertViewDelegate
-(void)alertViewDidClickButtonWithIndex:(NSInteger)index{
if (index == AlertSureButtonClick) {
[self alertSureButtonClick];
}else{
[self alertCauseButtonClick];
}
}

3、接下來(lái)就是實(shí)現(xiàn)我們封裝類(lèi)的.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];
}

通過(guò)代理的方式我們就完成了我們自己頁(yè)面的封裝了。

block彈窗

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

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

相比代理的方式的話,我們還行喜歡這種block回調(diào)的,簡(jiǎn)大氣接地氣啊。當(dāng)然在我們需要處理邏輯多的時(shí)候,還是代理會(huì)比較好一點(diǎn),具體環(huán)境下具體使用。

封裝成block的好處就是在我們構(gòu)造方法的時(shí)候就可以實(shí)現(xiàn)我們將來(lái)的點(diǎn)擊方法,所以在自定義彈窗類(lèi)的.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的封裝彈窗

好處就是不用寫(xiě)界面代碼了。

殊途同歸

還有一種實(shí)現(xiàn)彈窗效果的方法,不通過(guò)新建view而是Controller來(lái)實(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];

更加簡(jiǎn)單,邏輯也更加好處理一些。

最后附上demo地址:gibHub地址:https://github.com/MrBMask

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

 

相關(guān)文章

  • iOS scrollview實(shí)現(xiàn)三屏復(fù)用循環(huán)廣告

    iOS scrollview實(shí)現(xiàn)三屏復(fù)用循環(huán)廣告

    這篇文章主要介紹了iOS scrollview實(shí)現(xiàn)三屏復(fù)用循環(huán)廣告,從服務(wù)器請(qǐng)求的廣告,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS13 適配和Xcode11.0踩坑小結(jié)

    iOS13 適配和Xcode11.0踩坑小結(jié)

    這篇文章主要介紹了iOS13 適配和Xcode11.0踩坑小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • iOS實(shí)現(xiàn)卡片堆疊效果

    iOS實(shí)現(xiàn)卡片堆疊效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)卡片堆疊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • iOS實(shí)現(xiàn)簡(jiǎn)單的抽屜效果

    iOS實(shí)現(xiàn)簡(jiǎn)單的抽屜效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)單的抽屜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • iOS中設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間+模擬的方法詳解

    iOS中設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間+模擬的方法詳解

    這篇文章主要介紹了在iOS中設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間+模擬的方法,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • iOS開(kāi)發(fā)中使用UIWebView 屏蔽 alert警告框

    iOS開(kāi)發(fā)中使用UIWebView 屏蔽 alert警告框

    這篇文章主要介紹了iOS開(kāi)發(fā)中使用UIWebView 屏蔽 alert警告框的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • IOS代碼筆記之文字走馬燈效果

    IOS代碼筆記之文字走馬燈效果

    這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)文字走馬燈效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新

    詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新

    這篇文章主要介紹了iOS App中UITableView的創(chuàng)建與內(nèi)容刷新,講解了UITableView一些基本的樣式與cell的設(shè)置及刷新,需要的朋友可以參考下
    2016-04-04
  • iOS archive保存圖片到本地的方法

    iOS archive保存圖片到本地的方法

    這篇文章主要為大家詳細(xì)介紹了iOS archive保存圖片到本地的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果

    iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09

最新評(píng)論