標籤:ios 紅樓夢 xib
最終
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 [email protected][@"林黛玉",@"薛寶釵",@"妙玉",@"史湘雲",@"探春",@"晴雯",@"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<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; }]; } }]; }// 點擊頭像按鈕,彈出alterView- (void)headBtnClick:(UIButton *)sender{ NSLog(@"點擊了頭像按鈕--%@",sender); UIView *rowView = sender.superview; UILabel *name = (UILabel *)[rowView viewWithTag:1]; NSLog(@"被點擊的按鈕的兄弟標籤是:%@",name); // 彈出alterView UIAlertView *alert = [[UIAlertView alloc]init]; [alert show];}// 點擊刪除按鈕Item- (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:頭像按鈕的表徵圖名,參數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<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