最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開(kāi)發(fā)之手動(dòng)布局子視圖

 更新時(shí)間:2016年08月04日 10:34:58   作者:android_it  
這篇文章主要介紹了iOS開(kāi)發(fā)之手動(dòng)布局子視圖,從入門(mén)到精通幫助大家更好的開(kāi)發(fā)iOS項(xiàng)目,感興趣的小伙伴們可以參考一下

手動(dòng)布局子視圖;
下面先看下效果圖,我們今天要實(shí)現(xiàn)的效果:


這里我們默認(rèn)用storyboard啟動(dòng):
首先我們要在白色的屏幕上面創(chuàng)建一個(gè)父視圖SuperView(藍(lán)色的背景),在父視圖里面創(chuàng)建四個(gè)小視圖(橘黃色的背景)
下面看代碼,
在SuperView.h文件里面:

#import <UIKit/UIKit.h>

@interface SuperView : UIView{

 UIView * _view01;
 UIView * _view02;
 UIView * _view03;
 UIView * _view04;

}

//聲明創(chuàng)建視圖函數(shù)
-(void) createSubViews;

@end

在SuperView.m文件里面:


#import "SuperView.h"

@interface SuperView ()

@end

@implementation SuperView

-(void) createSubViews{

 //左上角視圖
 _view01 = [[UIView alloc] init];
 _view01.frame=CGRectMake(0, 0, 40, 40);

 //右上角視圖
 _view02 = [[UIView alloc] init];
 _view02.frame=CGRectMake(self.bounds.size.width-40, 0, 40, 40);

 //右下角視圖
 _view03 = [[UIView alloc] init];
 _view03.frame=CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);

 //左下角視圖
 _view04 = [[UIView alloc] init];
 _view04.frame=CGRectMake(0, self.bounds.size.height-40, 40, 40);

 _view01.backgroundColor=[UIColor orangeColor];
 _view02.backgroundColor=[UIColor orangeColor];
 _view03.backgroundColor=[UIColor orangeColor];
 _view04.backgroundColor=[UIColor orangeColor];

 [self addSubview:_view01];
 [self addSubview:_view02];
 [self addSubview:_view03];
 [self addSubview:_view04];

}

//當(dāng)需要重新布局時(shí)調(diào)用此函數(shù)
//通過(guò)此函數(shù)重新設(shè)定子視圖的位置
//手動(dòng)調(diào)整子視圖的位置
-(void)layoutSubviews{

 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1];

 _view01.frame=CGRectMake(0, 0, 40, 40);
 _view02.frame=CGRectMake(self.bounds.size.width-40, 0, 40, 40);
 _view03.frame=CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);
 _view04.frame=CGRectMake(0, self.bounds.size.height-40, 40, 40);

 [UIView commitAnimations];

}


@end

在ViewController.m文件里面:

#import "ViewController.h"
#import "SuperView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.

 //創(chuàng)建一個(gè)父視圖
 SuperView * sView = [[SuperView alloc]init];
 sView.frame = CGRectMake(20, 20, 180, 280);

 //父視圖調(diào)用函數(shù)創(chuàng)建四個(gè)小視圖
 [sView createSubViews];

 sView.backgroundColor = [UIColor blueColor];
 [self.view addSubview:sView];

 UIButton * btn01 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 btn01.frame = CGRectMake(240, 480, 80, 40);
 [btn01 setTitle:@"放大" forState:UIControlStateNormal];
 [btn01 addTarget:self action:@selector(pressLarge) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:btn01];

 UIButton * btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 btn02.frame = CGRectMake(240, 520, 80, 40);
 [btn02 setTitle:@"縮小" forState:UIControlStateNormal];
 [btn02 addTarget:self action:@selector(pressSmall) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:btn02];

 sView.tag = 101;


}

//放大父視圖
-(void) pressLarge{
 SuperView * sView = (SuperView*)[self.view viewWithTag:101];
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1];
 sView.frame=CGRectMake(20, 20, 280, 400);
 [UIView commitAnimations];

}

//縮小父視圖

-(void) pressSmall{
 SuperView * sView = (SuperView*)[self.view viewWithTag:101];
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1];
 sView.frame=CGRectMake(20, 20, 180, 280);
 [UIView commitAnimations];
}


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

@end

 以上代碼書(shū)寫(xiě)完畢,就達(dá)到了上面視圖的效果,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

柘城县| 怀远县| 连山| 武穴市| 益阳市| 琼海市| 永新县| 和林格尔县| 宝山区| 大邑县| 花垣县| 江永县| 交城县| 武义县| 新田县| 石屏县| 宝兴县| 牡丹江市| 恩平市| 淳安县| 荣成市| 三亚市| 孟津县| 岑巩县| 天等县| 资溪县| 望都县| 公主岭市| 辽中县| 彭阳县| 镇远县| 思茅市| 南溪县| 卓资县| 绥宁县| 石狮市| 合山市| 新建县| 宁明县| 堆龙德庆县| 常宁市|