UIView常见方法总结(一)

来源:互联网 发布:哪个软件播放rmvb 编辑:程序博客网 时间:2024/06/02 03:53



一、类扩展(class extension,匿名分类)

1.格式

@interface 类名()

{

    //成员变量……

}

@end


2.作用

1>写在.m文件中

2>一般用来扩充私有成员变量、@property属性、方法等


二、随机数生成

1.arc4random()会生成任意正整数和0

2.arc4random_uiiform(100)会生成0~99的整数(包括099


三、UIView常见方法

1.addSubView:(UIView *)child

添加子控件用(最新添加的子控件,会显示在最上面)


2.NSArray *subViews

通过addSubView:方法添加的子控件都会存在于这个数组中


3.removeFromSuperview

将控件本身从父控件中移除(控件本身也会从父控件的subviews数组中移除)


4.(UIView *)viewWithTag:(int)mytag

返回tag值为mytag的子控件,内部的实现是通过forin遍历控件数组,只会返回第一个匹配的tag值的subview,取出最前面的目标tag


//

//  RowView.h

//  01-联系人管理

//

//  Created by apple on 13-11-25.

//  Copyright (c) 2013 itcast. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface RowView :UIView

+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name;


@property (nonatomic,weak) IBOutletUIButton *iconBtn;

@property (nonatomic,weak) IBOutletUILabel *nameLabel;


@end



//

//  RowView.m

//  01-联系人管理

//

//  Created by apple on 13-11-25.

//  Copyright (c) 2013 itcast. All rights reserved.

//


#import "RowView.h"


@implementation RowView


+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name

{

    RowView *view = [[NSBundlemainBundle] loadNibNamed:@"RowView"owner:niloptions:nil][0];

    

    // 1.设置图标

//    UIButton *iconBtn = (UIButton *)[view viewWithTag:1];

    [view.iconBtnsetImage:[UIImageimageNamed:icon] forState:UIControlStateNormal];

    

    // 2.设置姓名

//    UILabel *nameLabel = (UILabel *)[view viewWithTag:2];

    view.nameLabel.text = name;

    

   return view;

}




@end




//

//  MJViewController.m

//  04-联系人管理

//

//  Created by apple on 13-11-24.

//  Copyright (c) 2013 itcast. All rights reserved.

//


#import "MJViewController.h"

#import "RowView.h"


#define kDuration 0.5

#define kRowH 50

#define kNameTag 10


// 类扩展(class extension,匿名分类)

@interface MJViewController ()

{

   NSArray *_allNames;

}

@end


@implementation MJViewController


#pragma mark 控制器的view加载完毕的时候调用一次

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    _allNames =@[@"西门庆",@"东门庆",@"北门庆",@"南门庆",@"中门庆"];

}


#pragma mark 添加一行

- (IBAction)add:(UIBarButtonItem *)sender {

    // 0.取出最后一个子控件

   UIView *last = [self.view.subviewslastObject];

    //这行的Y = 最后一个子控件的Y +最后一个子控件的高度 + 间距

    CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;

    

    // 1.创建一行

   UIView *rowView = [selfcreateRowView];

    

    // 2.添加一行到控制器的view

    [self.viewaddSubview:rowView];

    

    // 3.让删除item有效

    _removeItem.enabled =YES;

    

    // 4.执行动画

    rowView.frame =CGRectMake(320, rowY,320, kRowH);

    rowView.alpha =0;

    

    // 4.1.开始动画

    [UIViewanimateWithDuration:kDurationanimations:^{

        rowView.frame =CGRectMake(0, rowY,320, kRowH);

        rowView.alpha =1;

    }];

}


#pragma mark 创建一行(xib中加载一行的view)

// xib == nib

- (UIView *)createRowView

{

    NSString *icon = [NSStringstringWithFormat:@"01%d.png",arc4random_uniform(9)];

   NSString *name = _allNames[arc4random_uniform(_allNames.count)];

    

   RowView *rowView = [RowViewrowViewWithIcon:icon name:name];

    

   return rowView;

}


#pragma mark 监听删除按钮点击

- (void)deleteClick:(UIButton *)btn

{

    [UIViewanimateWithDuration:kDurationanimations:^{

       CGRect tempF = btn.superview.frame;

        tempF.origin.x =320;

        btn.superview.frame = tempF;

        

        btn.superview.alpha =0;

    }completion:^(BOOL finished) {

        // 1.获得即将删除的这行在数组中的位置

       int startIndex = [self.view.subviewsindexOfObject:btn.superview];

        

        // 2.删除当前行

        [btn.superviewremoveFromSuperview];

        

        [UIViewanimateWithDuration:0.3animations:^{

            // 3.遍历后面的子控件

           for (int i = startIndex; i<self.view.subviews.count; i++) {

               UIView *child = self.view.subviews[i];

               CGRect tempF = child.frame;

                tempF.origin.y -=kRowH + 1;

                child.frame = tempF;

            }

        }];

        

        // 4.判断垃圾桶

        _removeItem.enabled =self.view.subviews.count >1;

    }];

}


#pragma mark 监听头像按钮点击

- (void)iconClick:(UIButton *)btn

{

    // 1.取得按钮的父控件(因为labelbtn处在同一个父控件中)

//    btn.superview;

    // 2.获得文本标签

   UILabel *label = (UILabel *)[btn.superviewviewWithTag:kNameTag];

    

    // 3.打印

   NSLog(@"名字是:%@", label.text);

}


#pragma mark 删除一行

- (IBAction)remove:(UIBarButtonItem *)sender {

    // 1.取出最后一个子控件

   UIView *last = [self.view.subviewslastObject];

    

    [UIViewanimateWithDuration:kDurationanimations:^{

       CGRect tempF = last.frame;

        tempF.origin.x =320;

        last.frame = tempF;

        

        last.alpha =0;

    }completion:^(BOOL finished) {

        [last removeFromSuperview];

        

        // 剩下的子控件个数 > 1就能够点击删除

        _removeItem.enabled =self.view.subviews.count >1;

    }];

}

@end



0 0
原创粉丝点击