ios UIEdgeInsetsMake 是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7628004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:42:49  来源:igfitidea点击:

How does UIEdgeInsetsMake work?

ioscocoa-touchios5uiimageuiedgeinsets

提问by pmerino

I'm making an app where I use UIEdgeInsetsMakefor resizableImageWithCapInsets, but I don't understand how does it works exactly, UIEdgeInsetsMakehas 4 arguments:

我正在制作一个用于UIEdgeInsetsMakefor的应用程序resizableImageWithCapInsets,但我不明白它是如何工作的,UIEdgeInsetsMake有 4 个参数:

  • Top
  • Left
  • Bottom
  • Right
  • 最佳
  • 剩下
  • 底部

But they're floats so I don't know how to set that to an image, thanks! :D

但是它们是浮动的,所以我不知道如何将其设置为图像,谢谢!:D

回答by AliSoftware

According to the documentation:

根据文档:

You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched.

During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.

您可以使用此方法向图像添加帽状插图或更改图像的现有帽状插图。在这两种情况下,您都会返回一个新图像,而原始图像保持不变。

在缩放或调整图像大小期间,帽盖覆盖的区域不会缩放或调整大小。相反,每个方向上没有被帽盖覆盖的像素区域被平铺,从左到右,从上到下,以调整图像大小。这种技术通常用于创建宽度可变的按钮,这些按钮保留相同的圆角,但其中心区域会根据需要扩大或缩小。为获得最佳性能,请使用大小为 1x1 像素区域的平铺区域。

So you only need to use the amount of pixels you want to make unstretchable in the values of the UIEdgeInsetsMakefunction.

所以你只需要在UIEdgeInsetsMake函数的值中使用你想要使其不可拉伸的像素数量。

Say you have an image of 21x50 points (21x50 pixels in standard definition, 42x100 pixels in Retina "@2x" definition) and want this image to be horizontally stretchable, keeping the 10 points on the left and on the right untouched when stretching the image, but only stretch the 1-point-wide band in the middle. Then you will use UIEdgeInsetsMake(0,10,0,10).

假设您有一张 21x50 点的图像(标准清晰度中为 21x50 像素,Retina“@2x”清晰度中为 42x100 像素)并希望该图像可水平拉伸,拉伸图像时保持左右 10 个点不变,但只拉伸中间 1 点宽的带。然后您将使用UIEdgeInsetsMake(0,10,0,10).

Don't bother that they are floats (that's useful for subpixelling resizing for example, but in practice you will probably only use integers (or floats with no decimal parts)

不要担心它们是浮点数(例如,这对于调整子像素大小很有用,但实际上您可能只会使用整数(或没有小数部分的浮点数)

Be careful, this is an iOS5+ only method, not available prior iOS5. If you use pre-iOS5 SDK, use stretchableImageWithLeftCapWidth:topCapHeight:instead.

请注意,这是仅适用于 iOS5+ 的方法,在 iOS5 之前不可用。如果您使用 iOS5 之前的 SDK,请stretchableImageWithLeftCapWidth:topCapHeight:改用。



[EDIT] Some tipI use since some time, as I never remember in which order the fields of the UIEdgeInsetsstructure are — and in which order we are supposed to pass the arguments to UIEdgeInsetsMakefunction — I prefer using the "designated inits" syntaxlike this:

[编辑]一段时间以来我使用的一些技巧,因为我从不记得UIEdgeInsets结构字段的顺序- 以及我们应该以什么顺序将参数传递给UIEdgeInsetsMake函数 - 我更喜欢使用“指定的初始化”语法这样:

UIEdgeInsets insets = { .left = 50, .right = 50, .top = 10, .bottom = 10 };

Or when an explicit cast is needed:

或者当需要显式转换时:

UIImage* rzImg = [image resizableImageWithCapInsets:(UIEdgeInsets){
   .left = 50, .right = 50,
   .top = 10, .bottom = 10
}];

I find it more readable, especially to be sure we don't mix the different borders/directions!

我发现它更具可读性,尤其是要确保我们不会混合不同的边框/方向!

回答by matt

But they're floats so I don't know how to set that to an image

但它们是浮动的,所以我不知道如何将其设置为图像

This is a UIImage instance method. So, the way you use resizableImageWithCapInsetsto create in an image is to start by sending that message toan image (a UIImage).

这是一个 UIImage 实例方法。因此,您resizableImageWithCapInsets在图像中创建的方法是首先将该消息发送图像(UIImage)。

Cool new feature: note that if the edge insets are all zero, the image will be tiled. This works as a background to anything that accepts a background image. It even works in a UIImageView.

很酷的新功能:请注意,如果边缘插入都为零,则图像将被平铺。这可以作为任何接受背景图像的背景。它甚至适用于 UIImageView。

回答by Azhar

I just go through this article which cleared all my concept regarding UIEdgeInsetsMake

我刚刚浏览了这篇文章,它清除了我所有关于 UIEdgeInsetsMake

iOS: How To Make A Stretchable Button With UIEdgeInsetsMake

iOS:如何使用 UIEdgeInsetsMake 制作可伸缩按钮