問題描述
我想在 uiscrollview 周圍繪制邊框/陰影,我知道我可以通過額外的視圖或滾動視圖到達那里,但不喜歡處理缺點,但我聽說應該有可能直接繪制邊框到滾動視圖,這就是我想要的.
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 開發的新手,任何答案都會有所幫助.
I am quiet new to iphone developement,any answer would helpful.
推薦答案
如果您使用滾動視圖(或任何 UIView)的 layer 屬性,您可以輕松獲得實心邊框...
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;
您也可以通過設置 layer.shadow*
屬性來使用圖層來應用實時陰影,但是使用此技術可能會降低性能,因此我通常更喜歡使用以下更復雜的方法,但更高效的技術.您可以創建一個中間透明、邊緣有陰影的 PNG 圖像 - 它需要有 9 個不同的區域:每個角 4 個,每個邊緣 4 個,中間有一個完全透明的 1x1 像素區域.例如,如果您的陰影在圖像中延伸 6 個像素,則您的圖像將是 13x13,具有 6 像素寬/高邊框和 1x1 區域在中間.然后使用以下方法將其設置為可縮放圖像:
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:
已棄用,因此僅當您仍想支持 iOS 4.x 設備時才使用此功能.如果您只想支持 iOS 5.0+ 設備,請改用:
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)];
然后將圖像放在父視圖上,這樣它就占據了滾動視圖的整個區域.如果您希望陰影覆蓋您的可滾動元素,(因此您的滾動視圖看起來嵌入/位于頁面的其余部分之后)然后在頂部放置一個透明的 UIView 并在其上放置陰影圖像,以便它顯示到您的滾動視圖在它后面.
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.
這篇關于滾動視圖 iphone 周圍的陰影或邊框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!