iOS开发——UI基础-提示框
提示框的种类有很多,废话不多说,直接上代码
一、文本提示框
运行结果如下:
代码实现如下:
@interface ViewController ()
// 添加方法
- (IBAction)add;
// 移除方法
- (IBAction)remove;
// 商品容器
@property (weak, nonatomic) IBOutlet UIView *shopsView; @property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UILabel *hudLabel; @property (nonatomic, strong)NSMutableArray *shops;
@end @implementation ViewController // 控制器的view创建完毕就会调用
// 该方法只会调用一次
- (void)viewDidLoad
{
[super viewDidLoad]; // 一开始就让提示框隐藏
self.hudLabel.alpha = 0.0;
} - (IBAction)add
{
/**********************计算X/Y**************************/
// 商品的宽高
CGFloat shopWidth = ;
CGFloat shopHeight = ; // 总行数和列数
int totalCol = ; // 计算间隙
CGFloat ColMargin = (self.shopsView.frame.size.width - (totalCol * shopWidth)) / (totalCol - );
CGFloat RowMargin = ColMargin; // 获取绿色控件的子控件
NSUInteger index = self.shopsView.subviews.count; // 1.计算行号
int row = index / ;
// 2.计算当前行的Y值
// Y = 行号 * (商品的高度 + 间隙)
CGFloat shopY = row * (shopHeight + RowMargin); // 3.计算列号
int col = index % ;
// 4.计算当前列的X值
CGFloat shopX = col * (shopWidth + ColMargin); /**********************创建商品**************************/ // 1.加载XIB
XMGShopView *shopView = [XMGShopView shopView]; // 2.设置frame
CGRect tempFrame = shopView.frame;
tempFrame.origin.x = shopX;
tempFrame.origin.y = shopY;
shopView.frame = tempFrame; // 3.设置数据
NJShop *shop = self.shops[index];
shopView.shop = shop; // 2.添加子控件
[self.shopsView addSubview:shopView]; /**********************添加商品**************************/ // 6.控制删除按钮是否可以点击
self.removeBtn.enabled = YES; // 7.控制添加按钮是否可以点击
self.addBtn.enabled = self.shopsView.subviews.count < self.shops.count; // 8.控制提示框是否显示
if (self.addBtn.enabled == NO) {
[self showHUDWithText:@"商品柜已经满了, 不要再买买买了..."];
}
} - (IBAction)remove
{ NSLog(@"%@", self.shops[]); // 1.取出父控件中最后一个控件
UIView *subView = self.shopsView.subviews.lastObject;
// 2.移除最后一个控件
[subView removeFromSuperview]; // 3.控制删除按钮是否可以点击
self.removeBtn.enabled = self.shopsView.subviews.count > ; // 4.控制添加按钮是否可以点击
self.addBtn.enabled = YES; // 5.控制器提示框是否显示
if (self.removeBtn.enabled == NO) { self.hudLabel.text = @"商品柜已经空了, 继续买买买...";
// 显示提示框
[UIView animateWithDuration:1.0 animations:^{
self.hudLabel.alpha = 1.0;
} completion:^(BOOL finished) { [UIView animateWithDuration:1.0 delay:2.0 options:kNilOptions animations:^{
self.hudLabel.alpha = 0.0;
} completion:nil];
}]; [self showHUDWithText:@"商品柜已经空了, 继续买买买..."];
}
} - (void)showHUDWithText:(NSString *)text
{
// 1.设置需要展示的内容
self.hudLabel.text =text; // 2.显示提示框
[UIView animateWithDuration:1.0 animations:^{
self.hudLabel.alpha = 1.0;
} completion:^(BOOL finished) {
// 3.隐藏提示框
[UIView animateWithDuration:1.0 delay:2.0 options:kNilOptions animations:^{
self.hudLabel.alpha = 0.0;
} completion:nil];
}];
} // 重写getter方法
- (NSMutableArray *)shops
{
if (_shops == nil) { // 1.获取plist文件的绝对路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
// 2.根据路径加载plist文件
NSArray *tempArr = [NSArray arrayWithContentsOfFile:path]; // 3.将数组中所有的字典转换为模型
_shops = [NSMutableArray array];
for (NSDictionary *dict in tempArr) {
NJShop *shop = [[NJShop alloc] init];
shop.name = dict[@"name"];
shop.icon = dict[@"icon"];
[_shops addObject:shop];
}
}
return _shops;
}
@end
二、系统自带的提示框(HUD)
1.UIAlertView
代码如下:
- (void)useAlert
{
// 1.UIAlerView
// 如果看到iOS提供的方法中有... 就代表是一个可变参数,可以传一个或者多个 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"哥是标题" message:@"姐是正文..." delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert show];
}
#pragma mark - UIAlertViewDelegate
// 只要UIAlertView上的按钮被点击, 就会调用
// alertView:谁触发事件就会把谁传递进来
// ButtonAtIndex: 当前被点击按钮的索引
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonInde
{
// 取消 = 0, 确定 = 1 , 其他 = 2 , Other = 3 以此类推
switch (buttonInde) {
case :
NSLog(@"点击了取消");
break;
case :
NSLog(@"点击了确定");
break;
case :
NSLog(@"点击了其他");
break;
case :
NSLog(@"点击了Other");
break;
default:
break;
}
}
2. UIActionSheet
- (void)useActionSheet
{
// 2.UIActionSheet
// 1.创建UIActionSheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"哥是标题" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其它", @"Other", nil];
// 2.显示UIActionSheet
[sheet showInView:self.view];
} #pragma mark - UIActionSheetDelegate
// 只要UIActionSheet上的按钮被点击就会调用
// actionSheet:谁触发事件就会把谁传递进来
// clickedButtonAtIndex:当前被点击按钮的索引
- (void)actionSheet:(nonnull UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld", buttonIndex);
}
注意以上两中方法都要设置代理,那么分别要遵守
<UIAlertViewDelegate, UIActionSheetDelegate>协议
3. UIAlertController
- (void)useAlertControllerAlert
{
// 1.创建alert控制器
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"哥是标题" message:@"姐是正文..." preferredStyle:UIAlertControllerStyleAlert]; // 2.添加一个按钮
// Title : 按钮上显示的文字
// style: 按钮的样式
// handler: 点击按钮之后的回调
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {
NSLog(@"点击了确定按钮");
}]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {
NSLog(@"点击了取消按钮");
}]; [alertVc addAction:action1];
[alertVc addAction:action2]; // 3.添加三个输入框
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
textField.text = @"用户名";
NSLog(@"textField");
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
textField.secureTextEntry = YES;
NSLog(@"textField");
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"textField");
}]; // 4.presentViewController弹出一个控制器
[self presentViewController:alertVc animated:YES completion:nil]; }
三、自定义提示框
实现代码如下:
- (void)useCustomHUD
{
// 1.创建父控件
UIView *cover = [[UIView alloc] init];
cover.backgroundColor = [UIColor colorWithRed: green: blue: alpha:0.5];
cover.frame = CGRectMake(, , , );
cover.center = self.view.center; // 修改父控件为圆角
cover.layer.cornerRadius = ;
[self.view addSubview:cover]; // 2.创建菊花
// 菊花有默认的尺寸
// 注意: 虽然有默认的尺寸, 但是要想显示必须让菊花转起来
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activity.center = CGPointMake(cover.frame.size.width * 0.5, );
[activity startAnimating]; [cover addSubview:activity];
// 3.创建UILabel
UILabel *label = [[UILabel alloc] init];
// label.backgroundColor = [UIColor purpleColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"正在拼命加载中...";
label.frame = CGRectMake(, cover.frame.size.height - , cover.frame.size.width, );
[cover addSubview:label]; }
除了以上列出的几种方法外,还可以使用第三方框架,比如MBP
iOS开发——UI基础-提示框的更多相关文章
- iOS开发UI基础—手写控件,frame,center和bounds属性
iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...
- IOS开发UI基础--数据刷新
IOS开发UI基础--数据刷新 cell的数据刷新包括下面几个方面 加入数据 删除数据 更改数据 全局刷新方法(最经常使用) [self.tableView reloadData]; // 屏幕上的全 ...
- iOS开发——UI篇&提示效果
提示效果 关于iOS开发提示效果是一个很常见的技术,比如我们平时点击一个按钮,实现回馈,或者发送网络请求的时候! 技术点: 一:View UIAlertView UIActionSheet 二:控制器 ...
- iOS开发-UI基础Demo
现在更多的学习资料都是xCode4.X的,发现xCode6.1还是很多东西,如果有正在学习iOS开发的可以通过Demo简单了解下iOS的UI开发~ 1.新建单视图文件: 2.新建项目名称,语言选择OC ...
- iOS开发——UI基础-控制器,IBAction和IBOutlet,UIView
第一个ios程序 @interface ViewController : UIViewController @property(nonatomic, weak)IBOutlet UILabel *la ...
- iOS开发——UI基础-屏幕适配
一.适配 1.什么是适配?适应.兼容各种不同的情况 2.移动开发中,适配的常见种类 2.1系统适配 针对不同版本的操作系统进行适配 2.2屏幕适配 针对不同大小的屏幕尺寸进行适配 二.点和像素 1.在 ...
- IOS开发UI基础UITextFidle相关属性
UITextFidle相关属性 • enablesReturnKeyAutomatically默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的. ...
- IOS开发UI基础之Plis文件-字典转模型
什么是plist文件? 在开发中直接将数据写在代码里面 不是一种合理的做法 如果数据经常改变 就需要经常翻开对应的代码进行修改 造成代码扩展性低 因此,可以考虑将经常变的数据放在⽂文件中进⾏行存储,程 ...
- IOS开发UI基础UIView
主要介绍下UIView得基本概念和一些属性的介绍至于属性的用户后面会由详细的介绍 -.UIView基本概念 1.什么是控件? 屏幕上所有的UI元素都叫做控件 (也有很多书中叫做视图 组件) 比如 按钮 ...
随机推荐
- markdown安装和使用
下载 运行markdownpad2-setup.exe文件,下一步一直到结束. 使用 标题 列表 引用.网页链接.图片链接 代码框 星号
- BZOJ2286: [Sdoi2011]消耗战
建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...
- SSL/TLS协议工作流程
我看了CloudFlare的说明(这里和这里),突然意识到这是绝好的例子,可以用来说明SSL/TLS协议的运行机制.它配有插图,很容易看懂. 下面,我就用这些图片作为例子,配合我半年前写的<SS ...
- python学习笔记-(十)面向对象基础
面向对象相关知识简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...
- oracle自定义判断数据是否为数值函数
CREATE OR REPLACE FUNCTION isnumeric (str IN VARCHAR2) RETURN NUMBER IS v_str ); BEGIN IF str IS NUL ...
- HTML学习笔记——锚链接、pre标签、实体
1>锚链接 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- notification的使用
示例: NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); No ...
- 第4章 jQuery的事件和动画(1)——事件篇
jQuery扩展了JavaScript的基本事件处理机制,极大增强了事件处理能力 一. jQuery的事件 1. $(document).ready(function(){})加载方式 再次回到win ...
- GMU 简单使用一
<!doctype html> <html> <head> <title>iOS7风格的进度条</title> <meta chars ...
- jexus jws 安装
cd /tmp wget linuxdot.net/down/jexus--x64.tar.gz tar -zxvf jexus--x64.tar.gz mv jexus /usr rm -rf /t ...