- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章iOS中你需要的弹窗效果总结大全由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
弹框是人机交互中常见的方式,常常出现于询问、警示以及完成某个插入任务,常见于网页端及移动端。弹框能使用户有效聚焦于当前最紧急的信息,也可以在不用离开当前页面的前提下,完成一些轻量的任务.
在我们的实际开发项目中,弹窗是必不可少的,很多时候我们用的是系统的alertviewcontroller,但是实际情况中,并不能满足我们的开发需求,这个时候我们需要的就是自定义自己的弹窗效果。接下来我会写一些自己的所封装的弹窗效果。包括代理delegate回调,block 回调,xib新建view来创建我们需要的弹窗效果.
下面话不多说了,来一起看看详细的介绍吧 。
官方思路 。
1.在我们自己动手之前一定要先看看官方是怎么封装的,这样我们写出来的代码才接近苹果语言,看起来高大上。好的代码一定是见名知意的,别人一看这个方法就知道大概我们通过这个方法可以得到什么样的效果.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 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];
|
因为在代码量风格上,我还是比较喜欢老版本的弹窗,毕竟代码上啊,一句话调用美滋滋。所以接下来我们封装也是模仿官方开始..... 。
delegate 。
我们可以看到在苹果官方中,我们需要通过识别用户点击某个按钮来确定需要进一步的操作事件,这个时候是通过代理来实现的。代理的话,我们在熟悉不过了.
1、首先申明协议 。
1
2
3
4
5
|
#pragma mark - 协议
@
class
hlalertview;
@protocol hlalertviewdelegate<nsobject>
- (
void
)alertviewdidclickbuttonwithindex:(nsinteger)index;
@end
|
2、在viewcontroller中遵循代理,设置代理 , 实现方法即可 。
1
2
3
4
5
6
7
8
9
10
11
|
<hlalertviewdelegate>
self.delegate = self;
#pragma mark --- hlalertviewdelegate
-(
void
)alertviewdidclickbuttonwithindex:(nsinteger)index{
if
(index == alertsurebuttonclick) {
[self alertsurebuttonclick];
}
else
{
[self alertcausebuttonclick];
}
}
|
3、接下来就是实现我们封装类的.h文件方法申明,以及.m的实现方法 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//.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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
@interface hlalertview()
/** 弹窗主内容view */
@property (nonatomic,strong) uiview *contentview;
/** 弹窗标题 */
@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;
}];
//------- 弹窗主内容 -------//
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];
// 标题
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];
// 确认按钮
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弹窗 。
先看一下封装之后我们的调用方式吧:
1
2
3
4
5
6
7
8
|
hlalertviewblock * alertview = [[hlalertviewblock alloc] initwithtittle:@
"提示"
message:@
"通过block弹窗回调的弹窗"
block:^(nsinteger index) {
if
(index == alertsurebuttonclick) {
[self alertsurebuttonclick];
}
else
{
[self alertcausebuttonclick];
}
}];
[alertview show];
|
相比代理的方式的话,我们还行喜欢这种block回调的,简大气接地气啊。当然在我们需要处理逻辑多的时候,还是代理会比较好一点,具体环境下具体使用.
封装成block的好处就是在我们构造方法的时候就可以实现我们将来的点击方法,所以在自定义弹窗类的.h文件中,我们要申明block属性。代码 。
1
2
3
4
5
6
7
8
9
10
|
//.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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//.m
@interface hlalertviewblock()
/** 弹窗主内容view */
@property (nonatomic,strong) uiview *contentview;
/** 弹窗标题 */
@property (nonatomic,copy) nsstring *title;
/** message */
@property (nonatomic,copy) nsstring *message;
/** 确认按钮 */
@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的封装弹窗 。
好处就是不用写界面代码了.
殊途同归 。
还有一种实现弹窗效果的方法,不通过新建view而是controller来实现的,就是新建一个透明的控制器。代码如下 。
1
2
3
4
5
|
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 。
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.
原文链接:https://www.jianshu.com/p/817c8e71f1f8 。
最后此篇关于iOS中你需要的弹窗效果总结大全的文章就讲到这里了,如果你想了解更多关于iOS中你需要的弹窗效果总结大全的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!