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


Posted in PHP onNovember 16, 2016

首先来看看实现的效果图

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

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

#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
#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

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

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

总结

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

PHP 相关文章推荐
php缓存技术介绍
Nov 25 PHP
PHP 基于Yii框架中使用smarty模板的方法详解
Jun 13 PHP
Thinkphp的volist标签嵌套循环使用教程
Jul 08 PHP
跨浏览器PHP下载文件名中的中文乱码问题解决方法
Mar 05 PHP
php实现将任意进制数转换成10进制的方法
Apr 17 PHP
phpStudy访问速度慢和启动失败的解决办法
Nov 19 PHP
php通过两层过滤获取留言内容的方法
Jul 11 PHP
Thinkphp3.2.3整合phpqrcode生成带logo的二维码
Jul 21 PHP
PHP表单验证内容是否为空的实现代码
Nov 14 PHP
PHP封装函数实现生成随机的字符串验证码
Jan 24 PHP
php实现微信公众平台发红包功能
Jun 14 PHP
laravel清除视图缓存的代码
Oct 23 PHP
PHP中串行化用法示例
Nov 16 #PHP
PHP单态模式简单用法示例
Nov 16 #PHP
php实现替换手机号中间数字为*号及隐藏IP最后几位的方法
Nov 16 #PHP
php实现当前页面点击下载文件的实例代码
Nov 16 #PHP
php 文件下载 出现下载文件内容乱码损坏的解决方法(推荐)
Nov 16 #PHP
PHP Header失效的原因分析及解决方法
Nov 16 #PHP
php把时间戳转换成多少时间之前函数的实例
Nov 16 #PHP
You might like
资料注册后发信小技巧
2006/10/09 PHP
php绝对路径与相对路径之间关系的的分析
2010/03/03 PHP
PHP函数引用返回的实例详解
2016/09/11 PHP
php实现基于pdo的事务处理方法示例
2017/07/21 PHP
php源码的安装方法和实例
2019/09/26 PHP
非阻塞动态加载javascript广告实现代码
2010/11/17 Javascript
javascript轻量级模板引擎juicer使用指南
2014/06/22 Javascript
jQuery判断一个元素是否可见的方法
2015/06/05 Javascript
深入理解JS正则表达式---分组
2016/07/18 Javascript
微信小程序 swiper组件轮播图详解及实例
2016/11/16 Javascript
js实现分页功能
2017/05/24 Javascript
微信小程序使用Socket的实例
2017/09/19 Javascript
three.js实现3D影院的原理的代码分析
2017/12/18 Javascript
vuejs实现ready函数加载完之后执行某个函数的方法
2018/08/31 Javascript
react写一个select组件的实现代码
2019/04/03 Javascript
微信小程序判断页面是否从其他页面返回的实例代码
2019/07/03 Javascript
JS实现商品橱窗特效
2020/01/09 Javascript
JS原型prototype和__proto__用法实例分析
2020/03/14 Javascript
JavaScript 实现自己的安卓手机自动化工具脚本(推荐)
2020/05/13 Javascript
简单总结Python中序列与字典的相同和不同之处
2016/01/19 Python
Python对List中的元素排序的方法
2018/04/01 Python
Python中PyQt5/PySide2的按钮控件使用实例
2019/08/17 Python
python excel多行合并的方法
2020/12/09 Python
TensorFlow2.0使用keras训练模型的实现
2021/02/20 Python
VSCode中autopep8无法运行问题解决方案(提示Error: Command failed,usage)
2021/03/02 Python
Boden澳大利亚官网:英国在线服装公司
2018/08/05 全球购物
英国豪华家具和家居用品购物网站:Teddy Beau
2020/10/12 全球购物
心理健康心得体会
2014/01/02 职场文书
公司股权转让协议书
2014/04/12 职场文书
国际经济与贸易专业求职信
2014/07/10 职场文书
月度优秀员工获奖感言
2014/08/16 职场文书
普通党员整改措施
2014/10/24 职场文书
总经理助理岗位职责范本
2015/03/31 职场文书
企业投资意向书
2015/05/09 职场文书
离婚纠纷代理词
2015/05/23 职场文书
详解mysql三值逻辑与NULL
2021/05/19 MySQL