你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 處置視圖加載的復雜性

iOS 處置視圖加載的復雜性

編輯:IOS開發綜合

UITableView

看看我們的復雜形態量有哪些

init 辦法 updateConstrains 辦法 drawRect 辦法 setNeedsDisplay
breakpoint1 - (instancetype)init {};
breakpoint2 - (void)setNeedsDisplay {};
breakpoint3 - (void)updateConstraints {};
breakpoint4 - (void)drawRect:(CGRect)rect {};

依據我的測試 調用棧是這樣的
cell1 init updateConstrains setNeedsDisplay drawRect

2017-01-18 22:54:52.221624 RecordLife[6006:1468686] setNeedsDisplay
2017-01-18 22:54:52.221753 RecordLife[6006:1468686] setNeedsDisplay
2017-01-18 22:54:52.221805 RecordLife[6006:1468686] setNeedsDisplay
2017-01-18 22:54:52.225492 RecordLife[6006:1468686] updateConstraints
2017-01-18 22:54:52.229674 RecordLife[6006:1468686] updateConstraints
2017-01-18 22:54:52.237403 RecordLife[6006:1468686] layoutSubviews
2017-01-18 22:54:52.259127 RecordLife[6006:1468686] drawRect

一些規劃相關的辦法

//layoutIfNeeded 強迫提早在 drawRect 前規劃
// Allows you to perform layout before the draWing cycle happens. -layoutIfNeeded forces layout early
- (void)setNeedsLayout;
- (void)layoutIfNeeded;

// IOS 6.0 layoutIfNeeded 會自動調用 layoutSubviews
- (void)layoutSubviews;    // override point. called by layoutIfNeeded automatically. As of IOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.
//在傳入的矩形中繪制接納器的圖像。
//此辦法的默許完成不執行任何操作。運用Core Graphics和UIKit等技術繪制視圖內容的子類應該重寫這個辦法並在那裡完成它們的繪圖代碼。假如您的視圖以其他方式設置其內容,則不需求掩蓋此辦法。例如,假如視圖僅顯示背景顏色,或許您的視圖運用底層對象直接設置其內容,則不需求重寫此辦法。
//在調用此辦法時,UIKit已為您的視圖正確配置了繪圖環境,您可以復雜地調用任何繪制辦法和函數來渲染內容。詳細來說,UIKit創立和配置圖形上下文以繪制和調整該上下文的變換,使其原點與視圖邊界矩形的原點相婚配。您可以運用UIGraphicsGetCurrentContext函數獲取對圖形上下文的援用,但不樹立對圖形上下文的強援用,由於它可以在調用drawRect:辦法之間更改。
//相似地,假如運用OpenGL ES和GLKView類來繪制,GLKit在調用此辦法(或GLKView委托的glkView:draWinRect:辦法)之前,為您的視圖適外地配置底層OpenGL ES上下文,因而您可以復雜地收回任何OpenGL ES命令您需求出現您的內容。有關如何運用OpenGL ES繪制的詳細信息,請參閱適用於IOS的OpenGL ES編程指南。
//您應該將任何繪圖限制為rect參數中指定的矩形。此外,假如視圖的opaque屬性設置為YES,您的drawRect:辦法必需運用不通明內容完全填充指定的矩形。
//假如你直接子類化UIView,你的這個辦法的完成不需求調用super。但是,假如你是一個不同的視圖類的子類,你應該在完成的某個點調用super。
//當初次顯示視圖時或當發作使視圖的可見局部有效的事情時,將調用此辦法。你不應該直接自己調用這個辦法。要使視圖的一局部有效,從而使該局部重繪,請調用setNeedsDisplay或setNeedsDisplayInRect:辦法。
- (void)drawRect:(CGRect)rect;
// 告訴零碎,您的視圖內容需求重繪,此辦法記下懇求並立刻前往。 在下一個繪制周期之前,實踐上不會重繪視圖,此時一切有效視圖都會更新。
- (void)setNeedsDisplay;
// 特定的矩形范圍內重繪
- (void)setNeedsDisplayInRect:(CGRect)rect;
父子視圖調用

需求留意的是 updateConstrains 是從 子視圖 開端 父視圖 完畢
view

subview

察看 View frame

There are usually notifications or other observable events where KVO isn’t supported. Even though the docs says ‘no’, it is ostensibly safe to observe the CALayer backing the UIView. Observing the CALayer works in practice because of its extensive use of KVO and proper Accessors (instead of ivar manipulation). It’s not guaranteed to work going forward.

Anyway, the view’s frame is just the product of other properties. Therefore we need to observe those:

[self.view addObserver:self forKeyPath:@"frame" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"transform" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"position" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"zPosition" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"anchorPoint" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"anchorPointZ" options:0 context:NULL];
[self.view.layer addObserver:self forKeyPath:@"frame" options:0 context:NULL];

See full example here https://gist.github.com/hfossli/7234623

NOTE: This is not said to be supported in the docs, but it works as of today with all iOS versions this far (currently iOS 2 -> iOS 10)

With ReactiveCocoa you can do

RACSignal *signal = [RACSignal merge:@[
  RACObserve(view, frame),
  RACObserve(view, layer.bounds),
  RACObserve(view, layer.transform),
  RACObserve(view, layer.position),
  RACObserve(view, layer.zPosition),
  RACObserve(view, layer.anchorPoint),
  RACObserve(view, layer.anchorPointZ),
  RACObserve(view, layer.frame),
  ]];

[signal subscribeNext:^(id x) {
    NSLog(@"View probably changed its geometry");
}];

And if you only want to know when bounds changes you can do

@weakify(view);
RACSignal *boundsChanged = [[signal map:^id(id value) {
    @strongify(view);
    return [NSValue valueWithCGRect:view.bounds];
}] distinctUntilChanged];

[boundsChanged subscribeNext:^(id ignore) {
    NSLog(@"View bounds changed its geometry");
}];

And if you only want to know when frame changes you can do

@weakify(view);
RACSignal *frameChanged = [[signal map:^id(id value) {
    @strongify(view);
    return [NSValue valueWithCGRect:view.frame];
}] distinctUntilChanged];

[frameChanged subscribeNext:^(id ignore) {
    NSLog(@"View frame changed its geometry");
}];

【iOS 處置視圖加載的復雜性】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved