在 iphone sdk 中实现可拉伸对话框边框

发布于 2024-09-01 21:25:28 字数 1032 浏览 1 评论 0原文

我想实现可缩放到我需要的对话框大小的对话框边框。也许对于这类事情有一个更好、更传统的名称。如果有的话,如果有人能编辑标题,那就太好了。

无论如何,我想这样做,这样我就可以拥有任何大小的对话框,而不会出现将边框艺术缩放到小、大或古怪的不成比例尺寸所带来的视觉伪影。我对如何做到这一点有一些想法,但不确定哪种更适合 iPhone。我有几个问题。

1)我应该创建一个包含视图对象,基本上重载其drawRect方法并在调用该方法时以适当的比例绘制图像,还是应该维护一个仅包含8个UIImageView的包含视图对象?我怀疑如果我需要像动画一样主动缩放生成的对话框类,则后一种方法将不起作用。

1b)如果重载drawRect是可行的方法,是否有人有一些示例代码或指向演示直接从drawRect()绘制图像的示例的链接?

2) 通常创建

a) 3 x 3 图像(其中各部分位于图像的适当 1x1 网格中)是否更好?如果是这样,从该图像的一部分绘制到我的目标视图上是否简单(如果前面的假设是正确的,我应该使用drawRect)?

b) 这些片段分别存放在 8 个不同的文件中?

更新: 澄清一下,这个想法是采用任何定制的边框艺术,并能够拉伸第二个、第四个、第六个和第八个单元格(在 3x3 单元格网格中),以仅使用这些资源形成任何大小的边框。仅拉伸普通图像会导致角变形,因此我想根据需要拉伸那些偶数编号的单元格并粘在角上,这样就不会变形。我以前见过这样做,所以认为它可能是一个标准的东西,并且有一个标准的命名,而不是我所说的。

无论如何,我被告知向容器添加 8 个 UIImageView 的效率不如在 drawRect 中动态绘制 UIImage 那样有效,因此在对上下文应用必要的转换以平移和缩放 Y 后,使用 CGContextDrawImage() 采取该方法。因为该函数从图像的左下角绘制,但绘制到左上角的 UIView 上,图像是上下颠倒的,没有 Y 轴反转。我注意到使用像drawAtPoint这样的UIImage函数的建议也同样有效,但是对于反转来说,因为UIImage与UIViews的绘制方向相同。我将继续实施前者,看看进展如何,但还有一个问题。

有人会知道这些方法中哪一种更有效、更快等吗?

I want to implement dialog borders that scale to the size I require the dialog to be. Perhaps there is a better more conventional name for this sort of thing. If there is, if someone would edit the title, that'd be great.

Anyhow, I'd like to do this so I can have dialogs of any size without the visual artifacts that come with scaling border art to small, large, or wacky unproportional dimentions. I have a few ideas on how this is done, but am not sure which is better for iphone. I have a few questions.

1) Should I make a containing view object that basically overloads its drawRect method and draws the images where they should be at their appropriate scale when the method is called, or should I main a containing view object that simply contains 8 UIImageViews? I suspect the latter approach won't work if I need to actively scale the resulting dialog class like in an animation.

1b) If overloading drawRect is the way to go, does someone have some sample code or a link to an example that demonstrates drawing an image directly from drawRect()?

2) Is it generally better to create

a) a 3 x 3 image where the segments are in their appropriate 1x1 grid of the image? If so, is it simple to draw from a portion of this image onto my target view in drawRect (if the former assumption is correct that I should use drawRect)?

b) The pieces separately in 8 different files?

UPDATE:
To clarify, the idea is to take any customized border art and be able to stretch the 2nd, 4th, 6th, and 8th cell (in a 3x3-cell grid) to form a border of any size with just those assets. Stretching just a plain image would result in distortion of the corners, so I'd like to stretch those even numbered cells as needed and tack on the corners so there is no distortion. I'd seen this done before so thought it might be a standard thing and have a standard naming to it other than what I called it.

Anyhow, I was advised that adding 8 UIImageViews to a container would not be as efficient as drawing the UIImages on the fly in drawRect so took that approach using CGContextDrawImage() after applying the necessary transformations to the context to translate and scale the Y. Because this function draws from the bottom left corner of an image but onto a top-left origined UIView, the image is upside down without the Y axis invert. I noticed the suggestion to use UIImage functions like drawAtPoint works as well and similarly but for the invert since UIImage draws in the same orientation as UIViews. I will continue my implementation with the former and see how it goes, but one other question.

Would someone happen to know which of these approaches is more efficeint, faster, etc?

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

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

发布评论

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

评论(2

无声无音无过去 2024-09-08 21:25:28

我不确定我是否遵循,但这是我最好的答案......

使用drawRect:或将单独的UIImageViews添加到父视图完全取决于你。 UIImageView 免费为您提供了一些封装的功能,但就外观而言,它们在其他方面是相同的。

如果你确实想走drawRect路线,你只需要使用UIImage的drawAtPoint:方法。计算一下你想要的位置,然后画出来。您可以根据父视图的尺寸计算您的分数。

就缩放而言,不可能在不缩放的情况下调整这些图像的大小,因此我会提前计划并使您的原件与您期望显示的一样大或更大。

希望有一点帮助吗?

干杯

I'm not sure I follow, but here's my best shot at an answer...

Using drawRect: or adding individual UIImageViews to a parent view is entirely up to you. UIImageView gives you a bit of encapsulated functionality for free, but otherwise they are the same as far as appearances go.

If you do want to go the drawRect route, you just need to use UIImage's drawAtPoint: method. Do the math for where you want it to be, and draw it. You can calculate your points based on the parent view's dimensions.

As far as scaling, it's impossible to resize these images without scaling them, so I'd plan ahead and make your originals as large or larger than you ever expect to display them.

Hope that helps a little?

Cheers

べ繥欢鉨o。 2024-09-08 21:25:28

如果您想要对话框上有边框,假设该框是 UIView(或子类),则设置图层的边框属性并让系统为您绘制边框。

#import <QuartzCore/QuartzCore.h> 
// ... 

view.layer.borderWidth = 2;
view.layer.borderColor = [UIColor whiteColor].CGColor; 
view.layer.cornerRadius = 0;  // 0=square corners, >0 for rounded

If you want a border on a dialog box, assuming the box is a UIView (or subclass), then set the layer's border properties and let the system draw the border for you.

#import <QuartzCore/QuartzCore.h> 
// ... 

view.layer.borderWidth = 2;
view.layer.borderColor = [UIColor whiteColor].CGColor; 
view.layer.cornerRadius = 0;  // 0=square corners, >0 for rounded
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文