iOS自定义AlertView
来源:互联网 发布:h5小游戏网站源码 编辑:程序博客网 时间:2024/05/22 10:16
虽然系统的AlertView(AlertController)已经非常简单易用了,但偶尔还是想做些更酷炫的交互或者需要添加很多自定义的内容,这时候需要自定义个View来实现个性化的AlertView。
先来看看成品:
代码非常少,但效果十分出众。
首先我用xib创建了需要自定义的提示视图,建立好约束
建立对应的类我这里就叫MyAlertView
之后outlet中心需要动画的view,这里叫做centerView
@property (weak, nonatomic) IBOutlet UIView *centerView;
在自定义类MyAlertView中充写awakefromnib方法实现动画,并实现退出按钮的反向动画
- (void)awakeFromNib{ self.centerView.transform = CGAffineTransformMakeScale(0, 0); self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.centerView.transform = CGAffineTransformMakeScale(1, 1); self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; } completion:^(BOOL finished) { }]; }- (IBAction)quit:(id)sender { [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; self.centerView.transform = CGAffineTransformMakeScale(0.0001, 0.0001); } completion:^(BOOL finished) { [self removeFromSuperview]; }];}
最后在主控制器中创建自定义的“AlertView”,并展现出来
- (IBAction)showAlert:(id)sender{ MyAlertView *alert = [[NSBundle mainBundle] loadNibNamed:@"MyAlertView" owner:nil options:nil][0]; alert.frame = self.view.bounds; [self.view addSubview:alert];}
十分简单,但有一点需要注意最后移除view的动画设置缩放参数的时候不可以设置为0,否则动画不会显示,这里用了级小值0.0001来近似0的效果。
self.centerView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
最后,用自定义view来实现虽然可以奏效,但反观苹果在iOS8中用AlertContrller来代替AlertView,这种模态视图用控制器来管理结构才是比较清晰的,所以这里可以用自定义控制器来实现同样功能,模态弹出的时候presentation的方式需要设置为Over Current Contxt就可以了。
1 0
- ios 自定义alertview
- iOS自定义AlertView
- ios自定义AlertView
- iOS开发之自定义AlertView
- iOS自定义alertView提示框
- 【代码笔记】iOS-自定义alertView
- IOS: 自定义AlertView实现模态对话框
- [ios] alertview自定义提示信息【转】
- [IOS] 自定义AlertView实现模态对话框
- IOS: 自定义AlertView实现模态对话框
- iOS自定义alertView,继承自UIView
- iOS-自定义AlertView(便利构造器)
- ios 开发学习日志 自定义alertView 1
- ios 开发学习日志 自定义alertView 2
- ios 开发学习日志 自定义alertView 4
- ios 开发学习日志 自定义alertView 3
- [IOS] 自定义AlertView实现模态对话框
- iOS-YHAlertView(自定义AlertView,Swift版)
- 《精通Hadoop》:第 1 章 Hadoop 2.X
- Android项目文件夹结构以及意义
- sublime text注册码
- 【html/css】选择器以及其优先级的问题
- 散列表的学习和探讨(算法导论第11章)
- iOS自定义AlertView
- vs2010调试时无法查找或打开 PDB 文件
- ZOJ-3712-Hard to Play【10th浙江省赛】
- 面向对象编程其实很简单——Python 面向对象(初级篇)
- 考研开始
- 实现javaScript对象的"继承"的两种方法(prototype与闭包)
- eclipse如何下载插件
- 《Docker——容器与容器云》:第一章 从容器到容器云
- JavaScript变量引用问题