服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS自定义提示弹出框实现类似UIAlertView的效果

iOS自定义提示弹出框实现类似UIAlertView的效果

2021-03-26 17:48键盘上的演绎者 IOS

最近在学习iOS开发,在牛刀小试的时候发现系统的UIAlertView有点不喜欢,然后就自己自定义了一个UIAlertView,基本上实现了系统的UIAlertView,可以根据项目的需求修改UIAlertView的颜色。有需要的朋友们可以参考借鉴,也可以多多指导,下

首先来看看实现的效果图

iOS自定义提示弹出框实现类似UIAlertView的效果

下面话不多说,以下是实现的示例代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#import <UIKit/UIKit.h>
 
typedef void(^AlertResult)(NSInteger index);
 
@interface XLAlertView : UIView
 
@property (nonatomic,copy) AlertResult resultIndex;
 
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle;
 
- (void)showXLAlertView;
 
@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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#import "XLAlertView.h"
 
///alertView 宽
#define AlertW 280
///各个栏目之间的距离
#define XLSpace 10.0
 
@interface XLAlertView()
 
//弹窗
@property (nonatomic,retain) UIView *alertView;
//title
@property (nonatomic,retain) UILabel *titleLbl;
//内容
@property (nonatomic,retain) UILabel *msgLbl;
//确认按钮
@property (nonatomic,retain) UIButton *sureBtn;
//取消按钮
@property (nonatomic,retain) UIButton *cancleBtn;
//横线线
@property (nonatomic,retain) UIView *lineView;
//竖线
@property (nonatomic,retain) UIView *verLineView;
 
@end
 
@implementation XLAlertView
 
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
{
  if (self == [super init]) {
 
    self.frame = [UIScreen mainScreen].bounds;
 
    self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
 
    self.alertView = [[UIView alloc] init];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 5.0;
 
    self.alertView.frame = CGRectMake(0, 0, AlertW, 100);
    self.alertView.layer.position = self.center;
 
    if (title) {
 
      self.titleLbl = [self GetAdaptiveLable:CGRectMake(2*XLSpace, 2*XLSpace, AlertW-4*XLSpace, 20) AndText:title andIsTitle:YES];
      self.titleLbl.textAlignment = NSTextAlignmentCenter;
 
      [self.alertView addSubview:self.titleLbl];
 
      CGFloat titleW = self.titleLbl.bounds.size.width;
      CGFloat titleH = self.titleLbl.bounds.size.height;
 
      self.titleLbl.frame = CGRectMake((AlertW-titleW)/2, 2*XLSpace, titleW, titleH);
 
    }
    if (message) {
 
      self.msgLbl = [self GetAdaptiveLable:CGRectMake(XLSpace, CGRectGetMaxY(self.titleLbl.frame)+XLSpace, AlertW-2*XLSpace, 20) AndText:message andIsTitle:NO];
      self.msgLbl.textAlignment = NSTextAlignmentCenter;
 
      [self.alertView addSubview:self.msgLbl];
 
      CGFloat msgW = self.msgLbl.bounds.size.width;
      CGFloat msgH = self.msgLbl.bounds.size.height;
 
      self.msgLbl.frame = self.titleLbl?CGRectMake((AlertW-msgW)/2, CGRectGetMaxY(self.titleLbl.frame)+XLSpace, msgW, msgH):CGRectMake((AlertW-msgW)/2, 2*XLSpace, msgW, msgH);
    }
 
    self.lineView = [[UIView alloc] init];
    self.lineView.frame = self.msgLbl?CGRectMake(0, CGRectGetMaxY(self.msgLbl.frame)+2*XLSpace, AlertW, 1):CGRectMake(0, CGRectGetMaxY(self.titleLbl.frame)+2*XLSpace, AlertW, 1);
    self.lineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
    [self.alertView addSubview:self.lineView];
 
    //两个按钮
    if (cancleTitle && sureTitle) {
 
      self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
      self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), (AlertW-1)/2, 40);
      [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
      [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
      [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
      //[self.cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
      self.cancleBtn.tag = 1;
      [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
 
      UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5.0, 5.0)];
      CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
      maskLayer.frame = self.cancleBtn.bounds;
      maskLayer.path = maskPath.CGPath;
      self.cancleBtn.layer.mask = maskLayer;
 
      [self.alertView addSubview:self.cancleBtn];
    }
 
    if (cancleTitle && sureTitle) {
      self.verLineView = [[UIView alloc] init];
      self.verLineView.frame = CGRectMake(CGRectGetMaxX(self.cancleBtn.frame), CGRectGetMaxY(self.lineView.frame), 1, 40);
      self.verLineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
      [self.alertView addSubview:self.verLineView];
    }
 
    if(sureTitle && cancleTitle){
 
      self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
      self.sureBtn.frame = CGRectMake(CGRectGetMaxX(self.verLineView.frame), CGRectGetMaxY(self.lineView.frame), (AlertW-1)/2+1, 40);
      [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
      [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
      [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
      //[self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
      self.sureBtn.tag = 2;
      [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
 
      UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
      CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
      maskLayer.frame = self.sureBtn.bounds;
      maskLayer.path = maskPath.CGPath;
      self.sureBtn.layer.mask = maskLayer;
 
      [self.alertView addSubview:self.sureBtn];
 
    }
 
    //只有取消按钮
    if (cancleTitle && !sureTitle) {
 
      self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
      self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), AlertW, 40);
      [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
      [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
      [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
      //[self.cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
      self.cancleBtn.tag = 1;
      [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
 
      UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
      CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
      maskLayer.frame = self.cancleBtn.bounds;
      maskLayer.path = maskPath.CGPath;
      self.cancleBtn.layer.mask = maskLayer;
 
      [self.alertView addSubview:self.cancleBtn];
    }
 
    //只有确定按钮
    if(sureTitle && !cancleTitle){
 
      self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
      self.sureBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), AlertW, 40);
      [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
      [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
      [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
      //[self.sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
      self.sureBtn.tag = 2;
      [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
 
      UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
      CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
      maskLayer.frame = self.sureBtn.bounds;
      maskLayer.path = maskPath.CGPath;
      self.sureBtn.layer.mask = maskLayer;
 
      [self.alertView addSubview:self.sureBtn];
 
    }
 
    //计算高度
    CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
    self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
    self.alertView.layer.position = self.center;
 
    [self addSubview:self.alertView];
  }
 
  return self;
}
 
#pragma mark - 弹出 -
- (void)showXLAlertView
{
  UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
  [rootWindow addSubview:self];
  [self creatShowAnimation];
}
 
- (void)creatShowAnimation
{
  self.alertView.layer.position = self.center;
  self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
  [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
    self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  } completion:^(BOOL finished) {
  }];
}
 
#pragma mark - 回调 -设置只有2 -- > 确定才回调
- (void)buttonEvent:(UIButton *)sender
{
  if (sender.tag == 2) {
    if (self.resultIndex) {
      self.resultIndex(sender.tag);
    }
  }
  [self removeFromSuperview];
}
 
-(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andIsTitle:(BOOL)isTitle
{
  UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
  contentLbl.numberOfLines = 0;
  contentLbl.text = contentStr;
  contentLbl.textAlignment = NSTextAlignmentCenter;
  if (isTitle) {
    contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
  }else{
    contentLbl.font = [UIFont systemFontOfSize:14.0];
  }
 
  NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
  NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStyle alloc] init];
  mParaStyle.lineBreakMode = NSLineBreakByCharWrapping;
  [mParaStyle setLineSpacing:3.0];
  [mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0,[contentStr length])];
  [contentLbl setAttributedText:mAttrStr];
  [contentLbl sizeToFit];
 
  return contentLbl;
}
 
-(UIImage *)imageWithColor:(UIColor *)color
{
  CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  UIGraphicsBeginImageContext(rect.size);
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetFillColorWithColor(context, [color CGColor]);
  CGContextFillRect(context, rect);
  UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return theImage;
}
 
@end

在需要使用的地方直接调用

?
1
2
3
4
5
XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:@"自定义UIAlertView" message:@"不喜勿喷,大神多多指导。不胜感激" sureBtn:@"确认" cancleBtn:@"取消"];
xlAlertView.resultIndex = ^(NSInteger index){
//回调---处理一系列动作
};
[xlAlertView showXLAlertView];

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发们能有所帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.jianshu.com/p/e15d21c87b82

延伸 · 阅读

精彩推荐
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22