#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
#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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有