最終效果圖

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHAgY2xhc3M9"p1">
BeyondViewController.h
// // BeyondViewController.h // 6_ToolBar // // Created by beyond on 14-7-24. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import@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,置刪除按鈕為可用
_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];
// 為按鈕添加點擊事件
// [btn addTarget:self action:@selector(headBtnClick:) forControlEvents:UIControlEventTouchUpInside];
// 4,設置deleteBtn內容
UIButton *del = (UIButton *)[rowView viewWithTag:3];
// 為刪除按鈕添加點擊事件
[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; //方便後面點擊頭像按鈕時,得到兄弟標簽即姓名
// 隨機索引,取姓名,取圖片用的
//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,添加頭像按鈕到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];
// 為按鈕添加點擊事件
[btn addTarget:self action:@selector(headBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[rowView addSubview:btn];
// 4,添加刪除按鈕到view
UIButton *del = [[UIButton alloc]init];
del.frame = CGRectMake(260, 0,65, kRowHight);
[del setTitle:@"再見" forState:UIControlStateNormal];
// 為刪除按鈕添加點擊事件
[del addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[rowView addSubview:del];
// 為下一次添加行作准備
_index++;
return rowView ;
}
// 點擊rowView裡面的刪除按鈕
- (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
RowView.h
//
// RowView.h
// 6_ToolBar
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//
#import
@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:頭像按鈕的圖標名,參數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中的刪除按鈕被點擊了
- (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
RowView.xib
