IOS学习笔记---UIView

15 篇文章 0 订阅

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值