这里我们默认用storyboard启动:
首先我们要在白色的屏幕上面创建一个父视图SuperView(蓝色的背景),在父视图里面创建四个小视图(橘黄色的背景)
下面看代码,
在SuperView.h文件里面:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#import <UIKit/UIKit.h>
@interface SuperView : UIView{
UIView * _view01;
UIView * _view02;
UIView * _view03;
UIView * _view04;
}
//声明创建视图函数
-(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];
}
//当需要重新布局时调用此函数
//通过此函数重新设定子视图的位置
//手动调整子视图的位置
-(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文件里面:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#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.
//创建一个父视图
SuperView * sView = [[SuperView alloc]init];
sView.frame = CGRectMake(20, 20, 180, 280);
//父视图调用函数创建四个小视图
[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
|
以上代码书写完毕,就达到了上面视图的效果,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 74
-
2025-06-04 26
-
2025-05-25 84
-
2025-05-25 36
-
2025-05-29 84
热门评论


