ios 淺談UIView的幾個(gè)方法:layoutSubviews、layoutIfNeeded、setNeedsLayout...

在用xib中的約束做一些動(dòng)畫時(shí),相信很多小伙伴都遇到不少坑,下面總結(jié)下最近自己遇到的一些坑,歡迎各位大神糾正以及指教:

layoutSubviews

該方法默認(rèn)沒人做任何事,需要自己重寫,有很多地方系統(tǒng)會(huì)自動(dòng)調(diào)用這個(gè)方法:

  • 初始化時(shí)不會(huì)調(diào)用layoutSubviews,如果設(shè)置了非CGRectZero的frame就會(huì)調(diào)用
  • 調(diào)用addSubView時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用layoutSubviews
  • 當(dāng)view的frame發(fā)生變化時(shí),會(huì)調(diào)用layoutSubviews
  • UIScrollView滾動(dòng)的時(shí)候回調(diào)用layoutSubviews

setNeedsLayout

標(biāo)記view需要重新布局,但是不會(huì)立刻刷新,系統(tǒng)會(huì)自動(dòng)調(diào)用layoutSubviews,然后自行調(diào)用layoutIfNedded后,立刻刷新。

layoutIfNeeded

在有需要刷新的地方調(diào)用此方法

下面是幾個(gè)例子:

  • 例1:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *blueViewContant;
@end

- (IBAction)btnEvent:(id)sender {
        [UIView animateWithDuration:3 animations:^{
        self.blueViewContant.constant = 300;
    }];
}

動(dòng)畫效果:

blueTest.gif

你會(huì)發(fā)現(xiàn)并沒有什么卵用,而且制作gif的軟件也有點(diǎn)辣雞,壓根沒有這個(gè)動(dòng)畫過程,下面我試下用layoutIfNeeded方法

  • 例2:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *blueViewContant;
@end
- (IBAction)btnEvent:(id)sender {
        [UIView animateWithDuration:3 animations:^{
        self.blueViewContant.constant = 300;
        [self.view layoutIfNeeded];
    }];
    self.yellowViewContant.constant = 300;
}

blueTest.gif

這次效果就很明顯了,就是說如果用xib中的Constraint來設(shè)置動(dòng)畫,一定要在開始動(dòng)畫的地方調(diào)用layoutIfNeeded方法,告訴系統(tǒng)這時(shí)候開始刷新

  • 例3
@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *blueViewContant;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *yellowViewContant;
@end
- (IBAction)btnEvent:(id)sender {
        [UIView animateWithDuration:3 animations:^{
        self.blueViewContant.constant = 300;
        [self.view layoutIfNeeded];
    }];
    self.yellowViewContant.constant = 300;
}
blueTest.gif

總結(jié):如果想要用Constraint設(shè)置動(dòng)畫,一定要在調(diào)用layoutIfNeeded方面前設(shè)置Constraint的變化,如果不想view以動(dòng)畫的形式改變,那么久在調(diào)用layoutIfNeeded之后設(shè)置Constraint,或者不調(diào)用layoutIfNeeded.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容