这里使用代码实现

大概过程:

  1、创建工具条

  2、创建插入条

  3、添加头像、标签、删除按钮

  4、点击头像获取标签信息

做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添加

1、如何在UIToolBar两头出现两个按钮bar button item

可是在按钮中间添加一个bar button item,然后设置按钮的属性Identifier为Flexible Space

2、然后拖拽添加事件

  1. - (IBAction)add:(UIBarButtonItem *) sender; // 添加
  2. - (IBAction)remove:(UIBarButtonItem *) sender; // 删除

3、实现消息响应

  点击添加按钮后向视窗中添加一条联系人信息,每次都添加到最后边。

  点击删除按钮从视窗中删除一条联系人信息,每次都先删除最后一条。当只剩下工具条时使删除按钮失效。

  添加按钮

  1. - (IBAction)add:(UIBarButtonItem *) sender // 添加
  2. {
  3. NSLog(@"%@",@"add");
  4. // 0.获取最后一个控件
  5. UIView *last = [self.view.subviews lastObject];
  6. //计算当前要插入的控件的位置
  7. // y = 最后一个控件的y + 最后一个控件的高度 + 间距
  8. CGFloat rowY = last.frame.origin.y + last.frame.size.height + ;
  9.  
  10. // 1.创建
  11. UIView *view = [[UIView alloc] init];
  12. //view.frame = CGRectMake(0,rowY,320,50);
  13. view.backgroundColor = [UIColor redColor];
  14.  
  15. // 2.添加
  16. [self.view addSubview:view];
  17.  
  18. // 3.删除按钮有效
  19. _removeItem.enabled = YES;
  20.  
  21. view.frame = CGRectMake(, rowY, , );
  22. view.alpha = ;
  23. // 4.执行动画
  24. // 动画实现方式1
  25. // [UIView beginAnimations:nil context:nil];
  26. // [UIView setAnimationDuration:1.0];
  27.  
  28. // 动画实现方式2
  29. //使用UIView的方法实现动画block
  30. // [UIView animateWithDuration:1.0 animations:^{
  31. //
  32. // }];
  33. // 使用UIView的方法实现动画block
  34. [UIView animateWithDuration:kDur animations:^{
  35. view.frame = CGRectMake(, rowY, , );
  36. view.alpha = ;
  37. // 在动画结束后添加其他操作
  38. } completion:^(BOOL finished) {
  39. NSLog(@"%@",@"add完毕");
  40. }];
  41.  
  42. // [UIView commitAnimations];
  43. }

其中的动画实现方式有三种:

  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完毕");

       }];  

  删除按钮

  1. #pragma mark 删除一行
  2. - (IBAction)remove:(UIBarButtonItem *) sender // 删除
  3. {
  4. NSLog(@"%@",@"remove");
  5.  
  6. // 1.取出最后一个子控件
  7. UIView *last = [self.view.subviews lastObject];
  8. // 判断是否是toolbar
  9. //if([last isKindOfClass:[last class]]) return;//
  10. [UIView animateWithDuration:kDur animations:^{
  11. CGRect temp = last.frame;
  12. temp.origin.x = ;
  13. last.frame = temp; // 改变位置
  14. last.alpha = ; // 改变透明度
  15. } completion:^(BOOL finished) {
  16. // 2.删除子控件
  17. [last removeFromSuperview];
  18. _removeItem.enabled = self.view.subviews.count != ;
  19. NSLog(@"%@",@"remove完毕");
  20. }];
  21.  
  22. // 3.判断剩下的子控件的个数
  23. // if(self.view.subviews.count == 1)
  24. // {
  25. // _removeItem.enabled = NO;
  26. // }
  27.  
  28. }

4、给添加的子控件添加头像和标签

把add方法中得创建子控件分离出来到一个方法中

  1. UIView *view = [self createRowView]; // 在原处替换为这句话
  1. createRowView方法,常见一行信息包括头像和文字信息
  1. // 创建一行
  2. - (UIView *)createRowView
  3. {
  4. // 1 创建父控件
  5. UIView *view = [[UIView alloc] init];
  6. //view.frame = CGRectMake(0,rowY,320,50);
  7. view.backgroundColor = [UIColor redColor];
  8.  
  9. // 2 添加标签
  10. UILabel *lab = [[UILabel alloc] init];
  11. lab.frame = CGRectMake(, , self.view.frame.size.width, );
  12. // lab.center = CGPointMake(160, kRowH/2);
  13. // 随机获取已经存在的几个名字
  14. int nameIndex = arc4random_uniform([_allNames count]);
  15. // 设置文字
  16. lab.text = _allNames[nameIndex];
  17. // 设置背景
  18. lab.backgroundColor = [UIColor clearColor];
  19. // 设置文本居中显示
  20. lab.textAlignment = NSTextAlignmentCenter; // 枚举常量
  21. // 设置tag属性
  22. [lab setTag:kTag];
  23. // 添加到父控件
  24. [view addSubview:lab];
  25.  
  26. // 3 添加头像
  27. UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
  28. icon.frame = CGRectMake(, , , kRowH);
  29. // 随机产生文件名 ,0~8
  30. int randomindex = arc4random() % ;
  31. //int randomindex2 = arc4random_uniform(9);
  32. NSString *name = [NSString stringWithFormat:@"01%d.png",randomindex];
  33. // 设置图片
  34. [icon setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
  35.  
  36. // 添加监听器,响应按钮按下事件
  37. [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
  38. // 添加到父控件
  39. [view addSubview:icon];
  40.  
  41. return view;
  42. }
  1.  
  2. 1 // 监听器
  3. - (void)iconClick:(UIButton *)btn
  4. {
  5. //NSLog(@"%@",btn);
  6. // 根据tag获取父控件内部子控件tag为10的子控件
  7. UILabel *lab = (UILabel *)[btn.superview viewWithTag:];
  8.  
  9. NSLog(@"%@",lab.text);
  10. }

添加的数组在这里

  1. // 类扩展(class extension 匿名分类)
  2. // 添加类的私有属性和方法
  3. @interface SLQViewController ()
  4. {
  5. NSArray *_allNames;
  6. }
  7. @end
  8.  
  9. @implementation SLQViewController
  10.  
  11. #pragma mark 控制器的view调用完毕调用一次
  12. - (void)viewDidLoad
  13. {
  14. [super viewDidLoad];
  15.  
  16. _allNames = @[@"张三",@"李四",@"万物",@"傻缺"];
  17. }

现在效果如下,可添加一行,删除一行,并随机显示头像和标签

5、给每一条信息都添加删除按钮

在创建时直接添加上按钮,写在添加过头像之后

  1. // 4.删除按钮
  2. UIButton *del = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  3. del.frame = CGRectMake(, , , );
  4. del.backgroundColor = [UIColor yellowColor];
  5. [del setTitle:@"删除" forState:UIControlStateNormal];
  6. // 添加监听事件
  7. [del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
  8.  
  9. [view addSubview:del];

然后响应按下事件

  1. // 监听删除按钮
  2. - (void)deleteClick:(UIButton *)del
  3. {
  4. // NSLog(@"%f",del.superview.frame.origin.y);
  5. // 添加动画效果
  6. [UIView animateWithDuration:kDur animations:^{
  7. // 改变位置
  8. CGRect temp = del.superview.frame;
  9. temp.origin.x = kWidth;
  10. del.superview.frame = temp;
  11.  
  12. del.superview.alpha = ; // 透明
  13. }
  14. // 动画执行结束后执行这个
  15. completion:^(BOOL finished)
  16. {
  17. // 1 获取当前要删除控件的索引
  18. int index = [self.view.subviews indexOfObject:del.superview];
  19. // 2 删除当前行
  20. [del.superview removeFromSuperview];
  21.  
  22. [UIView animateWithDuration:kDur-0.3 animations:^{
  23. // 3 删除后后面控件上移
  24. for (int i = index; i < self.view.subviews.count; i ++)
  25. {
  26. //
  27. UIView *child = self.view.subviews[i];
  28. CGRect temp = child.frame;
  29. temp.origin.y -= kRowH + ;
  30. child.frame = temp;
  31. }
  32. }];
  33.  
  34. // 4 判断删除按钮是否失效
  35. _removeItem.enabled = self.view.subviews.count > ;
  36.  
  37. }];
  38.  
  39. }

代码

  1. //
  2. // SLQViewController.m
  3. // 联系人管理
  4. //
  5. // Created by Christian on 15/5/13.
  6. // Copyright (c) 2015年 itcast. All rights reserved.
  7. //
  8.  
  9. #import "SLQViewController.h"
  10.  
  11. #define kDur 1.0
  12. #define kRowH 50
  13. #define kTag 10
  14. #define kWidth 320
  15. // 类扩展(class extension 匿名分类)
  16. // 添加类的私有属性和方法
  17. @interface SLQViewController ()
  18. {
  19. NSArray *_allNames;
  20. }
  21. @end
  22.  
  23. @implementation SLQViewController
  24.  
  25. #pragma mark 控制器的view调用完毕调用一次
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29.  
  30. _allNames = @[@"张三",@"李四",@"万物",@"傻缺"];
  31. }
  32. #pragma mark 添加一行
  33. - (IBAction)add:(UIBarButtonItem *) sender // 添加
  34. {
  35. NSLog(@"%@",@"add");
  36. // 0.获取最后一个控件
  37. UIView *last = [self.view.subviews lastObject];
  38. //计算当前要插入的控件的位置
  39. // y = 最后一个控件的y + 最后一个控件的高度 + 间距
  40. CGFloat rowY = last.frame.origin.y + last.frame.size.height + ;
  41.  
  42. // 1.创建
  43. // UIView *view = [[UIView alloc] init];
  44. // //view.frame = CGRectMake(0,rowY,320,50);
  45. // view.backgroundColor = [UIColor redColor];
  46. UIView *view = [self createRowView];
  47. // 2.添加
  48. [self.view addSubview:view];
  49.  
  50. // 3.删除按钮有效
  51. _removeItem.enabled = YES;
  52.  
  53. view.frame = CGRectMake(, rowY, , );
  54. view.alpha = ;
  55. // 4.执行动画
  56. // 动画实现方式1
  57. // [UIView beginAnimations:nil context:nil];
  58. // [UIView setAnimationDuration:1.0];
  59.  
  60. // 动画实现方式2
  61. //使用UIView的方法实现动画block
  62. // [UIView animateWithDuration:1.0 animations:^{
  63. //
  64. // }];
  65. // 使用UIView的方法实现动画block
  66. [UIView animateWithDuration:kDur animations:^{
  67. view.frame = CGRectMake(, rowY, , );
  68. view.alpha = ;
  69.  
  70. } completion:^(BOOL finished) {
  71. NSLog(@"%@",@"add完毕");
  72. }];
  73.  
  74. // [UIView commitAnimations];
  75. }
  76.  
  77. // 创建一行
  78. - (UIView *)createRowView
  79. {
  80. // 1 创建父控件
  81. UIView *view = [[UIView alloc] init];
  82. //view.frame = CGRectMake(0,rowY,320,50);
  83. view.backgroundColor = [UIColor redColor];
  84.  
  85. // 2 添加标签
  86. UILabel *lab = [[UILabel alloc] init];
  87. lab.frame = CGRectMake(, , self.view.frame.size.width, );
  88. // lab.center = CGPointMake(160, kRowH/2);
  89. // 随机获取已经存在的几个名字
  90. int nameIndex = arc4random_uniform([_allNames count]);
  91. // 设置文字
  92. lab.text = _allNames[nameIndex];
  93. // 设置背景
  94. lab.backgroundColor = [UIColor clearColor];
  95. // 设置文本居中显示
  96. lab.textAlignment = NSTextAlignmentCenter; // 枚举常量
  97. // 设置tag属性
  98. [lab setTag:kTag];
  99. // 添加到父控件
  100. [view addSubview:lab];
  101.  
  102. // 3 添加头像
  103. UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
  104. icon.frame = CGRectMake(, , , kRowH);
  105. // 随机产生文件名 ,0~8
  106. int randomindex = arc4random() % ;
  107. //int randomindex2 = arc4random_uniform(9);
  108. NSString *name = [NSString stringWithFormat:@"01%d.png",randomindex];
  109. // 设置图片
  110. [icon setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
  111.  
  112. // 添加监听器
  113. [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
  114. // 添加到父控件
  115. [view addSubview:icon];
  116.  
  117. // 4.删除按钮
  118. UIButton *del = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  119. del.frame = CGRectMake(, , , );
  120. del.backgroundColor = [UIColor yellowColor];
  121. [del setTitle:@"删除" forState:UIControlStateNormal];
  122. // 添加监听事件
  123. [del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
  124.  
  125. [view addSubview:del];
  126.  
  127. return view;
  128. }
  129. // 监听删除按钮
  130. - (void)deleteClick:(UIButton *)del
  131. {
  132. // NSLog(@"%f",del.superview.frame.origin.y);
  133. // 添加动画效果
  134. [UIView animateWithDuration:kDur animations:^{
  135. // 改变位置
  136. CGRect temp = del.superview.frame;
  137. temp.origin.x = kWidth;
  138. del.superview.frame = temp;
  139.  
  140. del.superview.alpha = ; // 透明
  141. }
  142. // 动画执行结束后执行这个
  143. completion:^(BOOL finished)
  144. {
  145. // 1 获取当前要删除控件的索引
  146. int index = [self.view.subviews indexOfObject:del.superview];
  147. // 2 删除当前行
  148. [del.superview removeFromSuperview];
  149.  
  150. [UIView animateWithDuration:kDur-0.4 animations:^{
  151. // 3 删除后后面控件上移
  152. for (int i = index; i < self.view.subviews.count; i ++)
  153. {
  154. //
  155. UIView *child = self.view.subviews[i];
  156. CGRect temp = child.frame;
  157. temp.origin.y -= kRowH + ;
  158. child.frame = temp;
  159. }
  160. }];
  161.  
  162. // 4 判断删除按钮是否失效
  163. _removeItem.enabled = self.view.subviews.count > ;
  164.  
  165. }];
  166.  
  167. }
  168.  
  169. // 监听头像按钮
  170. - (void)iconClick:(UIButton *)btn
  171. {
  172. //NSLog(@"%@",btn);
  173. // 根据tag获取父控件内部子控件tag为10的子控件
  174. UILabel *lab = (UILabel *)[btn.superview viewWithTag:];
  175.  
  176. NSLog(@"%@",lab.text);
  177. }
  178.  
  179. #pragma mark 删除一行
  180. - (IBAction)remove:(UIBarButtonItem *) sender // 删除
  181. {
  182. NSLog(@"%@",@"remove");
  183.  
  184. // 1.取出最后一个子控件
  185. UIView *last = [self.view.subviews lastObject];
  186. // 判断是否是toolbar
  187. //if([last isKindOfClass:[last class]]) return;//
  188. [UIView animateWithDuration:kDur animations:^{
  189. CGRect temp = last.frame;
  190. temp.origin.x = ;
  191. last.frame = temp; // 改变位置
  192. last.alpha = ; // 改变透明度
  193. } completion:^(BOOL finished) {
  194. // 2.删除子控件
  195. [last removeFromSuperview];
  196. _removeItem.enabled = self.view.subviews.count != ;
  197. NSLog(@"%@",@"remove完毕");
  198. }];
  199.  
  200. // 3.判断剩下的子控件的个数
  201. // if(self.view.subviews.count == 1)
  202. // {
  203. // _removeItem.enabled = NO;
  204. // }
  205.  
  206. }
  207.  
  208. @end

IOS开发学习笔记023-UIToolBar的使用的更多相关文章

  1. iOS开发学习笔记:基础篇

    iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...

  2. ios开发学习笔记(1)

    objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...

  3. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  4. iOS开发学习笔记

    1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...

  5. IOS开发学习笔记017-第一个IOS应用

    第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...

  6. (ios开发学习笔记一)ios项目文件结构

    转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...

  7. ios开发学习笔记040-autolayout 第三方框架Masonry

    不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...

  8. IOS开发学习笔记033-UIScrollView

    1.滚动显示图片 如果图片过大,则需要滚动显示,这是需要用到类UIScrollView,可是实现控件的水平和垂直滚动. 可用三步实现:1 设置UIScrollView,2 设置UIImageView, ...

  9. IOS开发学习笔记026-UITableView的使用

    UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...

随机推荐

  1. Android 5.0以上获取系统运行进程信息

    在Android 5.0以上系统,调用getRunningAppProcesses 方法返回的列表为空,这是因为谷歌考虑到安全原因,已经把这个方法移除掉了, 那以后要获取系统运行的后台进程这个方法用不 ...

  2. uLua学习之创建游戏对象(二)

    前言 上节,刚刚说到创建一个“HelloWorld”程序,大家想必都对uLua有所了解了,现在我们一步步地深入学习.在有关uLua的介绍中(在这里),我们可以发现它使用的框架是Lua + LuaJIT ...

  3. Android商城开发系列(十三)—— 首页热卖商品布局实现

    热卖商品布局效果如下图: 这个布局跟我们上节做的推荐是一样的,也是用LinearLayout和GridView去实现的,新建一个hot_item.xml,代码如下所示: <?xml versio ...

  4. gitlab用户添加ssh免密钥认证后clone还是要求输入密码

    今天在centos 7公网服务器上安装gitlab在配置ssh免密钥时遇到一个奇怪的事,正确添加了本机的公钥到gitlab账户上,进行clone时死活都要你输入密码gitlab使用yum安装的,之前在 ...

  5. linux 命令——33 df(转)

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  6. 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 ...

  7. Activiti学习记录(五)

    1.排他网关 说明: 1) 一个排他网关对应一个以上的顺序流 2) 由排他网关流出的顺序流都有个conditionExpression元素,在内部维护返回boolean类型的决策结果. 3) 决策网关 ...

  8. 表格和网页ico图标

    表格: 表格格式: <table> <tr> 表格的行 <th >表头</th> <th>表头 </th> </tr> ...

  9. 转+更新 Graphviz 教程,例子+ 高级应用 写代码,编程绘制架构图(分层拓扑图) 转自官网

    1. Graphviz介绍 Graphviz是大名鼎鼎的贝尔实验室的几位牛人开发的一个画图工具. 它的理念和一般的“所见即所得”的画图工具不一样,是“所想即所得”. Graphviz提供了dot语言来 ...

  10. 架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级tu)

    最近要画架构图. 一方面有图片洁癖,另外一方面又不想不停的挪动图片. 一开始想用脑图软件. 发现脑图是树状的,架构模块依赖图是网状的.(也可以简化为层级图,不画交互关系.类似 dubbo 的架构图. ...