問(wèn)題描述
我正在制作一個(gè)應(yīng)用程序,我將 UIEdgeInsetsMake
用于 resizableImageWithCapInsets
,但我不明白它是如何工作的,UIEdgeInsetsMake
有 4 個(gè)參數(shù):
I'm making an app where I use UIEdgeInsetsMake
for resizableImageWithCapInsets
, but I don't understand how does it works exactly, UIEdgeInsetsMake
has 4 arguments:
- 頂部
- 左
- 底部
- 對(duì)
但它們是浮動(dòng)的,所以我不知道如何將其設(shè)置為圖像,謝謝!:D
But they're floats so I don't know how to set that to an image, thanks! :D
推薦答案
根據(jù)文檔:
您可以使用此方法向圖像添加大寫插圖或更改圖像的現(xiàn)有大寫插圖.在這兩種情況下,您都會(huì)取回一張新圖像,而原始圖像保持不變.
You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched.
在圖像縮放或調(diào)整大小期間,被帽子覆蓋的區(qū)域不會(huì)被縮放或調(diào)整大小.相反,每個(gè)方向上未被帽覆蓋的像素區(qū)域從左到右和從上到下平鋪,以調(diào)整圖像大小.這種技術(shù)通常用于創(chuàng)建可變寬度按鈕,這些按鈕保留相同的圓角,但其中心區(qū)域會(huì)根據(jù)需要增大或縮小.為獲得最佳性能,請(qǐng)使用大小為 1x1 像素的平鋪區(qū)域.
During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.
所以您只需要在 UIEdgeInsetsMake
函數(shù)的值中使用您想要使其不可拉伸的像素?cái)?shù)量.
So you only need to use the amount of pixels you want to make unstretchable in the values of the UIEdgeInsetsMake
function.
假設(shè)您有一張 21x50 點(diǎn)的圖像(標(biāo)準(zhǔn)清晰度為 21x50 像素,Retina @2x"清晰度為 42x100 像素)并希望此圖像可水平拉伸,保持左右 10 個(gè)點(diǎn)不變拉伸圖像時(shí),但僅拉伸中間 1 點(diǎn)寬的帶.然后你將使用 UIEdgeInsetsMake(0,10,0,10)
.
Say you have an image of 21x50 points (21x50 pixels in standard definition, 42x100 pixels in Retina "@2x" definition) and want this image to be horizontally stretchable, keeping the 10 points on the left and on the right untouched when stretching the image, but only stretch the 1-point-wide band in the middle. Then you will use UIEdgeInsetsMake(0,10,0,10)
.
不要擔(dān)心它們是浮點(diǎn)數(shù)(例如,這對(duì)于亞像素調(diào)整大小很有用,但實(shí)際上您可能只會(huì)使用整數(shù)(或沒(méi)有小數(shù)部分的浮點(diǎn)數(shù))
Don't bother that they are floats (that's useful for subpixelling resizing for example, but in practice you will probably only use integers (or floats with no decimal parts)
注意,這是 iOS5+ 獨(dú)有的方法,iOS5 之前不可用.如果您使用 iOS5 之前的 SDK,請(qǐng)改用 stretchableImageWithLeftCapWidth:topCapHeight:
.
Be careful, this is an iOS5+ only method, not available prior iOS5. If you use pre-iOS5 SDK, use stretchableImageWithLeftCapWidth:topCapHeight:
instead.
一些提示我使用了一段時(shí)間,因?yàn)槲也挥浀?UIEdgeInsets
結(jié)構(gòu)的字段的順序是什么 - 以及我們應(yīng)該以什么順序?qū)?shù)傳遞給 UIEdgeInsetsMake
函數(shù) — 我更喜歡使用 指定初始化"語(yǔ)法如下:
Some tip I use since some time, as I never remember in which order the fields of the UIEdgeInsets
structure are — and in which order we are supposed to pass the arguments to UIEdgeInsetsMake
function — I prefer using the "designated inits" syntax like this:
UIEdgeInsets insets = { .left = 50, .right = 50, .top = 10, .bottom = 10 };
或者當(dāng)需要顯式轉(zhuǎn)換時(shí):
Or when an explicit cast is needed:
UIImage* rzImg = [image resizableImageWithCapInsets:(UIEdgeInsets){
.left = 50, .right = 50,
.top = 10, .bottom = 10
}];
我發(fā)現(xiàn)它更具可讀性,尤其是要確保我們不會(huì)混合不同的邊框/方向!
I find it more readable, especially to be sure we don't mix the different borders/directions!
這篇關(guān)于UIEdgeInsetsMake 是如何工作的?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!