当前位置:Gxlcms > php框架 > iOS自定义提示弹出框实现类似UIAlertView的效果

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

时间:2021-07-01 10:21:17 帮助过:10人阅读

首先来看看实现的效果图


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

  1. #import <UIKit/UIKit.h>
  2. typedef void(^AlertResult)(NSInteger index);
  3. @interface XLAlertView : UIView
  4. @property (nonatomic,copy) AlertResult resultIndex;
  5. - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle;
  6. - (void)showXLAlertView;
  7. @end
  1. #import "XLAlertView.h"
  2. ///alertView 宽
  3. #define AlertW 280
  4. ///各个栏目之间的距离
  5. #define XLSpace 10.0
  6. @interface XLAlertView()
  7. //弹窗
  8. @property (nonatomic,retain) UIView *alertView;
  9. //title
  10. @property (nonatomic,retain) UILabel *titleLbl;
  11. //内容
  12. @property (nonatomic,retain) UILabel *msgLbl;
  13. //确认按钮
  14. @property (nonatomic,retain) UIButton *sureBtn;
  15. //取消按钮
  16. @property (nonatomic,retain) UIButton *cancleBtn;
  17. //横线线
  18. @property (nonatomic,retain) UIView *lineView;
  19. //竖线
  20. @property (nonatomic,retain) UIView *verLineView;
  21. @end
  22. @implementation XLAlertView
  23. - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
  24. {
  25. if (self == [super init]) {
  26. self.frame = [UIScreen mainScreen].bounds;
  27. self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
  28. self.alertView = [[UIView alloc] init];
  29. self.alertView.backgroundColor = [UIColor whiteColor];
  30. self.alertView.layer.cornerRadius = 5.0;
  31. self.alertView.frame = CGRectMake(0, 0, AlertW, 100);
  32. self.alertView.layer.position = self.center;
  33. if (title) {
  34. self.titleLbl = [self GetAdaptiveLable:CGRectMake(2*XLSpace, 2*XLSpace, AlertW-4*XLSpace, 20) AndText:title andIsTitle:YES];
  35. self.titleLbl.textAlignment = NSTextAlignmentCenter;
  36. [self.alertView addSubview:self.titleLbl];
  37. CGFloat titleW = self.titleLbl.bounds.size.width;
  38. CGFloat titleH = self.titleLbl.bounds.size.height;
  39. self.titleLbl.frame = CGRectMake((AlertW-titleW)/2, 2*XLSpace, titleW, titleH);
  40. }
  41. if (message) {
  42. self.msgLbl = [self GetAdaptiveLable:CGRectMake(XLSpace, CGRectGetMaxY(self.titleLbl.frame)+XLSpace, AlertW-2*XLSpace, 20) AndText:message andIsTitle:NO];
  43. self.msgLbl.textAlignment = NSTextAlignmentCenter;
  44. [self.alertView addSubview:self.msgLbl];
  45. CGFloat msgW = self.msgLbl.bounds.size.width;
  46. CGFloat msgH = self.msgLbl.bounds.size.height;
  47. self.msgLbl.frame = self.titleLbl?CGRectMake((AlertW-msgW)/2, CGRectGetMaxY(self.titleLbl.frame)+XLSpace, msgW, msgH):CGRectMake((AlertW-msgW)/2, 2*XLSpace, msgW, msgH);
  48. }
  49. self.lineView = [[UIView alloc] init];
  50. 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);
  51. self.lineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
  52. [self.alertView addSubview:self.lineView];
  53. //两个按钮
  54. if (cancleTitle && sureTitle) {
  55. self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  56. self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), (AlertW-1)/2, 40);
  57. [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  58. [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  59. [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
  60. //[self.cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  61. self.cancleBtn.tag = 1;
  62. [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  63. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5.0, 5.0)];
  64. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  65. maskLayer.frame = self.cancleBtn.bounds;
  66. maskLayer.path = maskPath.CGPath;
  67. self.cancleBtn.layer.mask = maskLayer;
  68. [self.alertView addSubview:self.cancleBtn];
  69. }
  70. if (cancleTitle && sureTitle) {
  71. self.verLineView = [[UIView alloc] init];
  72. self.verLineView.frame = CGRectMake(CGRectGetMaxX(self.cancleBtn.frame), CGRectGetMaxY(self.lineView.frame), 1, 40);
  73. self.verLineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
  74. [self.alertView addSubview:self.verLineView];
  75. }
  76. if(sureTitle && cancleTitle){
  77. self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  78. self.sureBtn.frame = CGRectMake(CGRectGetMaxX(self.verLineView.frame), CGRectGetMaxY(self.lineView.frame), (AlertW-1)/2+1, 40);
  79. [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  80. [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  81. [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
  82. //[self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  83. self.sureBtn.tag = 2;
  84. [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  85. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
  86. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  87. maskLayer.frame = self.sureBtn.bounds;
  88. maskLayer.path = maskPath.CGPath;
  89. self.sureBtn.layer.mask = maskLayer;
  90. [self.alertView addSubview:self.sureBtn];
  91. }
  92. //只有取消按钮
  93. if (cancleTitle && !sureTitle) {
  94. self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  95. self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), AlertW, 40);
  96. [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  97. [self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  98. [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
  99. //[self.cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  100. self.cancleBtn.tag = 1;
  101. [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  102. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
  103. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  104. maskLayer.frame = self.cancleBtn.bounds;
  105. maskLayer.path = maskPath.CGPath;
  106. self.cancleBtn.layer.mask = maskLayer;
  107. [self.alertView addSubview:self.cancleBtn];
  108. }
  109. //只有确定按钮
  110. if(sureTitle && !cancleTitle){
  111. self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  112. self.sureBtn.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame), AlertW, 40);
  113. [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  114. [self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  115. [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
  116. //[self.sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  117. self.sureBtn.tag = 2;
  118. [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  119. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
  120. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  121. maskLayer.frame = self.sureBtn.bounds;
  122. maskLayer.path = maskPath.CGPath;
  123. self.sureBtn.layer.mask = maskLayer;
  124. [self.alertView addSubview:self.sureBtn];
  125. }
  126. //计算高度
  127. CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
  128. self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
  129. self.alertView.layer.position = self.center;
  130. [self addSubview:self.alertView];
  131. }
  132. return self;
  133. }
  134. #pragma mark - 弹出 -
  135. - (void)showXLAlertView
  136. {
  137. UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
  138. [rootWindow addSubview:self];
  139. [self creatShowAnimation];
  140. }
  141. - (void)creatShowAnimation
  142. {
  143. self.alertView.layer.position = self.center;
  144. self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
  145. [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
  146. self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  147. } completion:^(BOOL finished) {
  148. }];
  149. }
  150. #pragma mark - 回调 -设置只有2 -- > 确定才回调
  151. - (void)buttonEvent:(UIButton *)sender
  152. {
  153. if (sender.tag == 2) {
  154. if (self.resultIndex) {
  155. self.resultIndex(sender.tag);
  156. }
  157. }
  158. [self removeFromSuperview];
  159. }
  160. -(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andIsTitle:(BOOL)isTitle
  161. {
  162. UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
  163. contentLbl.numberOfLines = 0;
  164. contentLbl.text = contentStr;
  165. contentLbl.textAlignment = NSTextAlignmentCenter;
  166. if (isTitle) {
  167. contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
  168. }else{
  169. contentLbl.font = [UIFont systemFontOfSize:14.0];
  170. }
  171. NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
  172. NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStyle alloc] init];
  173. mParaStyle.lineBreakMode = NSLineBreakByCharWrapping;
  174. [mParaStyle setLineSpacing:3.0];
  175. [mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0,[contentStr length])];
  176. [contentLbl setAttributedText:mAttrStr];
  177. [contentLbl sizeToFit];
  178. return contentLbl;
  179. }
  180. -(UIImage *)imageWithColor:(UIColor *)color
  181. {
  182. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  183. UIGraphicsBeginImageContext(rect.size);
  184. CGContextRef context = UIGraphicsGetCurrentContext();
  185. CGContextSetFillColorWithColor(context, [color CGColor]);
  186. CGContextFillRect(context, rect);
  187. UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  188. UIGraphicsEndImageContext();
  189. return theImage;
  190. }
  191. @end

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

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

总结

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

人气教程排行