UIView

1、setNeedsDisplay和setNeedsLayout 

首先兩個方法都是異步執行的。而setNeedsDisplay會自動調用drawRect方法,這樣可以拿到  UIGraphicsGetCurrentContext,就可以畫畫了。setNeedsLayout會默認調用layoutSubViews

 就可以  處理子視圖中的一些數據。

綜上所訴,setNeedsDisplay方便繪圖,而layoutSubViews方便出來數據

layoutSubviews在以下情況下會被調用:

1init初始化不會觸發layoutSubviews

2addSubview會觸發layoutSubviews

3、設置viewFrame會觸發layoutSubviews,當然前提是frame的值設置前後發生了變化。

4、滾動一個UIScrollView會觸發layoutSubviews

5、旋轉Screen會觸發父UIView上的layoutSubviews事件。

6、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件。

7、直接調用setLayoutSubviews

 

drawRect在以下情況下會被調用:

  1、如果在UIView初始化時沒有設置rect大小,將直接導致drawRect不被自動調用。drawRect調用是在Controller->loadView, Controller->viewDidLoad 兩方法之後掉用的.所以不用擔心在控制器中,這些ViewdrawRect就開始畫了.這樣可以在控制器中設置一些值給View(如果這些View draw的時候需要用到某些變量值).

2該方法在調用sizeToFit後被調用,所以可以先調用sizeToFit計算出size。然後系統自動調用drawRect:方法。

3、通過設置contentMode屬性值爲UIViewContentModeRedraw。那麼將在每次設置或更改frame的時候自動調用drawRect:

4、直接調用setNeedsDisplay,或者setNeedsDisplayInRect:觸發drawRect:,但是有個前提條件是rect不能爲0

以上1,2推薦;而3,4不提倡

 

drawRect方法使用注意點:

1、若使用UIView繪圖,只能在drawRect:方法中獲取相應的contextRef並繪圖。如果在其他方法中獲取將獲取到一個invalidateref並且不能用於畫圖。drawRect:方法不能手動顯示調用,必須通過調用setNeedsDisplay 或者 setNeedsDisplayInRect,讓系統自動調該方法

2若使用calayer繪圖,只能在drawInContext: 中(類似於drawRect)繪製,或者在delegate中的相應方法繪製。同樣也是調用setNeedDisplay等間接調用以上方法

3、若要實時畫圖,不能使用gestureRecognizer,只能使用touchbegan等方法來掉用setNeedsDisplay實時刷新屏幕


2、解析LayoutSubviews

從百度上搜索了一下layoutSubviews的用處,以下是搜索的結果,當然,筆者是會一一驗證的.

1、init初始化不會觸發layoutSubviews

2、addSubview會觸發layoutSubviews

3、設置view的Frame會觸發layoutSubviews,當然前提是frame的值設置前後發生了變化

4、滾動一個UIScrollView會觸發layoutSubviews

5、旋轉Screen會觸發父UIView上的layoutSubviews事件

6、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件

在開始驗證之前,先看看layoutSubviews到底是啥來着:)

Lays out subviews.
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.

在iOS5.1或之前的版本中,這個方法什麼也沒幹.這個方法的默認實現是用參數來設定subviews的尺寸和位置的.

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

如果你需要更加精確的佈局,可以在子類裏面重寫這個方法.僅僅在以下情況下:自動佈局達不到你想要效果時你纔有必要重寫這個方法.你可以直接設置subviews的尺寸.

You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.

你不能直接調用這個方法.如果你需要強制layout刷新,調用setNeedsLayout來代替.如果你想要立即刷新你的view,調用layoutIfNeeded

大概總結以下就是:

你不要直接調用方法layoutSubviews,如果想要刷新,請調用setNeedsLayout或者layoutIfNeeded

好了,開始驗證:)

現在提供繼承至UIView的類如下:

//
//  TestView.h
//  LayoutSubviews
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TestView : UIView

@end

//
//  TestView.m
//  LayoutSubviews
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "TestView.h"

@implementation TestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSLog(@"initWithFrame:%@" ,NSStringFromCGRect(frame));
    }
    return self;
}

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews %@", self);
    [super layoutSubviews];
}

@end

//
//  RootViewController.m
//  LayoutSubviews
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "TestView.h"

@interface RootViewController ()

@property (nonatomic, strong) NSTimer   *timer;
@property (nonatomic, strong) TestView  *largeView;
@property (nonatomic, strong) TestView  *smallView;

@end

@implementation RootViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    // 1、init初始化不會觸發layoutSubviews [正確的]
    // 2、addSubview會觸發layoutSubviews [不完全正確,當frame爲0時是不會觸發的]
    // 3、設置view的Frame會觸發layoutSubviews,當然前提是frame的值設置前後發生了變化 [正確]
    
//    [self test_1];
//    [self test_2];
//    [self test_3];
    
    // 4、滾動一個UIScrollView會觸發layoutSubviews[錯誤,不用滾動就會觸發]
//    [self test_4];
    
    // 5、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件
    [self test_5];
}


- (void)test_1
{
    /* 
     解釋:
     
     走了initWithFrame:方法,但是又有frame值爲{{0, 0}, {0, 0}},並不需要繪製任何的東西,
     所以即使添加了test,也沒必要繪製它,同時也驗證了addSubview會觸發layoutSubviews是錯
     誤的,只有當被添加的view有着尺寸的時候纔會觸發layoutSubviews
     */
    
    TestView *test = [TestView new];
    [self.view addSubview:test];
}

- (void)test_2
{
    TestView *test = [TestView new];
    test.frame = CGRectMake(0, 0, 100, 100);
    [self.view addSubview:test];
}

- (void)test_3
{
    /*
     解釋:
     
     layoutSubviews這個方法自身無法調用,是被父類添加的時候才執行的方法
     */
    
    TestView *test = [TestView new];
    test.frame = CGRectMake(0, 0, 50, 50);
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [test addSubview:showView];
}

- (void)test_4
{
    CGRect rect    = self.view.bounds;
    CGFloat height = rect.size.height;
    CGFloat width  = rect.size.width;
    
    UIScrollView *rootScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    NSArray *data            = @[@"", @"", @"", @""];
    [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        TestView *tmp        = [[TestView alloc] initWithFrame:CGRectMake(width*idx, 0,
                                                                          width, height)];
        [rootScroll addSubview:tmp];
    }];
    rootScroll.contentSize   = CGSizeMake(width * data.count, height);
    [self.view addSubview:rootScroll];
}

- (void)test_5
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f
                                              target:self
                                            selector:@selector(timerEvent:)
                                            userInfo:nil
                                             repeats:YES];
    _largeView = [[TestView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_largeView];
    
    _smallView = [[TestView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [_largeView addSubview:_smallView];
}

- (void)timerEvent:(id)sender
{
    _smallView.frame = CGRectMake(arc4random()%100 + 20,
                                  arc4random()%100 + 20,
                                  arc4random()%100 + 20,
                                  arc4random()%100 + 20);
    NSLog(@"_smallView %@", _smallView);
    NSLog(@"_smallView %@", _largeView);
}

@end

測試後的結論是這樣子的:

1. 一個view是不能夠自己調用layoutSubviews,如果要調用,需要調用setNeedsLayout或者layoutIfNeeded

2. 如果view的frame值爲0,即使被添加了耶不會調用layoutSubviews

3. 如果一個view的frame值改變了,那麼它的父類的layoutSubviews也會被執行


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章