终于效果图

BeyondViewController.h

//
// BeyondViewController.h
// 6_ToolBar
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <UIKit/UIKit.h> @interface BeyondViewController : UIViewController
- (IBAction)addClick:(UIBarButtonItem *)sender;
- (IBAction)removeClick:(UIBarButtonItem *)sender;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *trashItem;
// 从xib界面中拖过来的,前提是设置界面中的file 's owner 为当前控制器类,而且,在代码中载入nib的时候,也要指明owner是当前控制器类的实例对象,一般写self或者空
- (IBAction)deleteBtnClick:(UIButton *)sender;
- (IBAction)headBtnClick:(UIButton *)sender; @end

BeyondViewController.m

//
// BeyondViewController.m
// 6_ToolBar
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "BeyondViewController.h"
#import "RowView.h"
#define kRowHight 65
// 类扩展 class extension 也叫匿名分类
@interface BeyondViewController ()
{
// 成员,数组,由姓名组成
NSArray *_array_name;
// 数组取值时的索引,与图片名挂钩
int _index;
} @end @implementation BeyondViewController - (void)viewDidLoad
{
[super viewDidLoad];
_array_name =@[@"林黛玉",@"薛宝钗",@"妙玉",@"史湘云",@"探春",@"晴雯",@"nana"];
_index = 0;
} - (IBAction)addClick:(UIBarButtonItem *)sender {
// 调用自己定义方法,通过代码创建一行rowView
// UIView *rowView = [self createRowViewByCoding]; // 调用自己定义方法,通过xib创建一行rowView
UIView *rowView = [self createRowViewByXcodeInterfaceBuilding]; // 调用自己定义方法,通过RowView的类方法,返回一个RowView的实例对象
// UIView *rowView = [self createRowViewByXIB]; // 3,加入到当前控制器的view
[self.view addSubview:rowView];
// 5,动画效果
[UIView animateWithDuration:0.2 animations:^{
// 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
CGRect frame = rowView.frame;
frame.origin.x = 0;
rowView.frame=frame;
rowView.alpha = 1;
} completion:^(BOOL finished) {
// 4,置删除button为可用
_trashItem.enabled = YES;
}];
}
// 用xib创建一行 rowView,xib <--> nib ipa <-->apk
- (UIView *)createRowViewByXIB
{
// 假设加入到了数组最后一张,从头開始加入
if (_index >= _array_name.count) {
_index = 0;
} // 下面先计算3个參数,图片名,姓名,rowView要显示的frame的Y坐标
NSString *imgName = [NSString stringWithFormat:@"%d.png",_index];
NSString *labelName = _array_name[_index];
// 新加入一行的y值 取决于view中最后一个子控件的y + height + 1
UIView *lastView = self.view.subviews.lastObject;
CGFloat rowY = lastView.frame.origin.y + lastView.frame.size.height+1; // 调用类方法,返回一个创建好了的rowView
RowView *rowView = [RowView rowViewWithHeadName:imgName andLabelName:labelName andRowY:rowY]; // 为下一次加入行作准备
_index++;
return rowView;
}
// 用xib创建一行 rowView,xib <--> nib ipa <-->apk
- (UIView *)createRowViewByXcodeInterfaceBuilding
{
// mainBundel载入xib,扩展名不用写.xib owner为nil时,手动addTarget,若为xib界面中file's owner指定的class的实例对象时,填self,这样就能够直接拖线
// 1,xib界面中file's owner指定的类class,目的不过右击界面时,能够弹出连线
// 2,进行连线
// 3,代码loadNibNamed中指定owner为哪个实例对象,相当于addTarget中的第一个參数
NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"rowView" owner:self options:nil];
UIView *rowView = arrayXibObjects[0];
// 新加入一行的y值 取决于view中最后一个子控件的y + height + 1
UIView *lastView = self.view.subviews.lastObject;
CGFloat rowY = lastView.frame.origin.y + lastView.frame.size.height+1;
rowView.backgroundColor = [UIColor grayColor];
CGFloat winWidth = self.view.frame.size.width;
rowView.frame = CGRectMake(320, rowY,winWidth, kRowHight);
rowView.alpha = 0; // 假设加入到了数组最后一张,从头開始加入
if (_index >= _array_name.count) {
_index = 0;
} // 2,设置label内容
UILabel *name = (UILabel *)[rowView viewWithTag:1]; name.text = _array_name[_index]; // 3,设置headBtn内容
UIButton *btn = (UIButton *)[rowView viewWithTag:2];
NSString *imgName = [NSString stringWithFormat:@"%d.png",_index];
UIImage *img = [UIImage imageNamed:imgName];
[btn setImage:img forState:UIControlStateNormal];
// 为button加入点击事件
// [btn addTarget:self action:@selector(headBtnClick:) forControlEvents:UIControlEventTouchUpInside]; // 4,设置deleteBtn内容
UIButton *del = (UIButton *)[rowView viewWithTag:3];
// 为删除button加入点击事件
[del addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside]; // 为下一次加入行作准备
_index++;
return rowView;
}
// 用代码创建一行 rowView
- (UIView *)createRowViewByCoding
{
// 假设加入到了数组最后一张,从头開始加入
if (_index >= _array_name.count) {
_index = 0;
}
// 加入一行,实为view,view中左边是头像,右边是名字
UIView *rowView = [[UIView alloc]init];
// 新加入一行的y值 取决于view中最后一个子控件的y + height + 1
UIView *lastView = self.view.subviews.lastObject;
CGFloat rowY = lastView.frame.origin.y + lastView.frame.size.height+1;
rowView.backgroundColor = [UIColor grayColor];
CGFloat winWidth = self.view.frame.size.width;
rowView.frame = CGRectMake(320, rowY,winWidth, kRowHight);
rowView.alpha = 0;
// 2,加入label到view
UILabel *name = [[UILabel alloc]init];
name.frame = CGRectMake(0, 0, 320, kRowHight);
name.backgroundColor = [UIColor clearColor];
name.textAlignment = NSTextAlignmentCenter;
name.tag = 1; //方便后面点击头像button时,得到兄弟标签即姓名
// 随机索引,取姓名,取图片用的
//int randIndex = arc4random_uniform(_array_name.count);
//name.text = _array_name[randIndex];
name.text = _array_name[_index];
[rowView addSubview:name]; // 3,加入头像到view
// UIImage *img = [UIImage imageNamed:@"nana.jpg"];
// UIImageView *head = [[UIImageView alloc]initWithImage:img];
// head.frame = CGRectMake(0, 0,50, 50);
// [rowView addSubview:head]; // 3,加入头像button到view
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(0, 0,65, kRowHight);
NSString *imgName = [NSString stringWithFormat:@"%d.png",_index];
UIImage *img = [UIImage imageNamed:imgName];
[btn setImage:img forState:UIControlStateNormal];
// 为button加入点击事件
[btn addTarget:self action:@selector(headBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[rowView addSubview:btn]; // 4,加入删除button到view
UIButton *del = [[UIButton alloc]init];
del.frame = CGRectMake(260, 0,65, kRowHight);
[del setTitle:@"再见" forState:UIControlStateNormal];
// 为删除button加入点击事件
[del addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[rowView addSubview:del]; // 为下一次加入行作准备
_index++;
return rowView ;
}
// 点击rowView里面的删除button
- (void)deleteBtnClick:(UIButton *)sender
{
// 拿到rowView
UIView *rowView = sender.superview; [UIView animateWithDuration:0.3 animations:^{
// 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
CGRect frame = rowView.frame;
frame.origin.x = 320;
rowView.frame=frame;
rowView.alpha = 0;
} completion:^(BOOL finished) {
// rowView在父容器中的索引
int rowView_index = [self.view.subviews indexOfObject:rowView];
// 将rowView从其父控件中,即self.view中删除
[rowView removeFromSuperview];
_trashItem.enabled = self.view.subviews.count!=1; // rowView身后的这些rowView动画上移
for (int i=rowView_index; i<self.view.subviews.count; i++) {
// rowView身后的这些rowView动画上移
UIView *rowViewSibling = self.view.subviews[i];
[UIView animateWithDuration:0.3 animations:^{
// 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
CGRect frame = rowViewSibling.frame;
frame.origin.y -= kRowHight + 1;
rowViewSibling.frame=frame;
}];
}
}]; } // 点击头像button,弹出alterView
- (void)headBtnClick:(UIButton *)sender
{
NSLog(@"点击了头像button--%@",sender);
UIView *rowView = sender.superview;
UILabel *name = (UILabel *)[rowView viewWithTag:1];
NSLog(@"被点击的button的兄弟标签是:%@",name); // 弹出alterView
UIAlertView *alert = [[UIAlertView alloc]init]; [alert show];
} // 点击删除buttonItem
- (IBAction)removeClick:(UIBarButtonItem *)sender {
_trashItem.enabled = NO;
// 删除最后一行
UIView *last = [self.view.subviews lastObject];
Class cls = [UIToolbar class];
if ([last isKindOfClass:cls]) {
return;
}
// 动画效果
[UIView animateWithDuration:0.2 animations:^{
// 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
CGRect frame = last.frame;
frame.origin.x = 320;
last.frame=frame;
last.alpha = 0;
} completion:^(BOOL finished) {
// 动画完成,从父控件中移除
[last removeFromSuperview];
// 删除完之后,让删除barbuttionitem置灰
_trashItem.enabled = self.view.subviews.count != 1;
}]; } @end

RowView.h

//
// RowView.h
// 6_ToolBar
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <UIKit/UIKit.h> @interface RowView : UIView
@property (weak, nonatomic) IBOutlet UIButton *headBtn;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
- (IBAction)deleteBtnClick:(UIButton *)sender;
+ (RowView *)rowViewWithHeadName:(NSString *)headName andLabelName:(NSString *)labelName andRowY:(CGFloat)rowY;
@end

RowView.m

//
// RowView.m
// 6_ToolBar
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "RowView.h"
#define kRowHight 65
@implementation RowView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/ // + 类方法中不能直接使用_訪问成员变量,必须通过对象.点语法訪问成员变量
// 參数1:头像button的图标名,參数2:姓名标签
+ (RowView *)rowViewWithHeadName:(NSString *)headName andLabelName:(NSString *)labelName andRowY:(CGFloat)rowY
{
// mainBundel载入xib,扩展名不用写.xib
NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"rowView" owner:nil options:nil]; RowView *rowView = arrayXibObjects[0];
// 1,设置rowView的属性
rowView.backgroundColor = [UIColor grayColor];
// 先是在屏幕外面,所以x是320
rowView.frame = CGRectMake(320, rowY,320, kRowHight);
rowView.alpha = 0; // 2,设置label内容
// tag要遍历,效率低,不推荐,最好是rowView.xib连线到RowView.h文件,使用成员变量
// UILabel *nameLabel = (UILabel *)[rowView viewWithTag:1]; rowView.nameLabel.text = labelName; // 3,设置headBtn内容
// tag要遍历,效率低,不推荐,最好是rowView.xib连线到RowView.h文件,使用成员变量
// UIButton *headBtn = (UIButton *)[rowView viewWithTag:2]; UIImage *img = [UIImage imageNamed:headName];
[rowView.headBtn setImage:img forState:UIControlStateNormal];
return rowView;
} // rowView中的删除button被点击了
- (IBAction)deleteBtnClick:(UIButton *)sender {
// 拿到rowView
UIView *rowView = sender.superview; [UIView animateWithDuration:0.3 animations:^{
// 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
CGRect frame = rowView.frame;
frame.origin.x = 320;
rowView.frame=frame;
rowView.alpha = 0;
} completion:^(BOOL finished) {
//NSLog(@"rowView.superView is %@",rowView.superview);
// 先得到控制器的UIView
UIView *control_view = rowView.superview;
// rowView在父容器中的索引
int rowView_index = [control_view.subviews indexOfObject:rowView];
// 将rowView从其父控件中,即self.view中删除
[rowView removeFromSuperview];
// rowView身后的这些rowView动画上移
for (int i=rowView_index; i<control_view.subviews.count; i++) {
// rowView身后的这些rowView动画上移
UIView *rowViewSibling = control_view.subviews[i];
[UIView animateWithDuration:0.3 animations:^{
// 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
CGRect frame = rowViewSibling.frame;
frame.origin.y -= kRowHight + 1;
rowViewSibling.frame=frame;
}];
}
}];
} @end



RowView.xib

iOS_6_ToolBar+xib+红楼梦的更多相关文章

  1. 用R进行文本分析初探——以《红楼梦》为例

    一.写在前面的话~ 刚吃饭的时候同学问我,你为什么要用R做文本分析,你不是应该用R建模么,在我和她解释了一会儿后,她嘱咐我好好写这篇博文,嗯为了娟儿同学,细细说一会儿文本分析. 文本数据挖掘(Text ...

  2. Python3红楼梦人名出现次数统计分析

    一.程序说明 本程序流程是读取红楼梦txt文件----使用jieba进行分词----借助Counter读取各人名出现次数并排序----使用matplotlib将结果可视化 这里的统计除了将“熙凤”出现 ...

  3. 红楼梦人物关系图,一代大师成绝响,下回分解待何人,kindle读书摘要

      人物关系图: https://www.cnblogs.com/images/cnblogs_com/elesos/1120632/o_2033091006.jpg 红楼梦 (古典名著普及文库) ( ...

  4. 红楼梦3d游戏

    1. 红楼梦大观园2d图 2. 红楼梦3d图 潇湘馆 注册机:根据电脑名和时间生成一个id,然后根据注册机生成注册码.

  5. iOS_12_tableViewCell的删除更新_红楼梦

    终于效果图: Girl.h // // Girl.h // 12_tableView的增删改 // // Created by beyond on 14-7-27. // Copyright (c) ...

  6. Google BERT应用之《红楼梦》对话人物提取

    Google BERT应用之<红楼梦>对话人物提取 https://www.jiqizhixin.com/articles/2019-01-24-19

  7. 朴素贝叶斯文本分类-在《红楼梦》作者鉴别的应用上(python实现)

    朴素贝叶斯算法简单.高效.接下来我们来介绍其如何应用在<红楼梦>作者的鉴别上. 第一步,当然是先得有文本数据,我在网上随便下载了一个txt(当时急着交初稿...).分类肯定是要一个回合一个 ...

  8. 红楼梦 + 写入 MySQL

    import requests import re import pymysql from bs4 import BeautifulSoup conn = pymysql.Connect(host=' ...

  9. iOS_10_tableView的简单使用_红楼十二钗

    终于效果图: 方式1,用字典数组 BeyondViewController.h // // BeyondViewController.h // 10_tableView // // Created b ...

随机推荐

  1. javascript定义类或对象的方式

    本文介绍的几种定义类或对象的方式中,目前使用最广泛的是:混合的构造函数/原型方式.动态原型方式.不要单独使用经典的构造函数或原型方式. 工厂方式 构造器函数 原型方式 混合的构造函数/原型方式 动态原 ...

  2. BZOJ 2288 贪心 +链表

    思路: 放个题解好吧. http://www.cnblogs.com/zyfzyf/p/4114774.html //By SiriusRen #include <queue> #incl ...

  3. IT业常见职位英语缩写全攻略及详解

    现在中国人流行起英文名字,连职位也跟着作秀,什么CEO.COO.CFO.CTO.CIO啦,那CEO.COO.CFO.CTO.CIO到底是什么意思呢?总被这些概念搞晕,这可不是搞IT的应该犯的错误哦,好 ...

  4. lua类实现

    _Account = {} --创建一张借记卡 function _Account:new( tb ) local _Tb = tb or {} setmetatable(_Tb, self) sel ...

  5. Android 自定义控件之圆形扩散View(DiffuseView)

    实现效果 使用 属性方法 代码 源码下载 参考链接 实现效果 使用 XML中: <com.airsaid.diffuseview.widget.DiffuseView android:id=&q ...

  6. RedHat/CentOS 大文件拆分及合并与md5验证

    [root@tdh55 mnt]# cd /opt/[root@tdh55 opt]# ll -h-rw-r--r--. 1 root root 7.5G May 12 11:19 TDH-Image ...

  7. 决策树构建算法之—C4.5

    这个网站值得收藏一下,原文链接:http://shiyanjun.cn/archives/428.html 决策树算法的优越性在于:离散学习算法进行组合总可以表达任意复杂的布尔函数,并不受数据集的限制 ...

  8. Socket 数据包顺序的问题

    今天遇到一个问题,到现在还未查明原因,记录一下,留后续跟踪. 基于Netty的Socket通讯问题,Server在向Client发送数据时,假设数据原顺序为1,2,3,4...  但到了客户端顺序可能 ...

  9. Zabbix4.0 Web管理界面中文乱码解决方法(转)

    Zabbix安装好之后,监控图形页面出现字符集乱码 解决方法:1.复制本地电脑C:\Windows\Fonts\simkai.ttf(楷体)上传到zabbix服务器网站目录的fonts目录下 2.za ...

  10. 对服务器磁盘、CPU、内存使用状态,设置163邮件告警

    1,桥接模式可上网,首先你的邮箱已经开通yum -y install mailx dos2unix.x86_64  mailx -V[root@localhost ~]# vim /etc/mail. ...