滚动视图 iPhone 周围的阴影或边框

发布于 2024-08-07 20:46:48 字数 129 浏览 5 评论 0原文

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

我对 iPhone 开发很陌生,任何答案都会有帮助。

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.

I am quiet new to iphone developement,any answer would helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风尘浪孓 2024-08-14 20:46:48

如果您使用滚动视图(或任何 UIView)的 Layer 属性,您可以轻松获得实心边框...

#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 区域。然后使用以下方法将其设置为可缩放图像:

newImage = [image stretchableImageWithLeftCapWidth:6 topCapHeight:6];

更新: 由于 iOS 5.0 stretchableImageWithLeftCapWidth:topCapHeight: 已弃用,因此仅当您仍想支持 iOS 4.x 设备时才使用此选项。如果您只想支持 iOS 5.0+ 设备,请使用以下方法:

newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];

然后将图像放在父视图上,使其占据滚动视图的整个区域。如果您希望阴影覆盖可滚动元素(因此您的滚动视图看起来嵌入/位于页面其余部分的后面),则在顶部放置一个透明的 UIView,上面有阴影图像,以便它显示到您的滚动视图在它后面。

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;

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];

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)];

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.

夏末的微笑 2024-08-14 20:46:48

不要忘记:

#import <QuartzCore/QuartzCore.h>

如果您想使用

myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;

Dont forget to:

#import <QuartzCore/QuartzCore.h>

If you want to use

myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;
谜泪 2024-08-14 20:46:48

要从 UIColor 获取 CGColorRef,您还可以使用以下示例:

myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;

To get the CGColorRef from an UIColor you can also use this example:

myView.layer.borderWidth = 2;
myView.layer.borderColor = [UIColor blackColor].CGColor;
愛上了 2024-08-14 20:46:48

myView.layout.borderColor 在使用 UIColor 时生成不兼容的指针警告,并且没有为我更改边框。

使用 CGColorRef 对我有用:

CGFloat lComponents[4] = {0,0,0,1};
CGColorSpaceRef lColorSpace = CGColorSpaceCreateDeviceRGB();
myView.layer.borderColor = CGColorCreate(lColorSpace, lComponents);
CGColorSpaceRelease(lColorSpace);

myView.layout.borderColor generates an incompatible pointer warning when using UIColor, and did not change the border for me.

Using CGColorRef worked for me instead:

CGFloat lComponents[4] = {0,0,0,1};
CGColorSpaceRef lColorSpace = CGColorSpaceCreateDeviceRGB();
myView.layer.borderColor = CGColorCreate(lColorSpace, lComponents);
CGColorSpaceRelease(lColorSpace);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文