日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CoreAnimation编程指南(五)图层内容

發布時間:2024/7/23 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CoreAnimation编程指南(五)图层内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當我們使用Cocoa的視圖的時候,我們必須繼承NSView或者UIView并且重載函數drawRect:來顯示任何內容。但是CALayer實例可以直接使用,而無需繼承子類。因為CALayer是一個鍵-值編碼兼容的容器類,你可以在實例里面存儲任意值,所以子類實例化完全可以避免。
1.1 給CALayer提供內容
? 你可以通過以下任何一種方法指定CALayer實例的內容: ? (1)使用包含圖片內容的CGImageRef來顯式的設置圖層的contents的屬性。 ? (2)指定一個委托,它提供或者重繪內容。 ? (3)繼承CALayer類重載顯示的函數。
1.1.1 設置contents屬性
? 圖層的圖片內容可以通過指定contents屬性的值為CGImageRef。當圖層被創建的時候或者在任何其他時候,這個操作可以在其他實體上面完成(如表3所示)。? ? 代碼 1 設定layer的contents屬性 ? CALayer *theLayer;
? // create the layer and set the bounds and position ? theLayer=[CALayer layer]; ? theLayer.position=CGPointMake(50.0f,50.0f); ? theLayer.bounds=CGRectMake(0.0f,0.0f,100.0f,100.0f);
? // set the contents property to a CGImageRef ? // specified by theImage (loaded elsewhere) ? theLayer.contents=theImage; ? 1.1.2 通過委托提供內容
? 你可以繪制圖層的內容,或更好的封裝圖層的內容圖片,通過創建一個委托類實現下列方法之一: ? displayLayer:或drawLayer:inContext: ?? ? 實現委托重繪的方法并不意味會自動的觸發圖層使用實現的方法來重繪內容。而是你要顯式的告訴一個圖層實例來重新緩存內容,通過發送以下任何一個方法setNeedsDisplay或者setNeedsDisplayInRect:的消息,或者把圖層的needsDisplayOnBoundsChange屬性值設置為YES。 ?? ? 通過委托實現方法displayLayer:可以根據特定的圖層決定顯示什么圖片,還可以更加需要設置圖層的contents屬性值。下面的例子是“圖層的坐標系”部分的,它實現displayerLayer:方法根據state的值設置theLayer的contents屬性。子類不需要存儲state的值,因為CALayer的實例是一個鍵-值編碼容器。
? 代碼 2 ?委托方法displayLayer:的實現示例 ? - (void)displayLayer:(CALayer *)theLayer ? { ? ? ? // check the value of the layer's state key ? ? ? if ([[theLayer valueForKey:@"state"] boolValue]) ? ? ? { ? ? ? ? ? // display the yes image ? ? ? ? ? theLayer.contents=[someHelperObject loadStateYesImage]; ? ? ? } ? ? ? else { ? ? ? ? ? // display the no image ? ? ? ? ? theLayer.contents=[someHelperObject loadStateNoImage]; ? ? ? } ? } ? ? 如果你必須重繪圖層的內容,而不是通過加載圖片,那你需要實現drawLayer:inContext:方法。通過委托可以決定哪些內容是需要的并使用CGContextRef來重繪內容。
? 下面的例子是“指定圖層的幾何”部分內容,它實現了drawLayer:inContext:方法使用lineWidth鍵值來重繪一個路徑(path),返回therLayer。 ? ? 代碼 3 ?代理方法drawLayer:inContext:的實現示例 ? - (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext ? { ? ? CGMutablePathRef thePath = CGPathCreateMutable();
? ? CGPathMoveToPoint(thePath,NULL,15.0f,15.f); ? ? CGPathAddCurveToPoint(thePath, ? ? ? ? ? ? ? ? ? ? ? ? ? NULL, ? ? ? ? ? ? ? ? ? ? ? ? ? 15.f,250.0f, ? ? ? ? ? ? ? ? ? ? ? ? ? 295.0f,250.0f, ? ? ? ? ? ? ? ? ? ? ? ? ? 295.0f,15.0f);
? ? CGContextBeginPath(theContext); ? ? CGContextAddPath(theContext, thePath );
? ? CGContextSetLineWidth(theContext, ? ? ? ? ? ? ? ? ? ? ? ? ? [[theLayer valueForKey:@"lineWidth"] floatValue]); ? ? CGContextStrokePath(theContext);
? ? // release the path ? ? CFRelease(thePath); ? } ? 1.1.3 通過子類提供圖層的內容
? 雖然通常情況不需要這樣做,但是你仍可以繼承CALayer直接重載重繪和顯示方法。這個通常發生在你的圖層需要定制行為而委托又無法滿足需求的時候。
? 子類可以重載CALayer的顯示方法,設置圖層的內容為適當的圖片。下面的例子是“變換圖層的幾何”部分的內容,它提供了和“圖層的坐標系”例子相同的功能。不同的是子類定義state為實例的屬性,而不是根據CALayer的鍵-值編碼容器獲取。 ? ? 代碼 4 ?CALayer display 方法的覆蓋示例 ? - (void)display ? { ? ? // check the value of the layer's state key ? ? if (self.state) ? ? { ? ? ? ? // display the yes image ? ? ? ? self.contents=[someHelperObject loadStateYesImage]; ? ? } ? ? else { ? ? ? ? // display the no image ? ? ? ? self.contents=[someHelperObject loadStateNoImage]; ? ? } ? } ? ? CALayer子類可以通過重載drawInContext:繪制圖層的內容到一個圖形上下文。下面的例子是“修改變換的數據結構”的內容,它和“指定圖層的幾何”里面實現委托的辦法一樣產生相同的圖片內容。唯一的不同的是實現委托里面的lineWidth和lineColor現在是子類實例的屬性。
? Listing 5 ?覆蓋layer的drawInContext:方法示例 ? - (void)drawInContext:(CGContextRef)theContext ? { ? ? CGMutablePathRef thePath = CGPathCreateMutable();
? ? CGPathMoveToPoint(thePath,NULL,15.0f,15.f); ? ? CGPathAddCurveToPoint(thePath, ? ? ? ? ? ? ? ? ? ? ? ? ? NULL, ? ? ? ? ? ? ? ? ? ? ? ? ? 15.f,250.0f, ? ? ? ? ? ? ? ? ? ? ? ? ? 295.0f,250.0f, ? ? ? ? ? ? ? ? ? ? ? ? ? 295.0f,15.0f);
? ? CGContextBeginPath(theContext); ? ? CGContextAddPath(theContext, thePath );
? ? CGContextSetLineWidth(theContext, ? ? ? ? ? ? ? ? ? ? ? ? ? self.lineWidth); ? ? CGContextSetStrokeColorWithColor(theContext, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.lineColor); ? ? CGContextStrokePath(theContext); ? ? CFRelease(thePath); ? } ?? ? 繼承CALayer并且實現其中的重繪方法并不意味重繪會自動發生。你必須顯式的促使實例重新緩存其內容,可以通過發送以下任何一個方法setNeedsDisplay或setNeedsDisplayInRect:的消息,亦或者設置圖層的needsDisplaOnBoundsChange屬性為YES。 ? 1.2 修改圖層內容的位置
? CALayer的屬性contentsGravity允許你在圖層的邊界內容修改圖層的contents圖片的位置或者伸縮值。默認情況下,內容的圖像完全填充層的邊界,忽視自然的圖像寬高比。
? 使用contentsGravity位置常量,你可以指定圖片位于圖層任何一個邊界,比如位于圖層的角落,或者圖層邊界的中心。然而當你使用位置常量的時候,contentsCenter屬性會被忽略。表1列舉了位置常量和他們相應的位置。 ? ? ? layer的contentsGravity屬性的定位常量 ? kCAGravityTopLeft ? ? Positions the content image in the top left corner of the layer. ? kCAGravityTop ? ? Positions the content image horizontally centered along the top edge of the layer. ? kCAGravityTopRight ? ? Positions the content image in the top right corner of the layer. ? kCAGravityLeft ? ? Positions the content image vertically centered on the left edge of the layer. ? kCAGravityCenter ? ? Positions the content image at the center of the layer. ? kCAGravityRight ? ? Positions the content image vertically centered on the right edge of the layer. ? kCAGravityBottomLeft ? ? Positions the content image in the bottom left corner of the layer. ? kCAGravityBottom ? ? Positions the content image centered along the bottom edge of the layer.? ? kCAGravityBottomRight ? ? Positions the content image in the top right corner of the layer. ? ? “圖層的坐標系”標識了所支持的內容位置和他們相應的常量。 ? 圖 1 ?layer的contentsGravity屬性的定位常量 ? ? ? ? ? 通過設置contentsGravity屬性為其他一個常量(如表2所示)。圖層的內容圖片可以被向上或者向下拉伸, 僅當使用其他任何一個調整大小的常量的時候,contentsCenter屬性才會對內容圖片起作用。 ? ? 表 2 ?Layer的 contentsGravity 屬性的縮放常量 ? kCAGravityResize ? ? Resize the content image to completely fill the layer bounds, potentially ignoring the natural aspect of the content. This is the default. ? kCAGravityResizeAspect ? ? Resize the content image to scale such that it is displayed as large as possible within the layer bounds, yet still retains its natural aspect. ? kCAGravityResizeAspectFill ? ? Resize the content image to scale such that it is displayed filling the layer bounds, yet retaining its natural aspect. This may cause the content to extend outside the layer bounds.
? “變換圖層的幾何”演示了如何使用調整大小的模式來調整一個正方形圖像的大小讓其適應圖層的方形邊界。 ? 圖 2 ?Layer的 contentsGravity 屬性的縮放常量 ? ? ?? ? ? 注意:使用任何常量kCAGravityResize、kCAGravityResizeAspect和kCAGravityResizeAspectFill和表1中的重心位置常量無關。圖層的內容將會填充整個邊界,所以使用這些常量無法改變圖層內容的位置。
轉自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-e-the-content-layer.html

總結

以上是生活随笔為你收集整理的CoreAnimation编程指南(五)图层内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。