滚动视图iPhone周围的阴影或边框 [英] Shadow or Border around a scrollview iphone

查看:80
本文介绍了滚动视图iPhone周围的阴影或边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在uiscrollview周围绘制边框/阴影,我知道我可以通过其他视图或scrollview到达那里,但不喜欢处理缺点,但我听说应该有可能直接绘制边框滚动视图,这就是我想要的.

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图像,该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周围的阴影或边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆