使用缩放将 UIImageView 定位在另一个之上 [英] Position an UIImageView over another with scaling

查看:37
本文介绍了使用缩放将 UIImageView 定位在另一个之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIImageView 上是否有一个方法可以告诉我它的图像在其边界内的位置?假设我有一张汽车图片,如下所示:

Is there a method on UIImageView that tells me the position of its image within its bounds? Say I have an image of a car, like this:

这张图片是 600x243,后轮应该在的地方,有一个洞,它是 118,144,74,74 (x,y,w,h).

This image is 600x243, and, where the rear wheel should be, there's a hole which is 118,144,74,74 (x,y,w,h).

我想让用户看到不同的后轮选项,我有一些车轮图像可供选择(都是方形的,所以它们很容易缩放以匹配汽车上的孔).

I want to let the user see different rear wheel options, and I have a few wheel images to choose from (all square, so they are easily scaled to match the hole in the car).

我想将汽车图像放在一个 UIImageView 中,它的大小基于布局是任意的,我想以自然的纵横比看到整辆车.所以我将图像视图的内容模式设置为 UIViewContentModeScaleAspectFit,效果很好.

I wanted to place the car image in a UIImageView whose size is arbitrary based on layout, and I wanted to see the whole car at the natural aspect ratio. So I set the image view's content mode to UIViewContentModeScaleAspectFit, and that worked great.

例如,这是一个 267x200 的图像视图中的汽车:

For example, here's the car in an imageView that is 267x200:

我认为这样做会将图像从 w=600 缩放到 w=267,或者以 267/600=0.445 的系数缩放,并且(我认为)这意味着高度从 200 变为 200*0.445=89.而且我认为这个洞也被这个因素放大了

I think doing this scaled the image from w=600 to w=267, or, by a factor of 267/600=0.445, and (I think) that means that the height changed from 200 to 200*0.445=89. And I think it's true that the hole was scaled by that factor, too

但我想添加一个 UIImageView 子视图来显示轮子,这就是我感到困惑的地方.我知道图像大小,我知道 imageView 大小,我知道原始图像大小的孔框.图像缩放后如何获取孔框?

But I want to add a UIImageView subview to show the wheel, this is where I get confused. I know the image size, I know the imageView size, and I know the hole frame in terms of the original image size. How do I get the hole frame after the image is scaled?

我尝试过这样的事情:

  1. 确定汽车图像在其 UIImageView 中的位置.就像这样:

  1. determine the position of the car image in its UIImageView. That's something like:

float ratio=carImage.width/carImageView.frame.size.width;  // 0.445
CGFloat yPos=(carImageView.frame.size.height-carImage.height)/2;  // there should be a method for this?

  • 确定孔的缩放框架:

  • determine the scaled frame of the hole:

    CGFloat holeX = ratio*118;
    CGFloat holeY = yPos + ratio*144;
    CGFloat holeEdge = ratio*74;
    CGRect holeRect = CGRectMake(holeX,holeY,holeEdge,holeEdge);
    

  • 但一定有更好的方法.这些计算(如果它们是正确的)仅适用于 比汽车高的汽车图像视图.如果图像视图更宽,代码需要不同.

    But there must be a better way. These calculations (if they are right) are only right for a car image view that is taller than the car. The code needs to be different if the image view is wider.

    我想我可以为更广阔的视野找出逻辑,但它仍然可能是错误的.例如,那个 yPos 计算.文档是否说,对于内容模式 = AspectFit,图像在较大尺寸内居中?我没有看到任何地方.

    I think I can work out the logic for a wider view, but it still might be wrong. For example, that yPos calculation. Do the docs say that, for content mode = AspectFit, the image is centered inside the larger dimension? I don't see that any place.

    请告诉我有更好的方法,或者,如果没有,是否证明我的想法适用于任意大小的图像、图像视图、孔洞?

    Please tell me there's a better way, or, if not, is it proven that my idea here will work for arbitrary size images, image views, holes?

    谢谢.

    推荐答案

    (到目前为止)最简单的解决方案是对汽车图像和车轮选项图像使用相同的尺寸.

    The easiest solution (by far) is to simply use the same sizes for both the car image and the wheel option images.

    只需给车轮选项一个透明的填充(几乎在每个图形编辑程序中都很容易做到),并用相同的框架将它们覆盖在汽车上.

    Just give the wheel options a transparent padding (easy to do in nearly every graphics editing program), and overlay them over the car with the same frame.

    您可能会以微不足道的方式增加资产规模......但它会为您解决地狱尝试计算头寸和规模的麻烦,尤其是当您正在缩放汽车图像.

    You may increase your asset sizes by a minuscule amount.. but it'll save you one hell of a headache trying to work out positions and sizings, especially as you're scaling the car image.

    这篇关于使用缩放将 UIImageView 定位在另一个之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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