自定义的AlertView,可以选择出现的动画方式,正文信息高度自动变化,特意做了几个可以对比。没啥难点,直接上代码,一看就懂。

  1.在YYTAlertView.h文件中

//

//  YYTAlertView.h

//  Demo-自定义alertView

//

//  Created by yyt on 16/4/19.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger , ShowAnimationStyle) {

AnimationDefault = 0,

AnimationLeftShake  ,

AnimationTopShake   ,

AnimationNO         ,

};

typedef void(^AlertClickIndexBlock)(NSInteger clickIndex);

@interface YYTAlertView : UIView

@property (nonatomic,copy) AlertClickIndexBlock clickBlock;

@property (nonatomic,assign) ShowAnimationStyle animationStyle;

- (instancetype)initWithTitle:(NSString *)title AndMessage:(NSString *)message AndCancelBtnTitle:(NSString *)cancelTitle AndOtherBtnTitle:(NSString *)otherBtnTitle AndClickIndexBlock:(AlertClickIndexBlock)block;

-(void)showAlertView;

@end

  2.在YYTAlertView.m文件中

//

//  YYTAlertView.m

//  Demo-自定义alertView

//

//  Created by yyt on 16/4/19.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "YYTAlertView.h"

#define MainScreenRect [UIScreen mainScreen].bounds

#define AlertView_W 270.0f

@interface YYTAlertView ()

@property (nonatomic,strong)UIWindow *alertWindow;

@property (nonatomic,strong)UIView *alertView;

@property (nonatomic,strong)UILabel *titleLab;

@property (nonatomic,strong)UILabel *messageLab;

@property (nonatomic,strong)UIButton *cancelBtn;

@property (nonatomic,strong)UIButton *otherBtn;

@end

@implementation YYTAlertView

- (instancetype)initWithTitle:(NSString *)title AndMessage:(NSString *)message AndCancelBtnTitle:(NSString *)cancelTitle AndOtherBtnTitle:(NSString *)otherBtnTitle AndClickIndexBlock:(AlertClickIndexBlock)block{

if(self=[super init]){

self.frame = MainScreenRect;

self.backgroundColor=[UIColor colorWithWhite:.3 alpha:.7];

_alertView=[[UIView alloc] init];

_alertView.backgroundColor=[UIColor whiteColor];

_alertView.layer.cornerRadius=6.0;

_alertView.layer.masksToBounds=YES;

_alertView.userInteractionEnabled=YES;

if (title) {

_titleLab=[[UILabel alloc] initWithFrame:CGRectMake(0, 10, AlertView_W, 20)];

_titleLab.text=title;

_titleLab.textAlignment=NSTextAlignmentCenter;

_titleLab.textColor=[UIColor blackColor];

_titleLab.font=[UIFont systemFontOfSize:17];

}

_messageLab=[[UILabel alloc] init];

_messageLab.backgroundColor=[UIColor whiteColor];

_messageLab.text=message;

_messageLab.textColor=[UIColor lightGrayColor];

_messageLab.textAlignment=NSTextAlignmentCenter;

_messageLab.font=[UIFont systemFontOfSize:14];

_messageLab.numberOfLines=0;

CGRect rectOfText = CGRectMake(20, _titleLab.frame.size.height+_titleLab.frame.origin.y+10, AlertView_W-40, 999);

rectOfText = [_messageLab textRectForBounds:rectOfText limitedToNumberOfLines:0];

_messageLab.frame = rectOfText;

//计算_alertView的高度

_alertView.frame=CGRectMake(0, 0, AlertView_W, _messageLab.frame.size.height+90);

_alertView.center=self.center;

[self addSubview:_alertView];

[_alertView addSubview:_titleLab];

[_alertView addSubview:_messageLab];

if (cancelTitle) {

_cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];

[_cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];

[_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[_cancelBtn setBackgroundColor:[UIColor lightGrayColor]];

_cancelBtn.titleLabel.font=[UIFont systemFontOfSize:15];

_cancelBtn.layer.cornerRadius=3;

_cancelBtn.layer.masksToBounds=YES;

[_cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

[_alertView addSubview:_cancelBtn];

}

if (otherBtnTitle) {

_otherBtn=[UIButton buttonWithType:UIButtonTypeCustom];

[_otherBtn setTitle:otherBtnTitle forState:UIControlStateNormal];

[_otherBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

_otherBtn.titleLabel.font=[UIFont systemFontOfSize:15];

_otherBtn.layer.cornerRadius=3;

_otherBtn.layer.masksToBounds=YES;

[_otherBtn setBackgroundColor:[UIColor redColor]];

[_otherBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

[_alertView addSubview:_otherBtn];

}

CGFloat btnLeftSpace = 40;//btn到左边距

CGFloat btn_y = _alertView.frame.size.height-40;

if (cancelTitle && !otherBtnTitle) {

_cancelBtn.tag=0;

_cancelBtn.frame=CGRectMake(btnLeftSpace, btn_y, AlertView_W-btnLeftSpace*2, 30);

}else if (!cancelTitle && otherBtnTitle){

_otherBtn.tag=0;

_otherBtn.frame=CGRectMake(btnLeftSpace, btn_y, AlertView_W-btnLeftSpace*2, 30);

}else if (cancelTitle && otherBtnTitle){

_cancelBtn.tag=0;

_otherBtn.tag=1;

CGFloat btnSpace = 20;//两个btn之间的间距

CGFloat btn_w =(AlertView_W-btnLeftSpace*2-btnSpace)/2;

_cancelBtn.frame=CGRectMake(btnLeftSpace, btn_y, btn_w, 30);

_otherBtn.frame=CGRectMake(_alertView.frame.size.width-btn_w-btnLeftSpace, btn_y, btn_w, 30);

}

self.clickBlock=block;

}

return self;

}

-(void)btnClick:(UIButton *)btn{

if (self.clickBlock) {

self.clickBlock(btn.tag);

}

[self dismissAlertView];

}

-(void)showAlertView{

_alertWindow=[[UIWindow alloc] initWithFrame:MainScreenRect];

_alertWindow.windowLevel=UIWindowLevelAlert;

[_alertWindow becomeKeyWindow];

[_alertWindow makeKeyAndVisible];

[_alertWindow addSubview:self];

[self setShowAnimation];

}

-(void)dismissAlertView{

[self removeFromSuperview];

[_alertWindow resignKeyWindow];

}

-(void)setShowAnimation{

switch (_animationStyle) {

case AnimationDefault:

{

[UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

[_alertView.layer setValue:@(0) forKeyPath:@"transform.scale"];

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.23 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

[_alertView.layer setValue:@(1.2) forKeyPath:@"transform.scale"];

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.09 delay:0.02 options:UIViewAnimationOptionCurveEaseInOut animations:^{

[_alertView.layer setValue:@(.9) forKeyPath:@"transform.scale"];

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.05 delay:0.02 options:UIViewAnimationOptionCurveEaseInOut animations:^{

[_alertView.layer setValue:@(1.0) forKeyPath:@"transform.scale"];

} completion:^(BOOL finished) {

}];

}];

}];

}];

}

break;

case AnimationLeftShake:{

CGPoint startPoint = CGPointMake(-AlertView_W, self.center.y);

_alertView.layer.position=startPoint;

//damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显

//velocity:弹性复位的速度

[UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

_alertView.layer.position=self.center;

} completion:^(BOOL finished) {

}];

}

break;

case AnimationTopShake:{

CGPoint startPoint = CGPointMake(self.center.x, -_alertView.frame.size.height);

_alertView.layer.position=startPoint;

//damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显

//velocity:弹性复位的速度

[UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

_alertView.layer.position=self.center;

} completion:^(BOOL finished) {

}];

}

break;

case AnimationNO:{

}

break;

default:

break;

}

}

-(void)setAnimationStyle:(ShowAnimationStyle)animationStyle{

_animationStyle=animationStyle;

}

@end

  3.在需要的地方调用

//

//  ViewController.m

//  Demo-自定义alertView

//

//  Created by yyt on 16/4/19.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "ViewController.h"

#import "YYTAlertView.h"

#define klScreenWidth self.view.bounds.size.width

@interface ViewController ()<UIAlertViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];

button0.frame = CGRectMake(20, 100, klScreenWidth-40, 40);

button0.backgroundColor = [UIColor orangeColor];

[button0 setTitle:@"systemAlertView" forState:UIControlStateNormal];

[button0 addTarget:self action:@selector(clickButton0:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button0];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

button1.frame = CGRectMake(20, 160, klScreenWidth-40, 40);

button1.backgroundColor = [UIColor orangeColor];

[button1 setTitle:@"customAlertView1" forState:UIControlStateNormal];

[button1 addTarget:self action:@selector(clickButton1:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

button2.frame = CGRectMake(20, 220, klScreenWidth-40, 40);

button2.backgroundColor = [UIColor orangeColor];

[button2 setTitle:@"customAlertView2" forState:UIControlStateNormal];

[button2 addTarget:self action:@selector(clickButton2:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];

button3.frame = CGRectMake(20, 280, klScreenWidth-40, 40);

button3.backgroundColor = [UIColor orangeColor];

[button3 setTitle:@"customAlertView3" forState:UIControlStateNormal];

[button3 addTarget:self action:@selector(clickButton3:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button3];

}

- (void)clickButton0:(UIButton*)sender {

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"系统的AlertView" message:@"hehe" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSLog(@"系统alert==%ld",buttonIndex);

}

- (void)clickButton1:(UIButton*)sender {

YYTAlertView *alert=[[YYTAlertView alloc] initWithTitle:@"自定义AlertView1" AndMessage:@"默认缩放出现" AndCancelBtnTitle:@"取消" AndOtherBtnTitle:@"确定" AndClickIndexBlock:^(NSInteger clickIndex) {

NSLog(@"点击自定义AlertView1====%ld",clickIndex);

}];

[alert showAlertView];

}

- (void)clickButton2:(UIButton*)sender {

YYTAlertView *alert=[[YYTAlertView alloc] initWithTitle:@"自定义AlertView2" AndMessage:@"自顶部出现" AndCancelBtnTitle:@"取消" AndOtherBtnTitle:@"确定" AndClickIndexBlock:^(NSInteger clickIndex) {

NSLog(@"点击自定义AlertView2====%ld",clickIndex);

}];

alert.animationStyle = AnimationTopShake;

[alert showAlertView];

}

- (void)clickButton3:(UIButton*)sender {

YYTAlertView *alert=[[YYTAlertView alloc] initWithTitle:@"自定义AlertView3" AndMessage:@"从左边出现" AndCancelBtnTitle:@"取消" AndOtherBtnTitle:@"确定" AndClickIndexBlock:^(NSInteger clickIndex) {

NSLog(@"点击自定义AlertView3====%ld",clickIndex);

}];

alert.animationStyle = AnimationLeftShake;

[alert showAlertView];

}

@end

iOS开发——自定义AlertView的更多相关文章

  1. iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析

    做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...

  2. iOS开发自定义字体之静态字体

    最后更新 2017-04-25 在iOS开发中经常会用到字体, 一般字体文件比较小的,单一的,几十k, 可以通过内置进去;如果字体文件比较多或者字体文件比较大,通常通过动态加载方式. 静态加载方式 将 ...

  3. iOS 开发自定义一个提示框

    在开发的时候,会碰到很多需要提示的地方,提示的方法也有很多种,ios 8 以前的版本有alertview还是以后用的alertController,都是这种作用, 但是不够灵活,而且用的多了,用户体验 ...

  4. iOS开发-自定义UIAlterView(iOS 7)

    App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作.iOS7之前是可以自定 ...

  5. [IOS 开发] 自定义(重写) UITableViewCell的高亮背景色

    IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlue和UITableViewCellSelection ...

  6. IOS开发自定义CheckBox控件

    IOS本身没有系统的CheckBox组件,但是实际开发中会经常用到,所以专门写了一个CheckBox控件,直接上代码 效果图: UICheckBoxButton.h文件如下: #import #imp ...

  7. IOS开发自定义tableviewcell的注意点😄

    自定义tableviewcell 1.xib,nib拖控件:awakefromnib: 设置2,不拖控件:- (instancetype)initWithStyle:(UITableViewCellS ...

  8. iOS开发自定义流水布局

    //集成UICollectionViewFlowLayout 自己写的布局 //  SJBFlowLayout.m //  自定义流水布局 // //  Created by zyyt on 16/7 ...

  9. iOS开发 自定义UIAlertController的样式

    引言: 关于提示框, 系统自带的提示框有时可能满足不了我们的需求, 比如一个提示框的取消按钮我需要灰色字体显示, 这时候就需要自定义提示框的样式了. 示例图 苹果自iOS8开始,就已经废弃了之前用于界 ...

随机推荐

  1. 最短路径问题/Spfa

    题目链接 题目描述 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线, 则输出花费最少的. 最后一行是两个数 s,t;起 ...

  2. Swift: Alamofire -> http请求 & ObjectMapper -> 解析JSON

    1 2 3 4 5 6 7 8 9 10 11 NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.js ...

  3. java 持有对象

    1.泛型和类型安全的容器 ArrayList,可以自动扩充大小的数组,add插入对象,get访问对象,size查看对象数目. 1 /** 2 * 泛型和类型安全的容器 3 * 2016/5/6 4 * ...

  4. MySql数据库基本介绍和基本语法

    一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系统中,关系型数据库使用最为 ...

  5. Java学习之位运算符

    位运算符:&,|,^,~,<<,>> & (按位与):只有对应的两个二进制位均为1时,结果才为1.例如,9&5,即00001001&000001 ...

  6. HDU1372,BFS象棋马走日

    简单的对于bfs的运用,但是还是写的太慢了写了TMD的1H,主要是不熟悉,以后慢慢熟悉就好了,模型基本已经能建立了,主要出现bug是在方向数组的运用上面,一定要记得是从0开始的,而不是从1开始的,导致 ...

  7. JSP内置对象--web安全性及config对象的使用 (了解即可)

    tomcat服务器配置的时候,在虚拟目录中必须存在一个WEB-INF文件夹,但是访问的时候并不能发现这个文件夹.改成WEB-INFs就可以看到. 所以WEB-INF文件夹不轻易让用户看到,那么其安全性 ...

  8. gc CMSMaxAbortablePrecleanTime

    https://blogs.oracle.com/jonthecollector/ 链接打不开了,我记得里边说,这个参数是指定vm试图在它指定的时间间隔内等到一次小垃圾回收...minor gc, g ...

  9. Integer自动装箱拆箱bug,创建对象在-128到127

    1 public class Demo3 { public static void main(String[] args) { Integer a = 1; Integer b = 2; Intege ...

  10. StreamReader 读取文本文件乱码问题

    解决读取文本文件乱码问题.我采取的是读取前先判断文本文件格式. StreamReader sr = new StreamReader(fullfileName, GetFileEncodeType(f ...