这里使用代码实现

大概过程:

  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的使用的更多相关文章

  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. 判断一个点是否在多边形区域内--C算法

    /*函数的输入:(1)当前点的坐标p(2)区域顶点数组pt[]:(3)顶点数nCount 输出: 在区域内返回TRUE,否则返回FALSE.  Point类型是一个结构: struct Point { ...

  2. IOS 当一个控件被添加到父控件中会调用(didMoveToSuperview)

    /** * 当一个控件被添加到父控件中就会调用 */ - (void)didMoveToSuperview { if (self.group.opened) { self.nameView.image ...

  3. POJ 2104 K-th Number(分桶,线段树,主席树)

    一道比较经典的数据结构题.可以用多种方式来做. 一,分桶法(平方分解). 根据数字x的大小和区间内不大于x的数字数量cnt的单调性,可知第k大数kth对应的cnt应该满足cnt≥k, 且kth是满足条 ...

  4. Java 集合框架_上

    集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的. 该框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的 ...

  5. python序列化(数据本地存放持久性存储)和反序列化

    http://blog.csdn.net/uestcyao/article/details/7874817 #读取图片并存储为矩阵 from scipy.misc import imread im = ...

  6. Linux---cp命令学习

    cp命令 cp source_file  target_file 能够复制文件,如果target_file所指定的文件不存在,cp就创建这个文件,如果已经存在,就把文件内容清空并把source_fil ...

  7. 如果int x=20, y=5,则语句System.out.println(x+y +""+(x+y)+y); 的输出结果是()

    答案是25255 小括号优先级高,所以先算小括号内的x+y=25 然后再算前面的x+y=25 但是中间有个空的字符串,java会把这个空字符串后面的都当成字符串看待,所以结果是25255

  8. PAT (Basic Level) Practise (中文)- 1006. 换个格式输出整数 (15)

    http://www.patest.cn/contests/pat-b-practise/1006 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换 ...

  9. Windows服务调试

    Windows 服务(附服务开发辅助工具) 转: http://www.cnblogs.com/BoyXiao/archive/2011/08/07/2130208.html 近来在 Windows ...

  10. C# 文件操作 常用的类

    File------实用类,提供许多静态方法,用于移动.删除.和复制文件. Directory------实用类,提供许多静态方法,用于移动.删除和复制目录. Path------ 实用类,用于处理路 ...