侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

苹果(mac/ios)  /  管理员 发布于 6年前   396

首先来看看实现的效果图


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

#import 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开发们能有所帮助,如果有疑问大家可以留言交流,谢谢大家对AIDI的支持。

您可能感兴趣的文章:

  • iOS开发中ViewController的页面跳转和弹出模态
  • 解决ios模拟器不能弹出键盘问题的方法
  • react-native 封装选择弹出框示例(试用ios&android)
  • Android仿IOS底部弹出对话框
  • ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)
  • android底部弹出iOS7风格对话选项框(QQ对话框)--第三方开源之IOS_Dialog_Library
  • 高仿IOS的Android弹出框
  • iOS仿简书、淘宝等App的View弹出效果
  • iOS中自定义弹出pickerView效果(DEMO)
  • iOS仿AirPods弹出动画


  • 上一条:
    在Mac OS下搭建LNMP开发环境的步骤详解
    下一条:
    iOS10推送通知开发教程
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 苹果将在iOS 18中启用“苹果账户”名称,“Apple ID”将成历史(0个评论)
    • 2023年国内最新注册苹果开发者账号之申请邓白氏编码流程步骤(0个评论)
    • 2023年国内最新注册苹果个人开发者账号及支付会员年费流程步骤(0个评论)
    • 2022年3月2号最新免费的苹果美国id账号分享-美区Apple ID共享(0个评论)
    • Objective-C的%s和%@(1个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-11
    • 2018-11
    • 2022-01
    • 2023-02
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客