1. 程式人生 > >iOS_6_ToolBar+xib+紅樓夢

iOS_6_ToolBar+xib+紅樓夢

iboutlet com array send 中間 nor named make [0

終於效果圖


技術分享


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+紅樓夢