iOS - 数学帮助 - 基本图像缩放与捏手势需要叠加图像调整X / Y坐标相对 [英] iOS - Math help - base image zooms with pinch gesture need overlaid images adjust X/Y coords relative

查看:244
本文介绍了iOS - 数学帮助 - 基本图像缩放与捏手势需要叠加图像调整X / Y坐标相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPad应用程序,它有一个基本图像UIImageView(在这种情况下是一个大型建筑或场地平面图或图表),然后可以在计划的顶部添加多个图钉(在视觉上类似于谷歌地图)。这些引脚也是UIImageViews,并在轻击手势的主视图中添加。基本图像也会添加到viewDidLoad的主视图中。

I have an iPad application that has a base image UIImageView (in this case a large building or site plan or diagram) and then multiple 'pins' can be added on top of the plan (visually similar to Google Maps). These pins are also UIImageViews and are added to the main view on tap gestures. The base image is also added to the main view on viewDidLoad.

我的基本图像使用捏合手势进行缩放,但显然当你缩放基本图像时引脚保持在主视图的相同x和y坐标处,并且在基本图像(其x,y和宽度,高度坐标已更改)上相对定位松散。

I have the base image working with the pinch gesture for zooming but obviously when you zoom the base image all the pins stay in the same x and y coordinates of the main view and loose there relative positioning on the base image (whose x,y and width,height coordinates have changed).

到目前为止我有这个......

So far i have this...

- (IBAction)planZoom:(UIPinchGestureRecognizer *) recognizer;
{
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;

    for (ZonePin *pin in planContainer.subviews) {
        if ([pin isKindOfClass:[ZonePin class]]){
            CGRect pinFrame = pin.frame;

            // ****************************************
            // code to reposition the pins goes here...
            // ****************************************

            pin.frame = pinFrame;
        }
    }
}

我需要帮助来计算数学以重新定位引脚x / y坐标以保持放大或缩小计划/图表中的相对位置。这些引脚显然不希望在宽度或高度方面进行缩放/缩放 - 它们只需要相对于平面上初始位置的新x和y坐标。

I need help to calculate the math to reposition the pins x/y coordinates to retain there relative position on the zoomed in or out plan/diagram. The pins obviously do not want to be scaled/zoomed at all in terms of their width or height - they just need new x and y coordinates that are relative to there initial positions on the plan.

我自己试图计算出数学但是很难解决这个问题,不幸的是我还不熟悉SDK,知道是否有内置的服务可以提供帮助。

I have tried to work out the math myself but have struggled to work it through and unfortunately am not yet acquainted with the SDK enough to know if there is provision available built in to help or not.

非常感谢帮助解决这个与数学相关的问题! :)

Help with this math related problem would be really appreciated! :)

非常感谢,
迈克尔。
InNeedOfMathTuition.com

Many thanks, Michael. InNeedOfMathTuition.com

推荐答案

首先,您可以尝试嵌入 UIImageView UIScrollView 中,因此缩放基本上是为您完成的。然后,您可以轻松设置最大和最小刻度,并且可以根据需要滚动缩放图像(特别是如果您的图钉是 UIImageView 的子视图或 UIScrollView )。

First, you might try embedding your UIImageView in a UIScrollView so zooming is largely accomplished for you. You can then set the max and min scale easily, and you can scroll around the zoomed image as desired (especially if your pins are subviews of the UIImageView or something else inside the UIScrollView).

至于缩放引脚的位置,我认为它可以存储原始x和每个引脚的y坐标(即视图首次加载时,首次定位时,按比例为1.0)。然后在缩放视图时,设置 x =(originalX * zoomScale) y =(originalY * zoomScale)

As for scaling the locations of the pins, I think it would work to store the original x and y coordinates of each pin (i.e. when the view first loads, when they are first positioned, at scale 1.0). Then when the view is zoomed, set x = (originalX * zoomScale) and y = (originalY * zoomScale).

几年前我在iOS应用程序中遇到了同样的问题,如果我没记错的话,那就是我完成它的方式。

I had the same problem in an iOS app a couple of years ago, and if I recall correctly, that's how I accomplished it.

编辑:下面是关于我如何实现这一点的更多细节(我现在正在查看我的旧代码)。

EDIT: Below is more detail about how I accomplished this (I'm looking my old code now).

我有一个 UIScrollView 作为我主视图的子视图,我的 UIImageView 作为其子视图。我的按钮被添加到滚动视图中,我保留了原始位置(缩放1.0)以供参考。

I had a UIScrollView as a subview of my main view, and my UIImageView as a subview of that. My buttons were added to the scroll view, and I kept their original locations (at zoom 1.0) stored for reference.

In - (void) scrollViewDidScroll:(UIScrollView *)scrollView 方法:

for (id element in myButtons)
{
   UIButton *theButton = (UIButton *)element;
   CGPoint originalPoint = //get original location however you want
   [theButton setFrame:CGRectMake(
       (originalPoint.x - theButton.frame.size.width / 2) * scrollView.zoomScale,
       (originalPoint.y - theButton.frame.size.height / 2) * scrollView.zoomScale,
       theButton.frame.size.width, theButton.frame.size.height)];
}

对于 - (UIView *)viewForZoomingInScrollView :( UIScrollView *)scrollView 方法,我返回了我的 UIImageView 。我的按钮按比例缩放,但我没有在上面的代码中包含它。如果您发现引脚尺寸自动缩放,则可能必须存储原始尺寸和原始坐标,并在 setFrame 调用中使用。

For the -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView method, I returned my UIImageView. My buttons scaled in size, but I didn't include that in the code above. If you're finding that the pins are scaling in size automatically, you might have to store their original sizes as well as original coordinates and use that in the setFrame call.

这篇关于iOS - 数学帮助 - 基本图像缩放与捏手势需要叠加图像调整X / Y坐标相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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