动画 iOS 手动布局子视图和自动布局子视图 - _lb03

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多帮助力 👁️ 584 次阅读

iOS 手动布局子视图和自动布局子视图

手动布局子视图

手动重新布局子视图的关键在于UIView的

(void) layoutSubviews方法 该方法会在该类对象的视图重新布局时调用,在该方法内动态的改变视图位置实现手动布局子视图 可以简单看一下下面的代码:

//

// ViewController.m

// 手动布局子视图

//

// Created by 朱敬业 on 2023/5/31.

//

#import "ViewController.h"

#import "SuperView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

SuperView* sView = [[SuperView alloc] init];

sView.frame = CGRectMake(20, 20, 180, 280) ;

sView.backgroundColor = UIColor.blueColor;

[self.view addSubview: sView] ;

UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

btn1.frame = CGRectMake(240, 480, 80, 40) ;

[btn1 setTitle:@"放大" forState:UIControlStateNormal] ;

[btn1 addTarget:self action:@selector(presslarge) forControlEvents:UIControlEventTouchUpInside] ;

UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;

btn2.frame = CGRectMake(240, 520, 80, 40) ;

[btn2 setTitle:@"缩小" forState:UIControlStateNormal] ;

[btn2 addTarget:self action:@selector(presssmall) forControlEvents:UIControlEventTouchUpInside] ;

[self.view addSubview:btn1] ;

[self.view addSubview:btn2] ;

sView.tag = 101;

[sView createSubviews];

}

- (void)presslarge {

//通过标签找到子视图

SuperView* sview = [self.view viewWithTag:101] ;

//执行一个动画,p1:动画持续的时间,p2:动画执行的动作

[UIView animateWithDuration:2 animations:^{

sview.frame = CGRectMake(20, 20, 300, 400) ;

}] ; }

- (void)presssmall {

SuperView* sview = [self.view viewWithTag:101] ;

[UIView animateWithDuration:2 animations:^{

sview.frame = CGRectMake(20, 20, 90, 240) ;

}];

}

@end

//

// SuperView.h

// 手动布局子视图

//

// Created by 朱敬业 on 2023/5/31.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interface SuperView : UIView {

UIView* _view01 ;

UIView* _view02 ;

UIView* _view03 ;

UIView* _view04 ;

UIView* _view05 ;

}

- (void)createSubviews ;

- (void)layoutSubviews ;

@end

NS_ASSUME_NONNULL_END

//

// SuperView.m

// 手动布局子视图

//

// Created by 朱敬业 on 2023/5/31.

//

#import "SuperView.h"

@implementation SuperView

- (void)createSubviews {

_view01 = [[UIView alloc] init] ;

_view01.frame = CGRectMake(0, 0, 40, 40) ;

_view02 = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 40, 0, 40, 40)] ;

_view03 = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 40, 40, 40)] ;

_view04 = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 40, 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 {

_view01.frame = CGRectMake(0, 0, 40, 40) ;

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

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

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

}

/*

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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

}

*/

@end

自动布局子视图

自动布局子视图的关键在于设置子视图的自动布局属性autoresizingMake; 直接看一下代码就行了

//

// ViewController.m

// 自动布局子视图

//

// Created by 朱敬业 on 2023/6/1.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

_superView = [[UIView alloc] init] ;

_superView.frame = CGRectMake(20, 20, 180, 280) ;

_superView.backgroundColor = UIColor.blueColor;

//子视图的坐标是在父视图上算的

_lb01 = [[UILabel alloc] init] ;

_lb01.frame = CGRectMake(0, 0, 40, 40) ;

_lb01.text = @"1" ;

_lb01.backgroundColor = UIColor.greenColor ;

_lb02 = [[UILabel alloc] init] ; 拼多多帮助力入口

_lb02.frame =CGRectMake(180 - 40, 0, 40, 40) ;

_lb02.text = @"2" ;

_lb02.backgroundColor = UIColor.greenColor ;

_lb03 = [[UILabel alloc] init] ;

_lb03.frame =CGRectMake(0, 280 - 40, 40, 40) ;

_lb03.text = @"3" ;

_lb03.backgroundColor = UIColor.greenColor ;

_lb04 = [[UILabel alloc] init] ;

_lb04.frame =CGRectMake(180 - 40, 280 - 40, 40, 40) ;

_lb04.text = @"4" ;

_lb04.backgroundColor = UIColor.greenColor ;

[_superView addSubview:_lb01] ;

[_superView addSubview:_lb02] ;

[_superView addSubview:_lb03] ;

[_superView addSubview:_lb04] ;

[self.view addSubview:_superView] ;

_Viewcenter = [[UIView alloc] init] ;

_Viewcenter.frame = CGRectMake(0, 0, _superView.bounds.size.width, 40) ;

_Viewcenter.backgroundColor = UIColor.orangeColor ;

//视图的center属性表示中心的位置,且可以决定视图的位置

_Viewcenter.center = CGPointMake(180 / 2, 280 / 2) ;

[_superView addSubview:_Viewcenter] ;

//自动布局属性

_lb04.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;

_lb03.autoresizingMask = UIViewAutoresizingFlexibleTopMargin ;

_lb02.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin ;

_Viewcenter.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin ;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

static BOOL islarge = NO ;

if (islarge == NO) {

[UIView animateWithDuration:2 animations:^{

self->_superView.frame = CGRectMake(10, 10, 300, 480) ;

}] ;

} else {

[UIView animateWithDuration:2 animations:^{

self->_superView.frame = CGRectMake(20, 20, 180, 280) ;

}] ;

}

islarge = !islarge ;

}

@end

相关阅读

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧