终于效果图

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. Java-Zipkin:Zipkin 介绍

    ylbtech-Java-Zipkin:Zipkin 介绍 1.返回顶部 1. 介绍 Zipkin 是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 ...

  2. 第19章 Redis的一些常用技术

    19.1 Redis的基础事务                       图19-1 Redis命令执行事务的过程                      19-1:在Spring中使用Redis ...

  3. [Pulgin] 前端上传组件Plupload使用指南

    我之前写过一篇文章<文件上传利器SWFUpload使用指南>,里面介绍了上传组件SWFUpload的使用方法,但现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SW ...

  4. 使用multiprocessing解决PyMuPDF不支持多线程加载导致的界面卡死无响应问题,及一个PyQt5实现的简易PDF阅读器例子

    最近在用PyMuPDF实现一个PDF阅读器,发现PyMuPDF在加载某些epub时耗时非常长,有的长达10几秒,会导致界面卡死无响应. 尝试用多线程后台加载,发现还是不能解决问题,和作者交流(issu ...

  5. numpy快速指南

    Quickstart tutorial 引用https://docs.scipy.org/doc/numpy-dev/user/quickstart.html Prerequisites Before ...

  6. Python基础教程思维导图笔记

    说明:直接查看图片可能不太清楚,用浏览器打开后,按住 Ctrl ,网上滚动鼠标放大浏览器页面,可以看清楚图片

  7. 让Kafka在scala里面跑起来

    Kafka集群对消息的保存是根据Topic进行归类的,由消息生产者(Producer)和消息消费者(Consumer)组成,另外,每一个Server称为一个Broker(经纪人).对于Kafka集群而 ...

  8. ObjecT4:On-line multiple instance learning (MIL)学习

    原文链接:http://blog.csdn.net/ikerpeng/article/details/19235391 用到论文,直接看翻译. 文章:Robust object tracking wi ...

  9. Session和Cookie对比详解

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  10. 巧用Ajax的beforeSend提高用户体验

    jQuery是经常使用的一个开源js框架,其中的$.ajax请求中有一个beforeSend方法,用于在向服务器发送请求前执行一些动作. $.ajax({ beforeSend:function(){ ...