UIView的详细用法


UIView的使用

 

1.UIView创建和使用

//  重要性:

  (1) 常见控件父类或间接父类都是UIView

UILabel->UIView

UIImageView->UIView

UIButton->UIControl->UIView

  (2)自定义控件

//  继承与UIView, view加上其他控件

 (3)使用UIView作为界面布局, UIView作为其他控件的容器

//理解: 显示一块矩形区域

UIView *view1 = [[UIView alloc] init];

view1.frame = CGRectMake(100, 100, 100, 100);

view1.backgroundColor = [UIColor redColor];

[self.view addSubview:view1];

 

2.常用属性

//位置

view1.frame

//只改大小, 不改位置

view1.bounds = CGRectMake(0, 0, 50, 50);

view1.backgroundColor = [UIColor yellowColor];

 

//使用tag区分不同控件

view1.tag = 100;

 

//设置中心点位置

view1.center = CGPointMake(50, 50);

 

//是否打开用户交互

//UILabel和UIImageView控件这个值是NO

//  按钮加入到UIImageView没有反应

view1.userInteractionEnabled = YES;

 

//view设置圆角

view1.layer.cornerRadius = 10;

view1.clipsToBounds = YES;

 

//是否隐藏

view1.hidden = YES;

view1.hidden = NO;

 

//透明度

view1.alpha = 0.7;

 

//3.常用方法

view1 addSubview:<#(UIView *)#>

//界面上移除控件

[view1 removeFromSuperview];

 

//视图层级关系方法

view1.frame = CGRectMake(100, 100, 100, 100);

 

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];

view2.backgroundColor = [UIColor blueColor];

[self.view addSubview:view2];

 

//放到最前面

[self.view bringSubviewToFront:view1];

 

//最后面

[self.view sendSubviewToBack:view1];

 

//插入视图

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(125, 125, 100, 100)];

view3.backgroundColor = [UIColor greenColor];

//[self.view addSubview:view3];

[self.view insertSubview:view3 belowSubview:view2];

 

//属性

//所有子视图

self.view.subviews

 

//父视图

self.view.superview

 

//4.使用UIView进行控件自定义和界面布局

  view+2个label+一个图片

//原理: 只是原理展示, 控件定制需要继承

UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 100)];

photoView.backgroundColor = [UIColor lightGrayColor];

[self.view addSubview:photoView];

 

//头像

UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];

headImageView.image = [UIImage imageNamed:@"defaultHead.png"];

[photoView addSubview:headImageView];

 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, 100, 30)];

label.text = @"风景";

[photoView addSubview:label];

 

 

UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 100, 30)];

timeLabel.text = @"2014-08-26";

[photoView addSubview:timeLabel];

 

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap)];

[photoView addGestureRecognizer:tap];

 

 

//5.UIView的动画(扩展: 仿射变换)

UIImageView *bullet = [[UIImageView alloc] initWithFrame:CGRectMake(10, 450, 20, 20)];

bullet.image = [UIImage imageNamed:@"bullet.png"];

[self.view addSubview:bullet];

 

//    [UIView animateWithDuration:2 animations:^{

//        bullet.frame = CGRectMake(300, 450, 50, 50);

//        bullet.alpha = 0;

//    }];

 

//

[UIView animateWithDuration:4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

    bullet.frame = CGRectMake(300, 450, 50, 50);

    //bullet.alpha = 0;

} completion:^(BOOL finished) {

    [bullet removeFromSuperview];

}];

 

}

-(void)dealTap

{

    NSLog(@"点击相册");

}

 

 

 

 

UIView的相互嵌套

//能干什么: 组合出系统没有的控件

 

//实例: 图片列表界面需要按钮

//  左边一个图片, 右边两个label

 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(0, 20, 320, 100);

button.backgroundColor = [UIColor grayColor];

[self.window addSubview:button];

UIImage *image = [[UIImage imageNamed:@"table_cell_bg.png"] stretchableImageWithLeftCapWidth:8 topCapHeight:0];

[button setBackgroundImage:image forState:UIControlStateNormal];

 

//按钮的左边加上图片 defaultHead.png

UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];

headView.image = [UIImage imageNamed:@"defaultHead.png"];

[button addSubview:headView];

 

UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 200, 30)];

nameLabel.text = @"美女";

[button addSubview:nameLabel];

 

 

UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 65, 200, 30)];

detailLabel.text = @"12-11 6:09 (12张)";

detailLabel.font = [UIFont systemFontOfSize:14];

[button addSubview:detailLabel];

 

总结

(1). 几乎所有控件都是UIView的子类或间接子类, UIView有个方法是addSubview, 意味"理论上"任何控件都可以相互组合

(2) 组合 现在注意坐标是相对坐标

    按钮直接加上图片上, 需要开启图片的用户交互

 

 

//3. UIView层次感

 

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];

view1.backgroundColor = [UIColor redColor];

[self.window addSubview:view1];

 

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 250, 100, 100)];

view2.backgroundColor = [UIColor greenColor];

[self.window addSubview:view2];

 

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(200, 300, 100, 100)];

view3.backgroundColor = [UIColor blueColor];

[self.window addSubview:view3];

 

//后面加入的view在最上层

//(1)某个view放在上面

[self.window bringSubviewToFront:view1];

//(2)某个view放在下面

[self.window sendSubviewToBack:view1];

//(3)知道一个视图的父视图

UIView *sview = view1.superview;

NSLog(@"w=%f h=%f",sview.frame.size.width,

      sview.frame.size.height);

//(4)一个视图中有多少子视图

NSArray *array = self.window.subviews;

NSLog(@"array = %@",array);

 

//(5)让一个视图消失

//view1.hidden = YES;

//view1.alpha = 0;

//彻底从父视图消息

//[view1 removeFromSuperview];

 

//(6)插入视图

//添加的黄色的视图

UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 100, 100)];

view4.backgroundColor = [UIColor yellowColor];

[self.window insertSubview:view4 belowSubview:view1];

 

//(7)交换两个视图的层次

[self.window exchangeSubviewAtIndex:3 withSubviewAtIndex:4];

 

转载于:https://www.cnblogs.com/new-docker/p/4933596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值