###關于IOS開發(fā)

CALayer

1.官方解釋

The CALayer class manages image-based content(內容) and allows you to perform animations on that content. Layers are often used to provide the backing store(存儲器) for views but can also be used without a view to display content. A layer’s main job is to manage the visual content that you provide ++主要目的就是管理那些可見的內容++but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry(幾何) of its content (such as its position, size, and transform(改變) that is used to present that content on screen. Modifying the properties of the layer is how you initiate animations on the layer’s content or geometry. A layer object encapsulates(囊括) the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines the layer’s timing information.
If the layer object was created by a view, the view typically assigns itself as the layer’s delegate automatically, and you should not change that relationship. For layers you create yourself, you can assign(指定) a delegate object and use that object to provide the contents of the layer dynamically and perform other tasks. A layer may also have a layout manager object (assigned to the layoutManager property) to manage the layout of subviews separately.

2.自己的理解

  • 1.CALayer(Core Animation Layer),layer是層的意思听怕,所以這是一個核心動畫的層,之所以被稱作是核心動畫尾膊,- 2.CALayer包含在QuartzCore框架中欲逃,這是一個跨平臺的框架秤涩,既可以用在iOS中又可以用在Mac OS X中熬粗。在使用Core Animation開發(fā)動畫的本質就是將CALayer中的內容轉化為位圖從而供硬件操作虱朵;
  • 3.每一個UIView都有一個root layer布持,它是由系統(tǒng)創(chuàng)建的豌拙,并且self.view.layer是一個只讀的屬性,也就是說你無法更換這個系統(tǒng)自己創(chuàng)建的layer题暖,你只可以給他增加一個sublayer按傅,然后去對sublayer做各種事情捉超,一個view之所以能夠顯示東西,就是因為他有l(wèi)ayer逞敷;
  • 4.當利用drawRect:方法繪圖的本質就是繪制到了UIView的layer(屬性)中狂秦,但是在Core Animation中我們操作更多的則不再是UIView而是直接面對CALayer;
  • 5.CALayer和UIView的關系如下圖推捐,在UIView中有一個layer屬性作為根圖層裂问,根圖層上可以放其他子圖層,在UIView中所有能夠看到的內容都包含在layer中牛柒;
    圖:(img)
  • 6.在iOS中CALayer的設計主要是了為了內容展示和動畫操作堪簿,CALayer本身并不包含在UIKit中,它不能響應事件皮壁。由于CALayer在設計之初就考慮它的動畫操作功能椭更,CALayer很多屬性在修改時都能形成動畫效果,這種屬性稱為“隱式動畫屬性”蛾魄。但是對于UIView的根圖層而言屬性的修改并不形成動畫效果虑瀑,因為很多情況下根圖層更多的充當容器的做用,如果它的屬性變動形成動畫效果會直接影響子圖層滴须。

3.layer常用屬性的理解和使用詳解

(img)

(img)

部分屬性的詳細解釋如下:

1.@property id contents:

An object that provides the contents of the layer舌狗,The default value of this property is nil.     If you are using the layer to display a static image, you can set this property to the CGImageRef containing the image you want to display. (In OS X 10.6 and later, you can also set the property to an a href="" NSImage /a object.) Assigning a value to this property causes the layer to use your image rather than create a separate backing store.

'' id image = (id)[UIImage imageNamed:@"阿貍頭像"].CGImage;
'' self.view.layer.contents = image;

**If the layer object is tied to a view object, you should avoid setting the contents of this property directly.** The interplay between views and layers usually results in the view replacing the contents of this property during a subsequent update.

自己的理解:這個屬性的類型被定義為id,意味著它可以是任何類型的對象扔水。在這種情況下痛侍,你可以給contents屬性賦任何值,你的app仍然能夠編譯通過魔市。但是主届,在實踐中,如果你給contents賦的不是CGImage待德,那么你得到的圖層將是空白的君丁。
contents這個奇怪的表現(xiàn)是由Mac OS的歷史原因造成的。它之所以被定義為id類型将宪,是因為在Mac OS系統(tǒng)上绘闷,這個屬性對CGImage和NSImage類型的值都起作用。如果你試圖在iOS平臺上將UIImage的值賦給它涧偷,只能得到一個空白的圖層。一些初識Core Animation的iOS開發(fā)者可能會對這個感到困惑毙死。
頭疼的不僅僅是我們剛才提到的這個問題燎潮。事實上,你真正要賦值的類型應該是CGImageRef扼倘,它是一個指向CGImage結構的指針确封。UIImage有一個CGImage屬性除呵,它返回一個"CGImageRef",如果你想把這個值直接賦值給CALayer的contents,那你將會得到一個編譯錯誤爪喘。因為CGImageRef并不是一個真正的Cocoa對象颜曾,而是一個Core Foundation類型。
盡管Core Foundation類型跟Cocoa對象在運行時貌似很像(被稱作toll-free bridging)秉剑,它們并不是類型兼容的泛豪,不過你可以通過bridged關鍵字轉換。如果要給圖層的寄宿圖賦值侦鹏,你可以按照以下這個方法:
layer.contents = (_bridge id)image.CGImage;

2.@property CGRect contentsRect

The rectangle, in the unit coordinate space, that defines the portion of the layer’s contents that should be used. Animatable.
ex:
效果圖1:(img)
修改 contentrect屬性:

''UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
''view1.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);//我只想看到你y方向的一半诡曙,從0那個位置開始
''view1.image = [UIImage imageNamed:@"阿貍頭像"];
''[self.view addSubview:view1];
效果圖2:(img)
若對rect進行修改為(0,0.5,1,0.5)
''view1.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
效果圖3:(img)
Defaults to the unit rectangle (0.0, 0.0, 1.0, 1.0). If pixels outside the unit rectangle are requested, the edge pixels of the contents image will be extended outwards.(如果在矩形單元之外,仍然要求顯示像素略水,那么圖片的邊緣像素就會被拉伸)
''view1.layer.contentsRect = CGRectMake(0, 0, 2, 2);//我想要看到你這個圖片兩倍的面積那么大价卤,結果會對邊界進行像素的拉伸,基本上就毀掉了
效果圖4:(img)

3.anchorPoint+position+bounds = frame

We usually use the anchorPoint with the property “position” or we make a rotation animation,the anchorPoint is the center on point,and the layer will rotation around the point.And when we creat a new layer without set the frame,we need to set the bounds+position+anchorPoint,we get size from bounds,and get the position from “position”+”anchorPiont”,the position is in super layer coordinate system,and the anchorPoint is in the created layer itself,we make the two points superposition(重合),this is how we confirm the created layer’s position.
When we use a frame to specified a rectangle,it is supposed as (100, 100, 200, 200),it means the “position” is (100, 100),the anchorPoint is (0, 0),warning,when the layer is rotating,the center point is (0.5, 0.5),cause the default anchorPoint is(0.5, 0.5),this is for scale;
The main difference between frame and another three properties is we cannot get invisible animation with frame,so we usually use bounds+position+anchorPoint instead.


相同的position渊涝,不同的anchorPoint慎璧,不同的位置(img)

實例:
使用frame
''CALayer *layer = [CALayer layer];
''layer.backgroundColor = [UIColor redColor].CGColor;
''layer.frame = CGRectMake(0, 0, 100, 100);
''[self.view.layer addSublayer:layer];
效果圖1:(img)
使用position+anchorPoint+bounds
''CALayer *layer = [CALayer layer];
''layer.backgroundColor = [UIColor redColor].CGColor;
''//利用這三個屬性來定義一個layer的位置
''layer.position = CGPointMake(0, 0);
''layer.bounds = CGRectMake(0, 0, 100, 100);
''layer.anchorPoint = CGPointMake(0, 0);
''
''[self.view.layer addSublayer:layer];
效果圖2:(img)
如果不設置anchorPoint,那么這個值默認就是(0.5, 0.5),就會只顯示1/4.

4.shadow

''CALayer *layer = [CALayer layer];
''layer.backgroundColor = [UIColor redColor].CGColor;
''layer.frame = CGRectMake(0, 0, 100, 100);
''layer.shadowColor = [UIColor blueColor].CGColor;
''layer.shadowOffset = CGSizeMake(10, 1);//向右偏移10跨释,向下偏移1
''layer.shadowOpacity = 1;
''//layer.shadowPath 默認返回一個跟他一模一樣的東西
''layer.shadowRadius = 10;
''[self.view.layer addSublayer:layer];
效果圖:(img)

5.mask and maskToBounds


mask:
An optional layer whose alpha channel is used to mask the layer’s content.
The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content.
The default value of this property is nil nil. When configuring a mask, remember to set the size and position of the mask layer to ensure it is aligned properly with the layer it masks.
首先胸私,mask是蒙板的意思,它的實質是一個layer煤傍,我們使用的是layer的frame盖文,也就是他的位置和形狀,用它的位置和形狀來創(chuàng)建一個可見通道蚯姆,當你給一個添加了mask五续,那么只有在這個mask之下的部分才能被看到。默認情況下layer是沒有蒙板的龄恋,當你設置一個蒙板的時候疙驾,一定要設置尺寸和位置,要不然你啥也看不到郭毕。
關于mask的位置它碎,mask相當于是layer的sublayer,那么它的位置是相對于layer的显押。
通過兩個layer的組合扳肛,我們可以創(chuàng)造出一些比較奇怪的形狀。
實例:
''CALayer *layer = [CALayer layer];
''layer.backgroundColor = [UIColor blueColor].CGColor;
''layer.frame = CGRectMake(100, 100, 100, 100);
'' //mask蒙板
''CALayer *mask = [[CALayer alloc] init];
''mask.backgroundColor = [UIColor blackColor].CGColor;
''mask.frame = CGRectMake(0, 0, 50, 50);
''mask.cornerRadius = mask.frame.size.width/2;
''layer.mask = mask;
''[self.view.layer addSublayer:layer];
給layer添加一個蒙板乘碑,我們看到的是蒙板后邊的東西就(img)
效果圖:(img)


maskToBounds:
A Boolean indicating whether sublayers are clipped to the layer’s bounds.
When the value of this property is YES, Core Animation creates an implicit clipping mask that matches the bounds of the layer and includes any corner radius effects. If a value for the mask property is also specified, the two masks are multiplied to get the final mask value.
The default value of this property is NO.
理解這個屬性要抓住兩個要點:第一個是它會創(chuàng)建一個mask挖息,這個mask跟layer一模一樣,第二個是兽肤,這個mask只對layer本身的sublayers起作用套腹。
實例:
''CALayer *layer = [CALayer layer];
''layer.backgroundColor = [UIColor blueColor].CGColor;
''layer.frame = CGRectMake(100, 100, 100, 100);
''CALayer *layer1 = [CALayer layer];
''layer1.backgroundColor = [UIColor redColor].CGColor;
''layer1.frame = CGRectMake(50, 0, 100, 100);
''layer1.cornerRadius = layer1.frame.size.width/2;
''//把layer1變成layer的sublayer绪抛,這樣masktobounds就可以對他起作用
''[layer addSublayer:layer1];
''layer.masksToBounds = YES;
''[self.view.layer addSublayer:layer];
效果圖:(img)
稍作更改,layer1.frame = CGRectMake(0, 0, 100, 100);
效果圖:(img)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末电禀,一起剝皮案震驚了整個濱河市幢码,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尖飞,老刑警劉巖症副,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異葫松,居然都是意外死亡瓦糕,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門腋么,熙熙樓的掌柜王于貴愁眉苦臉地迎上來咕娄,“玉大人,你說我怎么就攤上這事珊擂∈ダ眨” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵摧扇,是天一觀的道長圣贸。 經常有香客問我,道長扛稽,這世上最難降的妖魔是什么吁峻? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮在张,結果婚禮上用含,老公的妹妹穿的比我還像新娘。我一直安慰自己帮匾,他們只是感情好啄骇,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瘟斜,像睡著了一般缸夹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上螺句,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天虽惭,我揣著相機與錄音,去河邊找鬼蛇尚。 笑死芽唇,一個胖子當著我的面吹牛,可吹牛的內容都是我干的佣蓉。 我是一名探鬼主播披摄,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼勇凭!你這毒婦竟也來了疚膊?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤虾标,失蹤者是張志新(化名)和其女友劉穎寓盗,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體璧函,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡傀蚌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蘸吓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片善炫。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖库继,靈堂內的尸體忽然破棺而出箩艺,到底是詐尸還是另有隱情,我是刑警寧澤宪萄,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布艺谆,位于F島的核電站,受9級特大地震影響拜英,放射性物質發(fā)生泄漏静汤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一居凶、第九天 我趴在偏房一處隱蔽的房頂上張望虫给。 院中可真熱鬧,春花似錦排监、人聲如沸狰右。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽棋蚌。三九已至,卻和暖如春挨队,著一層夾襖步出監(jiān)牢的瞬間谷暮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工盛垦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留湿弦,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓腾夯,卻偏偏與公主長得像颊埃,于是被迫代替她去往敵國和親蔬充。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內容

  • 轉載:http://www.reibang.com/p/32fcadd12108 每個UIView有一個伙伴稱為l...
    F麥子閱讀 6,200評論 0 13
  • 在iOS中隨處都可以看到絢麗的動畫效果班利,實現(xiàn)這些動畫的過程并不復雜饥漫,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,488評論 6 30
  • @interface NSObject (QMUI) /**對 super 發(fā)送消息 @param aSelect...
    蕭城x閱讀 469評論 0 0
  • 親愛的葉子老師: 這是我第二次給您寫信罗标。上一次寫信給您庸队,我還是個懶惰而迷茫的高中生,等待您的鼓勵和指引闯割。而...
    S蘇蘇S閱讀 510評論 1 3
  • 可曾對宇宙的神秘感到恐懼 你不該 是長在一顆頑石上的刺 不要去指責夜色的虛偽 黑色也并不能代表火焰 波濤洶涌的是昨...
    寡小城閱讀 1,732評論 0 6