UIEdgeInsetsMake如何工作? [英] How does UIEdgeInsetsMake work?

查看:142
本文介绍了UIEdgeInsetsMake如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中我使用 UIEdgeInsetsMake resizableImageWithCapInsets ,但我不明白它工作正常, UIEdgeInsetsMake 有4个参数:

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


  • 顶部


但是他们是浮动,所以我不知道如何设置为一个图像,谢谢! :D

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

推荐答案

根据文档:


您可以使用此方法为图像添加上限插入或更改图像的现有上限插入。

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.

在缩放或调整图像大小时,顶盖覆盖的区域不会缩放或调整大小。相反,在每个方向上未被帽覆盖的像素区域被平铺,从左到右和从上到下,以调整图像的大小。此技术通常用于创建可变宽度按钮,其保留相同的圆角,但其中心区域根据需要增长或收缩。为了获得最佳效果,请使用尺寸为1x1像素区域的平铺区域。

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.

因此,您只需使用像素数您希望在 UIEdgeInsetsMake 函数的值中使其不可伸缩。

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

假设您有21x50点的图像(标准清晰度为21x50像素,Retina@ 2x清晰度为42x100像素),并且希望此图像具有水平伸缩性,当拉伸图像时左边和右边的10个点未被触摸,但是仅在中间延伸1点宽的带。然后你将使用 UIEdgeInsetsMake(0,10,0,10)

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).

不要担心浮动(这对子像素调整大小有用,例如,但实际上你可能只使用整数(或没有小数部分的浮点数)。

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)

小心,这是一个iOS5 +方法,在iOS5之前不可用。如果您使用iOS5之前的SDK,请改用 stretchableImageWithLeftCapWidth:topCapHeight:

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

一些提示我使用了一段时间,因为我从来不记得 UIEdgeInsets 结构是 - 而且我们应该将参数传递给 UIEdgeInsetsMake 函数 - 我更喜欢使用指定inits语法如下:

Some tip I use since some time, as I never remember in which order the fields of the UIEdgeInsets structure are — and in which order we are supposed to pass the arguments to UIEdgeInsetsMake function — I prefer using the "designated inits" syntax like this:

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

或需要显式转换时:

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!

这篇关于UIEdgeInsetsMake如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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