[iOS Animation]-CALayer 圖層幾何學二

座標系

和視圖同樣,圖層在圖層樹當中也是相對於父圖層按層級關係放置,一個圖層的position依賴於它父圖層的bounds,若是父圖層發生了移動,它的全部子圖層也會跟着移動。 git

這樣對於放置圖層會更加方便,由於你能夠經過移動根圖層來將它的子圖層做爲一個總體來移動,可是有時候你須要知道一個圖層的絕對位置,或者是相對於另外一個圖層的位置,而不是它當前父圖層的位置。 github

CALayer給不一樣座標系之間的圖層轉換提供了一些工具類方法: 算法

1
2
3
4
- (CGPoint)convertPoint:(CGPoint)point fromLayer:(CALayer *)layer;
- (CGPoint)convertPoint:(CGPoint)point toLayer:(CALayer *)layer;
- (CGRect)convertRect:(CGRect)rect fromLayer:(CALayer *)layer;
- (CGRect)convertRect:(CGRect)rect toLayer:(CALayer *)layer;

這些方法能夠把定義在一個圖層座標系下的點或者矩形轉換成另外一個圖層座標系下的點或者矩形 ide

翻轉的幾何結構

常規說來,在iOS上,一個圖層的position位於父圖層的左上角,可是在Mac OS上,一般是位於左下角。Core Animation能夠經過geometryFlipped屬性來適配這兩種狀況,它決定了一個圖層的座標是否相對於父圖層垂直翻轉,是一個BOOL類型。在iOS上經過設置它爲YES意味着它的子圖層將會被垂直翻轉,也就是將會沿着底部排版而不是一般的頂部(它的全部子圖層也同理,除非把它們的geometryFlipped屬性也設爲YES)。 函數

Z座標軸

和UIView嚴格的二維座標系不一樣,CALayer存在於一個三維空間當中。除了咱們已經討論過的position和anchorPoint屬性以外,CALayer還有另外兩個屬性,zPosition和anchorPointZ,兩者都是在Z軸上描述圖層位置的浮點類型。 工具

注意這裏並無更的屬性來描述由寬和高作成的bounds了,圖層是一個徹底扁平的對象,你能夠把它們想象成相似於一頁二維的堅硬的紙片,用膠水粘成一個空洞,就像三維結構的摺紙同樣。 佈局

zPosition屬性在大多數狀況下其實並不經常使用。在第五章,咱們將會涉及CATransform3D,你會知道如何在三維空間移動和旋轉圖層,除了作變換以外,zPosition最實用的功能就是改變圖層的顯示順序了。 測試

一般,圖層是根據它們子圖層的sublayers出現的順序來類繪製的,這就是所謂的畫家的算法--就像一個畫家在牆上做畫--後被繪製上的圖層將會遮蓋住以前的圖層,可是經過增長圖層的zPosition,就能夠把圖層向相機方向前置,因而它就在全部其餘圖層的前面了(或者至少是小於它的zPosition值的圖層的前面)。 ui

這裏所謂的「相機」其實是相對於用戶是視角,這裏和iPhone背後的內置相機沒任何關係。 atom

圖3.8顯示了在Interface Builder內的一對視圖,正如你所見,首先出如今視圖層級綠色的視圖被繪製在紅色視圖的後面。

圖3.8

圖3.8 在視圖層級中綠色視圖被繪製在紅色視圖的後面

咱們但願在真實的應用中也能顯示出繪圖的順序,一樣地,若是咱們提升綠色視圖的zPosition(清單3.3),咱們會發現順序就反了(圖3.9)。其實並不須要增長太多,視圖都很是地薄,因此給zPosition提升一個像素就可讓綠色視圖前置,固然0.1或者0.0001也可以作到,可是最好不要這樣,由於浮點類型四捨五入的計算可能會形成一些不便的麻煩。

清單3.3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@interface ViewController ()
 
@property (nonatomic, weak) IBOutlet UIView *greenView;
@property (nonatomic, weak) IBOutlet UIView *redView;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //move the green view zPosition nearer to the camera
    self.greenView.layer.zPosition = 1.0f;
}
@end

圖3.9

圖3.9 綠色視圖被繪製在紅色視圖的前面

Hit Testing

第一章「圖層樹」證明了最好使用圖層相關視圖,而不是建立獨立的圖層關係。其中一個緣由就是要處理額外複雜的觸摸事件。

CALayer並不關心任何響應鏈事件,因此不能直接處理觸摸事件或者手勢。可是它有一系列的方法幫你處理事件:-containsPoint:和-hitTest:。

-containsPoint:接受一個在本圖層座標系下的CGPoint,若是這個點在圖層frame範圍內就返回YES。如清單3.4所示第一章的項目的另外一個合適的版本,也就是使用-containsPoint:方法來判斷究竟是白色仍是藍色的圖層被觸摸了 (圖3.10)。這須要把觸摸座標轉換成每一個圖層座標系下的座標,結果很不方便。

清單3.4 使用containsPoint判斷被點擊的圖層

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@interface ViewController ()
 
@property (nonatomic, weak) IBOutlet UIView *layerView;
@property (nonatomic, weak) CALayer *blueLayer;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    //create sublayer
    self.blueLayer = [CALayer layer];
    self.blueLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
    self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    //add it to our view
    [self.layerView.layer addSublayer:self.blueLayer];
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //get touch position relative to main view
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //convert point to the white layer's coordinates
    point = [self.layerView.layer convertPoint:point fromLayer:self.view.layer];
    //get layer using containsPoint:
    if ([self.layerView.layer containsPoint:point]) {
        //convert point to blueLayer’s coordinates
        point = [self.blueLayer convertPoint:point fromLayer:self.layerView.layer];
        if ([self.blueLayer containsPoint:point]) {
            [[[UIAlertView alloc] initWithTitle:@"Inside Blue Layer"
                                        message:nil
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        } else {
            [[[UIAlertView alloc] initWithTitle:@"Inside White Layer"
                                        message:nil
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
    }
}
 
@end

圖3.10

圖3.10 點擊圖層被正確標識

-hitTest:方法一樣接受一個CGPoint類型參數,而不是BOOL類型,它返回圖層自己,或者包含這個座標點的葉子節點圖層。這意味着再也不須要像使用-containsPoint:那樣,人工地在每一個子圖層變換或者測試點擊的座標。若是這個點在最外面圖層的範圍以外,則返回nil。具體使用-hitTest:方法被點擊圖層的代碼如清單3.5所示。

清單3.5 使用hitTest判斷被點擊的圖層

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //get touch position
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //get touched layer
    CALayer *layer = [self.layerView.layer hitTest:point];
    //get layer using hitTest
    if (layer == self.blueLayer) {
        [[[UIAlertView alloc] initWithTitle:@"Inside Blue Layer"
                                    message:nil
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    } else if (layer == self.layerView.layer) {
        [[[UIAlertView alloc] initWithTitle:@"Inside White Layer"
                                    message:nil
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }
}

注意當調用圖層的-hitTest:方法時,測算的順序嚴格依賴於圖層樹當中的圖層順序(和UIView處理事件相似)。以前提到的zPosition屬性能夠明顯改變屏幕上圖層的順序,但不能改變事件傳遞的順序。

這意味着若是改變了圖層的z軸順序,你會發現將不可以檢測到最前方的視圖點擊事件,這是由於被另外一個圖層遮蓋住了,雖然它的zPosition值較小,可是在圖層樹中的順序靠前。咱們將在第五章詳細討論這個問題。

自動佈局

你可能用過UIViewAutoresizingMask類型的一些常量,應用於當父視圖改變尺寸的時候,相應UIView的frame也跟着更新的場景(一般用於橫豎屏切換)。

在iOS6中,蘋果介紹了自動排版機制,它和自動調整不一樣,而且更加複雜。

在Mac OS平臺,CALayer有一個叫作layoutManager的屬性能夠經過CALayoutManager協議和CAConstraintLayoutManager類來實現自動排版的機制。但因爲某些緣由,這在iOS上並不適用。

當使用視圖的時候,能夠充分利用UIView類接口暴露出來的UIViewAutoresizingMask和NSLayoutConstraintAPI,但若是想隨意控制CALayer的佈局,就須要手工操做。最簡單的方法就是使用CALayerDelegate以下函數:

1
- (void)layoutSublayersOfLayer:(CALayer *)layer;

當圖層的bounds發生改變,或者圖層的-setNeedsLayout方法被調用的時候,這個函數將會被執行。這使得你能夠手動地從新擺放或者從新調整子圖層的大小,可是不能像UIView的autoresizingMask和constraints屬性作到自適應屏幕旋轉。

這也是爲何最好使用視圖而不是單獨的圖層來構建應用程序的另外一個重要緣由之一。

總結

本章涉及了CALayer的集合結構,包括它的frame,position和bounds,介紹了三維空間內圖層的概念,以及如何在獨立的圖層內響應事件,最後簡單說明了在iOS平臺,Core Animation對自動調整和自動佈局支持的缺少。

相關文章
相關標籤/搜索