Core Animation筆記(- Layer 基本屬性)

一.Layer的基本屬性

1. contents 圖層內容默認爲nil 能夠指定一張圖片做爲內容展現緩存

 self.layerView.layer.contents = (__bridge id)imag.CGImage;

2. contentsGravity 相似於contentMode的效果,app

如kCAGravityCenter居中不拉伸,iview

  kCAGravityResize,自動縮放.ui

3. contentsScale 內容縮放因子,默認爲1,通常都設置爲屏幕的縮放spa

  contensRect:內容顯示的區域代理

  self.layerView.layer.contentsScale = [UIScreen mainScreen].scale;

 contensCenter:拉伸區域rest

4. masksToBounds 超出圖層邊界的內容是否顯示 true:不顯示code

    mask:圖層輪廓,mak layer的實心部分會被保留下來blog

5.陰影事件

  //shadow
    layer1.shadowOpacity = 0.5;
    layer1.shadowOffset = CGSizeMake(0, 4);
    layer1.shadowRadius = 3;
    layer1.shadowColor = [UIColor yellowColor].CGColor;
    CGMutablePathRef circlePath = CGPathCreateMutable();
    CGPathAddEllipseInRect(circlePath, NULL, layer1.bounds);
    //陰影路徑
    layer1.shadowPath = circlePath;
    CFRelease(circlePath);

6.錨點 anchorPoint 默認爲0.5 0.5

7.magnificationFilter(放大率過濾) 看下官方文檔的解釋: The circle on the left uses kCAFilterLinear and the circle on the right uses kCAFilterNearest.

  shows the difference between linear and nearest filtering when a 10 x 10 point image of a circle is magnified by a scale of 10.

8.光柵化   // 當shouldRasterize設成true時,開啓shouldRasterize後,CALayer會被光柵化爲bitmap,layer的陰影等效果也會被緩存到bitmap中,等下次使用時不會再從新去渲染了。實現圓角自己就是在作顏色混合(blending),若是每次頁面出來時都blending,消耗太大,這時shouldRasterize = yes,下次就只是簡單的從渲染引擎的cache裏讀取那張bitmap,節約系統資源

    btn2.layer.shouldRasterize = true;
    btn2.layer.rasterizationScale = [UIScreen mainScreen].scale;

9.   cornerRadius 圓角 

  borderColor 邊框顏色

  borderWidth 邊框寬度

10layer 代理

//只有單獨使用layer的時候纔有可能須要實現代理繪畫,若是是uiview,layer代理是其自己,

//實現drawRect方法

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;

11. geometryFlipped 圖層翻轉 默認爲no 若是設置爲yes,則子圖層相對於父圖層底部排版而不是頂部

12. -hitTest: 返回觸摸範圍內的layer 注:r若是改變了layer.zPosition 可能沒法獲取正確的觸摸圖層(沒法改變觸摸事件傳遞順序,雖然改變zPosition使圖層視覺上在某個圖層之上,但這個圖層實際上是先於另外一圖層添加,則可能被另外一圖層遮擋)

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint p = [[touches anyObject] locationInView:self.view];
    CALayer *laer = [self.view.layer hitTest:p];
}
相關文章
相關標籤/搜索