iOS中你需要的d窗效果总结大全

iOS中你需要的d窗效果总结大全,第1张

概述iOS中你需要的d窗效果总结大全 前言 d框是人机交互中常见的方式,常常出现于询问.警示以及完成某个插入任务,常见于网页端及移动端.d框能使用户有效聚焦于当前最紧急的信息,也可以在不用离开当前页面的前提下,完成一些轻量的任务. 在我们的实际开发项目中,d窗是必不可少的,很多时候我们用的是系统的AlertViewController,但是实际情况中,并不能满足我们的开发需求,这个时候我们需要的就是自定义自己的d窗效果.接下来我会写一些自己的所封装的d窗效果.包括代理delegate回调,block 回调,xib新建view来创建我

@H_403_1@前言

d框是人机交互中常见的方式,常常出现于询问、警示以及完成某个插入任务,常见于网页端及移动端。d框能使用户有效聚焦于当前最紧急的信息,也可以在不用离开当前页面的前提下,完成一些轻量的任务。

在我们的实际开发项目中,d窗是必不可少的,很多时候我们用的是系统的AlertVIEwController,但是实际情况中,并不能满足我们的开发需求,这个时候我们需要的就是自定义自己的d窗效果。接下来我会写一些自己的所封装的d窗效果。包括代理delegate回调,block 回调,xib新建vIEw来创建我们需要的d窗效果。

下面话不多说了,来一起看看详细的介绍吧

@H_403_1@官方思路

1.在我们自己动手之前一定要先看看官方是怎么封装的,这样我们写出来的代码才接近苹果语言,看起来高大上。好的代码一定是见名知意的,别人一看这个方法就知道大概我们通过这个方法可以得到什么样的效果。

// 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:selfcancelbuttonTitle:@"cancel" otherbuttonTitles:nil,nil];[alertVIEw show];

因为在代码量风格上,我还是比较喜欢老版本的d窗,毕竟代码上啊,一句话调用美滋滋。所以接下来我们封装也是模仿官方开始.....

@H_403_1@delegate

我们可以看到在苹果官方中,我们需要通过识别用户点击某个按钮来确定需要进一步的 *** 作事件,这个时候是通过代理来实现的。代理的话,我们在熟悉不过了。

1、首先申明协议

#pragma mark - 协议@class HLAlertVIEw;@protocol HLAlertVIEwDelegate<NSObject>- (voID)alertVIEwDIDClickbuttonWithIndex:(NSInteger)index;@end

2、在vIEwController中遵循代理,设置代理 , 实现方法即可

<HLAlertVIEwDelegate>self.delegate = self;#pragma mark --- HLAlertVIEwDelegate-(voID)alertVIEwDIDClickbuttonWithIndex:(NSInteger)index{if (index == AlertSurebuttonClick) {[self alertSurebuttonClick];}else{[self alertCausebuttonClick];}}

3、接下来就是实现我们封装类的.h文件方法申明,以及.m的实现方法

//.h 文件#import <UIKit/UIKit.h>typedef enum : NSUInteger {AlertCausebuttonClick = 0,AlertSurebuttonClick} AlertbuttonClickIndex;#pragma mark - 协议@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()/** d窗主内容vIEw */@property (nonatomic,strong) UIVIEw *contentVIEw;/** d窗标题 */@property (nonatomic,copy) Nsstring *Title;/** message */@property (nonatomic,copy) Nsstring *message;/** 确认按钮 */@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;}];//------- d窗主内容 -------//self.contentVIEw = [[UIVIEw alloc]init];self.contentVIEw.frame = CGRectMake(0,SCREEN_WIDTH - 80,150);self.contentVIEw.center = self.center;self.contentVIEw.backgroundcolor = [UIcolor whitecolor];self.contentVIEw.layer.cornerRadius = 6;[self addSubvIEw:self.contentVIEw];// 标题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];// messageUILabel *messageLable = [[UILabel alloc]initWithFrame:CGRectMake(0,50,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];// 确认按钮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 - 移除此d窗/** 移除此d窗 */- (voID)dismiss{[self removeFromSupervIEw];}

通过代理的方式我们就完成了我们自己页面的封装了。

@H_403_1@blockd窗

先看一下封装之后我们的调用方式吧:

HLAlertVIEwBlock * alertVIEw = [[HLAlertVIEwBlock alloc] initWithTittle:@"提示" message:@"通过Blockd窗回调的d窗" block:^(NSInteger index) {if (index == AlertSurebuttonClick) {[self alertSurebuttonClick];}else{[self alertCausebuttonClick];}}];[alertVIEw show];

相比代理的方式的话,我们还行喜欢这种block回调的,简大气接地气啊。当然在我们需要处理逻辑多的时候,还是代理会比较好一点,具体环境下具体使用。

封装成block的好处就是在我们构造方法的时候就可以实现我们将来的点击方法,所以在自定义d窗类的.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()/** d窗主内容vIEw */@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;}

到此为止,我们的blockd窗申明方法也搞定了。

@H_403_1@xib的封装d窗

好处就是不用写界面代码了。

@H_403_1@殊途同归

还有一种实现d窗效果的方法,不通过新建vIEw而是Controller来实现的,就是新建一个透明的控制器。代码如下

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

@H_403_1@总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

总结

以上是内存溢出为你收集整理的iOS中你需要的d窗效果总结大全全部内容,希望文章能够帮你解决iOS中你需要的d窗效果总结大全所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1110117.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存