問(wèn)題描述
我想在 uiscrollview 周圍繪制邊框/陰影,我知道我可以通過(guò)額外的視圖或滾動(dòng)視圖到達(dá)那里,但不喜歡處理缺點(diǎn),但我聽(tīng)說(shuō)應(yīng)該有可能直接繪制邊框到滾動(dòng)視圖,這就是我想要的.
i would like to draw a border / shadow around a uiscrollview, i know that i could get there with an additional view or scrollview but dont like the handling an drawbacks but i heard that there should be a possibility to dirctly draw a border to a scrollview and that is what i would prefer.
我是 iphone 開(kāi)發(fā)的新手,任何答案都會(huì)有所幫助.
I am quiet new to iphone developement,any answer would helpful.
推薦答案
如果您使用滾動(dòng)視圖(或任何 UIView)的 layer 屬性,您可以輕松獲得實(shí)心邊框...
If you use the layer property of your scroll view (or any UIView) you can easily get a solid border...
#import <QuartzCore/QuartzCore.h>
...
myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;
您也可以通過(guò)設(shè)置 layer.shadow*
屬性來(lái)使用圖層來(lái)應(yīng)用實(shí)時(shí)陰影,但是使用此技術(shù)可能會(huì)降低性能,因此我通常更喜歡使用以下更復(fù)雜的方法,但更高效的技術(shù).您可以創(chuàng)建一個(gè)中間透明、邊緣有陰影的 PNG 圖像 - 它需要有 9 個(gè)不同的區(qū)域:每個(gè)角 4 個(gè),每個(gè)邊緣 4 個(gè),中間有一個(gè)完全透明的 1x1 像素區(qū)域.例如,如果您的陰影在圖像中延伸 6 個(gè)像素,則您的圖像將是 13x13,具有 6 像素寬/高邊框和 1x1 區(qū)域在中間.然后使用以下方法將其設(shè)置為可縮放圖像:
You can also use the layer to apply real-time shadows by setting the layer.shadow*
properties, but performance can be slow with this technique, so I generally prefer to use the following more complex, but more performant technique. You can create a PNG image with transparency in the middle and shadows around the edge - it needs to have 9 distinct areas: 4 for each corner, 4 for each edge, and a completely transparent 1x1 pixel area in the middle. For example if your shadow extends 6 pixels into your image, your image would be 13x13 with the 6 pixel wide/high borders and the 1x1 area in the middle. Then you set it as a scalable image using:
newImage = [image stretchableImageWithLeftCapWidth:6 topCapHeight:6];
更新: 由于 iOS 5.0 stretchableImageWithLeftCapWidth:topCapHeight:
已棄用,因此僅當(dāng)您仍想支持 iOS 4.x 設(shè)備時(shí)才使用此功能.如果您只想支持 iOS 5.0+ 設(shè)備,請(qǐng)改用:
UPDATE: Since iOS 5.0 stretchableImageWithLeftCapWidth:topCapHeight:
is deprecated so only use this if you still want to support iOS 4.x devices. If you want to support only iOS 5.0+ devices use this instead:
newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
然后將圖像放在父視圖上,這樣它就占據(jù)了滾動(dòng)視圖的整個(gè)區(qū)域.如果您希望陰影覆蓋您的可滾動(dòng)元素,(因此您的滾動(dòng)視圖看起來(lái)嵌入/位于頁(yè)面的其余部分之后)然后在頂部放置一個(gè)透明的 UIView 并在其上放置陰影圖像,以便它顯示到您的滾動(dòng)視圖在它后面.
Then you put the image on the parent view so it takes up the entire area of the scroll view. If you want the shadows to go OVER your scrollable elements, (so your scroll view looks inset/behind the rest of the page) then place a transparent UIView over the top with the shadow image on it so that it shows through to your scroll view behind it.
這篇關(guān)于滾動(dòng)視圖 iphone 周圍的陰影或邊框的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!