开发小结之layoutSubviews调用

iOS中的layoutSubviews是UIView的方法,该方法用于更精确的视图进行布局,可以在子类里重写这个方法。
开发过程中,了解layoutSubviews何时会被调用,从而可以熟悉uiview的重绘机制
参考网络资料,并进行验证,在此记录,希望大家一起探讨学习

测试定义UIView类TestView

#import "TestView.h"


@implementation TestView


-(id)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];

    if (self) {

        NSLog(@"initWithFrame:%@",NSStringFromCGRect(frame));

    }

    return self;

}

//重写layoutSubviews方法

-(void)layoutSubviews{

    NSLog(@"layoutSunView %@"self);

    [super layoutSubviews];

}


@end



测试代码
在视图控制器 ViewControllerviewDidLoad方法调用 testX

- (void)viewDidLoad {

    [super viewDidLoad];

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

    [self testX];

}

1、init初始化不会触发layoutSubviews
结果:没有调用 layoutSubviews

2、addSubview会触发layoutSubviews(frame!={0,0,0,0})
frame={0,0,0,0}
测试代码test21

-(void)test21{

    TestView *test = [[TestView allocinit];

    [self.view addSubview:test];

}

结果: 调用了 addSubview,但frame={0,0,0,0},没有绘制,所以不会调用TestView类的layoutSubviews方法


frame!={0,0,0,0}
测试代码test22

-(void)test22{

    TestView *test = [[TestView allocinit];

    test.frame = CGRectMake(00100100);

    [self.view addSubview:test];

}

结果: frame!={0,0,0,0},会调用TestView类的layoutSubviews方法



3、设置view的Frame会触发layoutSubviews,前提是frame的值设置前后发生了变化(view的with,height发现变化才会触发layoutSubviews,original. x ,original. y变化不会触发layoutSubviews
公共代码test3

-(void)test3{

    _largeView = [[TestView allocinit];

    _largeView.frame = CGRectMake(005050);

    [_largeView setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:_largeView];

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f

                                              target:self

                                            selector:@selector(timerEventX:)

                                            userInfo:nil

                                            repeats:YES];

}

case1:frame{0,0,50,50}变化为frame{random,random,random,random}
timerEvent1:

- (void)timerEvent1:(id)sender

{

   _smallView.frame = CGRectMake(arc4random()%100 + 20,

                                  arc4random()%100 + 20,

                                  arc4random()%100 + 20,

                                  arc4random()%100 + 20);

}

结果:frame发生变化,且包括oringal,width,height,方法layoutSubviews被调用


case2:{0,0,50,50}变化为frame{0,0,random,random}
timerEvent2:

- (void)timerEvent2:(id)sender

{

   _smallView.frame = CGRectMake(0,

                                  0,

                                  arc4random()%100 + 20,

                                  arc4random()%100 + 20);

}

结果:frame的oringal不变,width,height变化,方法layoutSubviews被调用


case3:{0,0,50,50}变化为frame{random ,random ,0,0}
timerEvent3:

- (void)timerEvent3:(id)sender

{

   _smallView.frame = CGRectMake(arc4random()%100 + 20,

                                  arc4random()%100 + 20

                                  50,

                                  50);

}

结果:frame的oringal变化,width,height不变,方法layoutSubviews不被调用




对于测试如何了解layoutSubviews调用时机,可以较好掌握uiview显示的机理,有利于测试过程

好这里总结下

1、init初始化不会触发layoutSubviews

2、addSubview会触发layoutSubviews(但frame!={0,0,0,0})

3、设置view的Frame会触发layoutSubviews,前提是frame的值设置前后发生了变化(view的with,height发现变化才会触发layoutSubviews,original. x ,original. y变化不会触发layoutSubviews

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值