您的位置:首页 > 产品设计 > UI/UE

IOS学习笔记---UIView

2013-12-25 20:00 316 查看
UIView是很多视图控件的父类,继承UIView的控件具有它的一切功能,一下代码来学习一下UIView:

//
//  ViewController.m
//  UIViewDemo
//
//  Created by 5016 on 13-12-24.
//  Copyright (c) 2013年 dradon. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
//1.使用initWithFrame
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[self.view addSubview:view];
[view release];
[view setBackgroundColor:[UIColor blueColor]];

//2.bounds
UIView *view2 = [[UIView alloc]init];
[view2 setBounds:CGRectMake(0, 0, 50, 50)];
[self.view addSubview:view2];
[view2 setBackgroundColor:[UIColor redColor]];
[view2 setCenter:CGPointMake(75, 25)];//通过设置中心点来设置
[view2 release];

/**********************视图层次关系***************************/
/*
*最先创建的会覆盖之前创建的
*/
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(120, 0, 120, 120)];
[view3 setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:view3];
[view3 release];

UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(140, 0, 120, 120)];
[view4 setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:view4];
[view4 release];

UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(160, 0, 120, 120)];
[view5 setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:view5];
[view5 release];

/*
*插入视图
*/
UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(150, 0, 120, 120)];
[view6 setBackgroundColor:[UIColor redColor]];
[self.view insertSubview:view6 aboveSubview:view4];//插入到view4上面
[view6 release];

UIView *view7 = [[UIView alloc]initWithFrame:CGRectMake(130, 20, 120, 120)];
[view7 setBackgroundColor:[UIColor blackColor]];
[self.view insertSubview:view7 atIndex:6];//添加到父视图的第几个索引值的视图之上,索引值按创建的先后顺序,由0开始
[view7 release];

/**********************父子视图的添加删除关系***************************/

UIView *view8 = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 120, 120)];
[view8 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view8];//插入到view4上面
[view8 release];

UIView *view9 = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 100, 100)];
[view9 setBackgroundColor:[UIColor greenColor]];
NSLog(@"view9 的引用计数 = %d",[view9 retainCount]);
[view8 addSubview:view9];//插入到view4上面
NSLog(@"view9 的引用计数 = %d",[view9 retainCount]);
[view9 release];
//[view9 removeFromSuperview];//自己删除自己 引用计数-1
NSLog(@"view9 的引用计数 = %d",[view9 retainCount]);

//如果父类自己删除自己,子类可视化被去掉,其实还在,引用计数不变
[view8 removeFromSuperview];
//[self.view addSubview:view9];
// NSLog(@"view9 的引用计数 = %d",[view9 retainCount]);

//嵌套图形

view10 = [[UIView alloc]initWithFrame:CGRectMake(20, 140, 280, 280)];
[view10 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view10];//插入到view4上面
[view10 release];

UIView *view11 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 260, 260)];
[view11 setBackgroundColor:[UIColor blueColor]];
[view10 addSubview:view11];//插入到view4上面
[view11 release];
[view11 setAutoresizesSubviews:YES];
[view11 setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

UIView *view12 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 240, 240)];
[view12 setBackgroundColor:[UIColor orangeColor]];
[view11 addSubview:view12];//插入到view4上面
[view12 release];
//设置自动适应父类大小变化
[view12 setAutoresizesSubviews:YES];
[view12 setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 420, 80, 40)];
[button setTitle:@"放大" forState:UIControlStateNormal];
//绑定事件
[button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
[button release];

button1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(140, 420, 80, 40)];
[button1 setTitle:@"缩小" forState:UIControlStateNormal];    //绑定事件
[button1 addTarget:self action:@selector(onclick1:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button1];
[button1 release];

}

-(void)onclick:(UIButton*)btn
{
NSLog(@"btn");
//设置缩小,不可无限缩小,当缩小为看不见的适合,就不能再放大了
view10.frame = CGRectInset(view10.frame, -10, -10);

}

-(void)onclick1:(UIButton*)btn
{
NSLog(@"btn1");
//设置放大
view10.frame = CGRectInset(view10.frame, 10, 10);
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: