IOS开发学习笔记023-UIToolBar的使用
这里使用代码实现
大概过程:
1、创建工具条
2、创建插入条
3、添加头像、标签、删除按钮
4、点击头像获取标签信息
做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添加
1、如何在UIToolBar两头出现两个按钮bar button item
可是在按钮中间添加一个bar button item,然后设置按钮的属性Identifier为Flexible Space
2、然后拖拽添加事件
- - (IBAction)add:(UIBarButtonItem *) sender; // 添加
- - (IBAction)remove:(UIBarButtonItem *) sender; // 删除
3、实现消息响应
点击添加按钮后向视窗中添加一条联系人信息,每次都添加到最后边。
点击删除按钮从视窗中删除一条联系人信息,每次都先删除最后一条。当只剩下工具条时使删除按钮失效。
添加按钮
- - (IBAction)add:(UIBarButtonItem *) sender // 添加
- {
- NSLog(@"%@",@"add");
- // 0.获取最后一个控件
- UIView *last = [self.view.subviews lastObject];
- //计算当前要插入的控件的位置
- // y = 最后一个控件的y + 最后一个控件的高度 + 间距
- CGFloat rowY = last.frame.origin.y + last.frame.size.height + ;
- // 1.创建
- UIView *view = [[UIView alloc] init];
- //view.frame = CGRectMake(0,rowY,320,50);
- view.backgroundColor = [UIColor redColor];
- // 2.添加
- [self.view addSubview:view];
- // 3.删除按钮有效
- _removeItem.enabled = YES;
- view.frame = CGRectMake(, rowY, , );
- view.alpha = ;
- // 4.执行动画
- // 动画实现方式1
- // [UIView beginAnimations:nil context:nil];
- // [UIView setAnimationDuration:1.0];
- // 动画实现方式2
- //使用UIView的方法实现动画block
- // [UIView animateWithDuration:1.0 animations:^{
- //
- // }];
- // 使用UIView的方法实现动画block
- [UIView animateWithDuration:kDur animations:^{
- view.frame = CGRectMake(, rowY, , );
- view.alpha = ;
- // 在动画结束后添加其他操作
- } completion:^(BOOL finished) {
- NSLog(@"%@",@"add完毕");
- }];
- // [UIView commitAnimations];
- }
其中的动画实现方式有三种:
1、动画实现方式1
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
// 出现动画
[UIView commitAnimations];
2、[UIView animateWithDuration:1.0 animations:^{
// 动画改变
}];
3、 [UIView animateWithDuration:kDur animations:^{
view.frame = CGRectMake(0, rowY, 320, 50);
view.alpha = 1;
}//这里在动画结束实现一些其他操作
completion:^(BOOL finished) {
NSLog(@"%@",@"add完毕");
}];
删除按钮
- #pragma mark 删除一行
- - (IBAction)remove:(UIBarButtonItem *) sender // 删除
- {
- NSLog(@"%@",@"remove");
- // 1.取出最后一个子控件
- UIView *last = [self.view.subviews lastObject];
- // 判断是否是toolbar
- //if([last isKindOfClass:[last class]]) return;//
- [UIView animateWithDuration:kDur animations:^{
- CGRect temp = last.frame;
- temp.origin.x = ;
- last.frame = temp; // 改变位置
- last.alpha = ; // 改变透明度
- } completion:^(BOOL finished) {
- // 2.删除子控件
- [last removeFromSuperview];
- _removeItem.enabled = self.view.subviews.count != ;
- NSLog(@"%@",@"remove完毕");
- }];
- // 3.判断剩下的子控件的个数
- // if(self.view.subviews.count == 1)
- // {
- // _removeItem.enabled = NO;
- // }
- }
4、给添加的子控件添加头像和标签
把add方法中得创建子控件分离出来到一个方法中
- UIView *view = [self createRowView]; // 在原处替换为这句话
- createRowView方法,常见一行信息包括头像和文字信息
- // 创建一行
- - (UIView *)createRowView
- {
- // 1 创建父控件
- UIView *view = [[UIView alloc] init];
- //view.frame = CGRectMake(0,rowY,320,50);
- view.backgroundColor = [UIColor redColor];
- // 2 添加标签
- UILabel *lab = [[UILabel alloc] init];
- lab.frame = CGRectMake(, , self.view.frame.size.width, );
- // lab.center = CGPointMake(160, kRowH/2);
- // 随机获取已经存在的几个名字
- int nameIndex = arc4random_uniform([_allNames count]);
- // 设置文字
- lab.text = _allNames[nameIndex];
- // 设置背景
- lab.backgroundColor = [UIColor clearColor];
- // 设置文本居中显示
- lab.textAlignment = NSTextAlignmentCenter; // 枚举常量
- // 设置tag属性
- [lab setTag:kTag];
- // 添加到父控件
- [view addSubview:lab];
- // 3 添加头像
- UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
- icon.frame = CGRectMake(, , , kRowH);
- // 随机产生文件名 ,0~8
- int randomindex = arc4random() % ;
- //int randomindex2 = arc4random_uniform(9);
- NSString *name = [NSString stringWithFormat:@"01%d.png",randomindex];
- // 设置图片
- [icon setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
- // 添加监听器,响应按钮按下事件
- [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
- // 添加到父控件
- [view addSubview:icon];
- return view;
- }
- 1 // 监听器
- - (void)iconClick:(UIButton *)btn
- {
- //NSLog(@"%@",btn);
- // 根据tag获取父控件内部子控件tag为10的子控件
- UILabel *lab = (UILabel *)[btn.superview viewWithTag:];
- NSLog(@"%@",lab.text);
- }
添加的数组在这里
- // 类扩展(class extension 匿名分类)
- // 添加类的私有属性和方法
- @interface SLQViewController ()
- {
- NSArray *_allNames;
- }
- @end
- @implementation SLQViewController
- #pragma mark 控制器的view调用完毕调用一次
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- _allNames = @[@"张三",@"李四",@"万物",@"傻缺"];
- }
现在效果如下,可添加一行,删除一行,并随机显示头像和标签
5、给每一条信息都添加删除按钮
在创建时直接添加上按钮,写在添加过头像之后
- // 4.删除按钮
- UIButton *del = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- del.frame = CGRectMake(, , , );
- del.backgroundColor = [UIColor yellowColor];
- [del setTitle:@"删除" forState:UIControlStateNormal];
- // 添加监听事件
- [del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
- [view addSubview:del];
然后响应按下事件
- // 监听删除按钮
- - (void)deleteClick:(UIButton *)del
- {
- // NSLog(@"%f",del.superview.frame.origin.y);
- // 添加动画效果
- [UIView animateWithDuration:kDur animations:^{
- // 改变位置
- CGRect temp = del.superview.frame;
- temp.origin.x = kWidth;
- del.superview.frame = temp;
- del.superview.alpha = ; // 透明
- }
- // 动画执行结束后执行这个
- completion:^(BOOL finished)
- {
- // 1 获取当前要删除控件的索引
- int index = [self.view.subviews indexOfObject:del.superview];
- // 2 删除当前行
- [del.superview removeFromSuperview];
- [UIView animateWithDuration:kDur-0.3 animations:^{
- // 3 删除后后面控件上移
- for (int i = index; i < self.view.subviews.count; i ++)
- {
- //
- UIView *child = self.view.subviews[i];
- CGRect temp = child.frame;
- temp.origin.y -= kRowH + ;
- child.frame = temp;
- }
- }];
- // 4 判断删除按钮是否失效
- _removeItem.enabled = self.view.subviews.count > ;
- }];
- }
代码
- //
- // SLQViewController.m
- // 联系人管理
- //
- // Created by Christian on 15/5/13.
- // Copyright (c) 2015年 itcast. All rights reserved.
- //
- #import "SLQViewController.h"
- #define kDur 1.0
- #define kRowH 50
- #define kTag 10
- #define kWidth 320
- // 类扩展(class extension 匿名分类)
- // 添加类的私有属性和方法
- @interface SLQViewController ()
- {
- NSArray *_allNames;
- }
- @end
- @implementation SLQViewController
- #pragma mark 控制器的view调用完毕调用一次
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- _allNames = @[@"张三",@"李四",@"万物",@"傻缺"];
- }
- #pragma mark 添加一行
- - (IBAction)add:(UIBarButtonItem *) sender // 添加
- {
- NSLog(@"%@",@"add");
- // 0.获取最后一个控件
- UIView *last = [self.view.subviews lastObject];
- //计算当前要插入的控件的位置
- // y = 最后一个控件的y + 最后一个控件的高度 + 间距
- CGFloat rowY = last.frame.origin.y + last.frame.size.height + ;
- // 1.创建
- // UIView *view = [[UIView alloc] init];
- // //view.frame = CGRectMake(0,rowY,320,50);
- // view.backgroundColor = [UIColor redColor];
- UIView *view = [self createRowView];
- // 2.添加
- [self.view addSubview:view];
- // 3.删除按钮有效
- _removeItem.enabled = YES;
- view.frame = CGRectMake(, rowY, , );
- view.alpha = ;
- // 4.执行动画
- // 动画实现方式1
- // [UIView beginAnimations:nil context:nil];
- // [UIView setAnimationDuration:1.0];
- // 动画实现方式2
- //使用UIView的方法实现动画block
- // [UIView animateWithDuration:1.0 animations:^{
- //
- // }];
- // 使用UIView的方法实现动画block
- [UIView animateWithDuration:kDur animations:^{
- view.frame = CGRectMake(, rowY, , );
- view.alpha = ;
- } completion:^(BOOL finished) {
- NSLog(@"%@",@"add完毕");
- }];
- // [UIView commitAnimations];
- }
- // 创建一行
- - (UIView *)createRowView
- {
- // 1 创建父控件
- UIView *view = [[UIView alloc] init];
- //view.frame = CGRectMake(0,rowY,320,50);
- view.backgroundColor = [UIColor redColor];
- // 2 添加标签
- UILabel *lab = [[UILabel alloc] init];
- lab.frame = CGRectMake(, , self.view.frame.size.width, );
- // lab.center = CGPointMake(160, kRowH/2);
- // 随机获取已经存在的几个名字
- int nameIndex = arc4random_uniform([_allNames count]);
- // 设置文字
- lab.text = _allNames[nameIndex];
- // 设置背景
- lab.backgroundColor = [UIColor clearColor];
- // 设置文本居中显示
- lab.textAlignment = NSTextAlignmentCenter; // 枚举常量
- // 设置tag属性
- [lab setTag:kTag];
- // 添加到父控件
- [view addSubview:lab];
- // 3 添加头像
- UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
- icon.frame = CGRectMake(, , , kRowH);
- // 随机产生文件名 ,0~8
- int randomindex = arc4random() % ;
- //int randomindex2 = arc4random_uniform(9);
- NSString *name = [NSString stringWithFormat:@"01%d.png",randomindex];
- // 设置图片
- [icon setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
- // 添加监听器
- [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
- // 添加到父控件
- [view addSubview:icon];
- // 4.删除按钮
- UIButton *del = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- del.frame = CGRectMake(, , , );
- del.backgroundColor = [UIColor yellowColor];
- [del setTitle:@"删除" forState:UIControlStateNormal];
- // 添加监听事件
- [del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
- [view addSubview:del];
- return view;
- }
- // 监听删除按钮
- - (void)deleteClick:(UIButton *)del
- {
- // NSLog(@"%f",del.superview.frame.origin.y);
- // 添加动画效果
- [UIView animateWithDuration:kDur animations:^{
- // 改变位置
- CGRect temp = del.superview.frame;
- temp.origin.x = kWidth;
- del.superview.frame = temp;
- del.superview.alpha = ; // 透明
- }
- // 动画执行结束后执行这个
- completion:^(BOOL finished)
- {
- // 1 获取当前要删除控件的索引
- int index = [self.view.subviews indexOfObject:del.superview];
- // 2 删除当前行
- [del.superview removeFromSuperview];
- [UIView animateWithDuration:kDur-0.4 animations:^{
- // 3 删除后后面控件上移
- for (int i = index; i < self.view.subviews.count; i ++)
- {
- //
- UIView *child = self.view.subviews[i];
- CGRect temp = child.frame;
- temp.origin.y -= kRowH + ;
- child.frame = temp;
- }
- }];
- // 4 判断删除按钮是否失效
- _removeItem.enabled = self.view.subviews.count > ;
- }];
- }
- // 监听头像按钮
- - (void)iconClick:(UIButton *)btn
- {
- //NSLog(@"%@",btn);
- // 根据tag获取父控件内部子控件tag为10的子控件
- UILabel *lab = (UILabel *)[btn.superview viewWithTag:];
- NSLog(@"%@",lab.text);
- }
- #pragma mark 删除一行
- - (IBAction)remove:(UIBarButtonItem *) sender // 删除
- {
- NSLog(@"%@",@"remove");
- // 1.取出最后一个子控件
- UIView *last = [self.view.subviews lastObject];
- // 判断是否是toolbar
- //if([last isKindOfClass:[last class]]) return;//
- [UIView animateWithDuration:kDur animations:^{
- CGRect temp = last.frame;
- temp.origin.x = ;
- last.frame = temp; // 改变位置
- last.alpha = ; // 改变透明度
- } completion:^(BOOL finished) {
- // 2.删除子控件
- [last removeFromSuperview];
- _removeItem.enabled = self.view.subviews.count != ;
- NSLog(@"%@",@"remove完毕");
- }];
- // 3.判断剩下的子控件的个数
- // if(self.view.subviews.count == 1)
- // {
- // _removeItem.enabled = NO;
- // }
- }
- @end
IOS开发学习笔记023-UIToolBar的使用的更多相关文章
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS开发学习笔记
1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- (ios开发学习笔记一)ios项目文件结构
转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...
- ios开发学习笔记040-autolayout 第三方框架Masonry
不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...
- IOS开发学习笔记033-UIScrollView
1.滚动显示图片 如果图片过大,则需要滚动显示,这是需要用到类UIScrollView,可是实现控件的水平和垂直滚动. 可用三步实现:1 设置UIScrollView,2 设置UIImageView, ...
- IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...
随机推荐
- Android 5.0以上获取系统运行进程信息
在Android 5.0以上系统,调用getRunningAppProcesses 方法返回的列表为空,这是因为谷歌考虑到安全原因,已经把这个方法移除掉了, 那以后要获取系统运行的后台进程这个方法用不 ...
- uLua学习之创建游戏对象(二)
前言 上节,刚刚说到创建一个“HelloWorld”程序,大家想必都对uLua有所了解了,现在我们一步步地深入学习.在有关uLua的介绍中(在这里),我们可以发现它使用的框架是Lua + LuaJIT ...
- Android商城开发系列(十三)—— 首页热卖商品布局实现
热卖商品布局效果如下图: 这个布局跟我们上节做的推荐是一样的,也是用LinearLayout和GridView去实现的,新建一个hot_item.xml,代码如下所示: <?xml versio ...
- gitlab用户添加ssh免密钥认证后clone还是要求输入密码
今天在centos 7公网服务器上安装gitlab在配置ssh免密钥时遇到一个奇怪的事,正确添加了本机的公钥到gitlab账户上,进行clone时死活都要你输入密码gitlab使用yum安装的,之前在 ...
- linux 命令——33 df(转)
linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...
- POJ - 3111 K Best(二分)
包含一些ai和bi的集用S来表示,x = max(sigma(ai)/sigma(bi),i 属于S) ,k 表示S的大小,k= |S|. x和k之间具有单调性.k0 < k1 → x0 ≥ x ...
- Activiti学习记录(五)
1.排他网关 说明: 1) 一个排他网关对应一个以上的顺序流 2) 由排他网关流出的顺序流都有个conditionExpression元素,在内部维护返回boolean类型的决策结果. 3) 决策网关 ...
- 表格和网页ico图标
表格: 表格格式: <table> <tr> 表格的行 <th >表头</th> <th>表头 </th> </tr> ...
- 转+更新 Graphviz 教程,例子+ 高级应用 写代码,编程绘制架构图(分层拓扑图) 转自官网
1. Graphviz介绍 Graphviz是大名鼎鼎的贝尔实验室的几位牛人开发的一个画图工具. 它的理念和一般的“所见即所得”的画图工具不一样,是“所想即所得”. Graphviz提供了dot语言来 ...
- 架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级tu)
最近要画架构图. 一方面有图片洁癖,另外一方面又不想不停的挪动图片. 一开始想用脑图软件. 发现脑图是树状的,架构模块依赖图是网状的.(也可以简化为层级图,不画交互关系.类似 dubbo 的架构图. ...