iOS学习应用开发就业课_026:手动布局子视图

建立两个视图:

1.mainView,

  2个按钮:实现视图的方法缩小功能

  2.视图方法缩小的动画

2.SubView

  包含4个视图;

  4个视图在4个角

  随着视图的大小变化而变化


复习次数:

6月3日

6月4日


源码实现:Main界面第二部分是View界面

#import "ViewController.h"

#import "View02.h"

@interfaceViewController ()

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIButton *btnLarge=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnLarge.frame=CGRectMake(50,50,80,40);

    btnLarge.backgroundColor=[UIColoryellowColor];

    [btnLargesetTitle:@"视图变大"forState:UIControlStateNormal];

    [btnLargeaddTarget:selfaction:@selector(pressBig)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnLarge];

    

    UIButton *btnSmall=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnSmall.frame=CGRectMake(50,120,80,40);

    btnSmall.backgroundColor=[UIColoryellowColor];

    [btnSmallsetTitle:@"视图变大"forState:UIControlStateNormal];

    [btnSmalladdTarget:selfaction:@selector(pressSmall)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnSmall];

    

    View02* vc02=[[View02alloc]initWithFrame:CGRectMake(50,220,100, 200)];

    vc02.tag=101;

    vc02.backgroundColor=[UIColoryellowColor];

    [vc02createSubViews];

    [self.viewaddSubview:vc02];


    

}

-(void)pressBig

{

    UIView *vc=[[UIViewalloc]init];

    vc=[self.viewviewWithTag:101];

    

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:1];

    vc.frame=CGRectMake(50,220,200,400);

    [UIViewcommitAnimations];

}


-(void)pressSmall

{


    UIView *vc=[[UIViewalloc]init];

    vc=[self.viewviewWithTag:101];

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:1];

    

    vc.frame=CGRectMake(50,220,100,200);

        [UIViewcommitAnimations];

}



View02界面

#import"View02.h"

@implementation View02

-(void)createSubViews

{

    _view01=[[UIViewalloc]init];

    _view02=[[UIViewalloc]init];

    _view03=[[UIViewalloc]init];

    _view04=[[UIViewalloc]init];

    

    _view01.backgroundColor=[UIColorredColor];

    _view02.backgroundColor=[UIColorredColor];

    _view03.backgroundColor=[UIColorredColor];

    _view04.backgroundColor=[UIColorredColor];

    

    _view01.frame=CGRectMake(0,0, self.bounds.size.width/8,self.bounds.size.height/8);

    _view02.frame=CGRectMake(self.bounds.size.width/8*7,0, self.bounds.size.width/8,self.bounds.size.height/8);

    _view03.frame=CGRectMake(0,self.bounds.size.height/8*7,self.bounds.size.width/8,self.bounds.size.height/8);

    _view04.frame=CGRectMake(self.bounds.size.width/8*7,self.bounds.size.height/8*7,self.bounds.size.width/8,self.bounds.size.height/8);

    

    [selfaddSubview:_view01];

    [selfaddSubview:_view02];

    [selfaddSubview:_view03];

    [selfaddSubview:_view04];

    

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

-(void)layoutSubviews

{

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:2];

    _view01.frame=CGRectMake(0,0, self.bounds.size.width/8,self.bounds.size.height/8);

    _view02.frame=CGRectMake(self.bounds.size.width/8*7,0, self.bounds.size.width/8,self.bounds.size.height/8);

    _view03.frame=CGRectMake(0,self.bounds.size.height/8*7,self.bounds.size.width/8,self.bounds.size.height/8);

    _view04.frame=CGRectMake(self.bounds.size.width/8*7,self.bounds.size.height/8*7,self.bounds.size.width/8,self.bounds.size.height/8);


    [UIViewcommitAnimations];

}


@end






猜你喜欢

转载自blog.csdn.net/sap_support/article/details/51545507