iOS开发——自定义AlertView

  自定义的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

时间: 2024-10-12 11:29:54

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

IOS开发自定义CheckBox控件

IOS本身没有系统的CheckBox组件,但是实际开发中会经常用到,所以专门写了一个CheckBox控件,直接上代码 效果图: UICheckBoxButton.h文件如下: #import #import "Common.h" @interface UICheckBoxButton : UIControl { UILabel *label; UIImageView *icon; BOOL checked; id delegate; } @property (retain, nonat

iOS开发-自定义后台显示图片(iOS7-Background Fetch的应用)

之前在用电池医生的时候, 发现它有这样一个功能:当应用进入后台的时候, 会显示另外一张图片覆盖App Switcher显示的界面. 效果如下: 变成----> 而这样的一个功能, 对于保护用户隐私还是挺有用的. 这就涉及到了Background Fetch的使用.当然, Background Fetch有更多,更有用的功能, 详见-->iOS 7学习:多任务处理之Background Fetch 下面就介绍下后台图片切换的实现. 1.程序配置后台模式 操作如下: 2. AppDelegate.

iOS开发- 自定义遮罩视图(引导, 功能说明)源码+解析

iOS开发- 自定义遮罩视图(引导, 功能说明)源码+解析 我们平时使用App的时候, 经常在第一次使用的时候, 会有类似"新手教程"之类的东西, 来引导我们应该如何使用这个App. 但是这个"新手教程"不同于常规的引导页(引导页指第一次打开App时候, 弹出的那种介绍视图. 他是静态的, 不需要与用户交互, 可以直接一页页翻, 或者直接跳过.)所谓的"新手教程", 就是按照App的提示, 一步步跟着完成. 那这个"新手教程"

iOS开发自定义时间选取器

又是一年的暑假日期而至,小孩子放假,都会到在外打工的父母身边.孩子想父母,父母也思念着自己的孩子.我身边的亲戚朋友的孩子 也都来了.这个暑假又该热闹起来. 努力什么时候都不晚 我有一个表妹,今年参加完高考,对自己的成绩不是特别满意.上次我回老家,刚好她给我一起来上海.准备来上海锻炼一下. 车上我问她,准备去哪上学?她说不想上了,想打工.我没有怎么劝她,我想让她体验一下打工的生活,她就知道还是上学好. 一个高中生,出来找工作,困难可想而知.经姐姐介绍,进了一家餐厅工作.工资也不是很高.干了两天,我

iOS开发自定义键盘回车键Return Key

在iOS开发中,用户在进行文本输入的时候,往往会用到虚拟键盘上的回车键,也就是Return Key.回车键有时候可以是"完成"(表示输入结束),可以是"下一项"(连续信息输入),可以是"搜索"(网页中输入),可以是"发送"(邮件输入),可以是"加入"(比如连接Wi-Fi),等等.我们需要怎么设置呢?方法如下: (1)选中一个Text Field,点击右侧的"Show the Attributes

iOS开发-自定义AlterView(iOS 7)

App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作.iOS7之前是可以自定义AlterView的,就是继承一下UIAlterView,然后初始化的时候通过addSubview添加自定义的View,但是iOS7之后这样做就不行了,不过还好有开源项目可以解决这个问题. iOS默认弹框 viewDidLoad中添加两个按钮,代码如下: UIButton *orignalBtn=

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

IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlue和UITableViewCellSelectionStyleGray. 那么如何自定义这个颜色呢.一个思路是当用户点下cell时设置你想要的cell的背景色,当释放点击时给cell重新设回原来的背景色,这样就能达到预想的效果了. 下面是具体实现的代码: - (void)drawRect:(CGRect)rect { if (self.highlig

iOS 开发: 自定义相册, 保存多张图片到自定义相册中

1.自定义相册(兼容 iOS7 iOS8) - (void)viewDidLoad { //search all photo albums in the library    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)     {         //compare the names of the albums        

iOS 开发 自定义UIFont字体

之前的好几个项目,客户都要求使用微软雅黑字体,可是iOS没有自带这个字体,肿么办 只能自己自定义字体了,下面是自定义字体的几个重要步骤: 1.添加对应的字体(.ttf或.otf)到工程的resurce,例如my.ttf. 2.在info.plist中添加一项Fonts provided by application (item0对应的value为my.ttf,添加多个字体依次添加就可以了). 3.使用时 aLabel.font=[UIFont fontWithName:@"XXX" s