Objective c UIView坐标系-偏移和平移

Objective c UIView坐标系-偏移和平移,objective-c,uiview,translation,coordinates,coordinate-systems,Objective C,Uiview,Translation,Coordinates,Coordinate Systems,我很确定这更像是一个数学问题,但我会在UIView和iPad相关的objective C的上下文中表达它 我正在从从其他地方下载的一些公共域材料创建的映射文件中导入原始数据,然后分离以隔离映射中的各个区域。每个地区都有许多次区域,很像,例如,美国大陆,然后是美国境内出现的各个州,然后每个次区域又被分解成,比如说,县 每个州、每个县都有一个边界框,告诉我每个州的起点、宽度和高度 在我的初始设置中,我为每个州创建了一个单独的视图,然后为每个县创建了另一个视图。相对于我通过interface buil

我很确定这更像是一个数学问题,但我会在UIView和iPad相关的objective C的上下文中表达它

我正在从从其他地方下载的一些公共域材料创建的映射文件中导入原始数据,然后分离以隔离映射中的各个区域。每个地区都有许多次区域,很像,例如,美国大陆,然后是美国境内出现的各个州,然后每个次区域又被分解成,比如说,县

每个州、每个县都有一个边界框,告诉我每个州的起点、宽度和高度

在我的初始设置中,我为每个州创建了一个单独的视图,然后为每个县创建了另一个视图。相对于我通过interface builder(称为mainContainerView)创建的视图,表示州/县区域的多边形被渲染(显然,县位于州的顶部,因此可见)。这个初始设置工作正常

现在我正试图改变一些事情,通过将县添加到包含该州多边形的UIView中,这样我就能够将该州作为剪辑遮罩覆盖在县上。问题是,无论我做什么尝试,我似乎都无法让该县在该州的视野中找到正确的位置

它似乎应该是简单的加法或减法,因为每个项目的缩放都完全相同,我不尝试进行任何主要转换,因此我不认为需要CFAffineTransformation系列

如果有必要,我可以发布代码,但我并不是想让别人帮我写程序;我只是想有人在这里给我指出正确的方向,给我一个建议,如何在州的视野内设置县相对于州的位置

根据请求,以下是我正在编写的相关代码。这段代码不起作用,但它让你知道我在做什么。发布样本数据有点困难,因为它涉及从.SHP文件中提取的点和数据数组,该文件用于生成地图(和细分)。我将在代码中包含一些注释和一些实际的点值,我将逐步通过程序向您展示它们发生了什么

蒙版最大东距、蒙版最大北距、蒙版最小东距和蒙版最小北距是常量,用于定义由州组成的整个国家地图的边界框

DIST_MAX_EASTING、DIST_MAX_NORTHING、DIST_MIN_EASTING和DIST_MIN_NORTHING是常数,用于定义由县组成的国家地图的边界框。两个贴图的比例略有不同,因此,通过使用不同的边界框,我能够将两个贴图缩放到相同的大小

-(void)didLoadMap:(NSNotification *)notification {

id region = [notification object];
ShapePolyline *polygon = [region polygon];

if ([notification name] == @"MapsLoadingForState") {

    // m_nBoundingBox is an array which contains the RAW northing and easting values for each subdivision.  [0] - west limit, [1] - south limit, [2] - east limit, [3] - north limit.

    // The code below, combined with the drawrect method in DrawMap.m (below) puts all the states on the map in precisely the right places, so for the state maps, it works just fine.

    CGFloat originX = ((polygon->m_nBoundingBox[0]-MASK_MIN_EASTING)*stateScaleMultiplier)+([mainContainerView frame].size.width/2);

    CGFloat originY = ((MASK_MAX_NORTHING-(polygon->m_nBoundingBox[3]))*stateScaleMultiplier)+[mainContainerView frame].origin.y;  
    CGFloat width = polygon->m_nBoundingBox[2] - polygon->m_nBoundingBox[0];
    CGFloat height = polygon->m_nBoundingBox[3] - polygon->m_nBoundingBox[1];

    CGFloat scaledWidth = width*stateScaleMultiplier;
    CGFloat scaledHeight = height*stateScaleMultiplier;
    UIColor *subViewColor = [UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:0.0];

    stateMapView = [[DrawMap alloc] initWithFrame:CGRectMake(originX, originY, scaledWidth, scaledHeight)];
    [stateMapView setBackgroundColor:subViewColor];

    [stateMapView setStateScale:stateScaleMultiplier];
    [stateMapView setCountyScale:countyScaleMultiplier];  // Not actually needed.

    [stateMapView setClippingMask:polygon];
    UIColor *colorMask = [UIColor colorWithWhite:1.0 alpha:1.0];
    [stateMapView setForeground:colorMask];

    [states addObject:stateMapView];                      // Add the state map view to an array (for future use)
    [mapView addSubview:stateMapView];  // MapView is a UIView of equivalent size and shape as mainContainerView.

} else {

    // This is where the problems occur.  

    CGFloat originX = (polygon->m_nBoundingBox[0]-DIST_MIN_EASTING);  // 4431590 (raw data)
    originX *= countyScaleMultiplier;  // 303.929108
    originX += ([mainContainerView frame].size.width/2);  // 815.929077

    CGFloat originY = (DIST_MAX_NORTHING-polygon->m_nBoundingBox[3]); 4328997 
    originY *= countyScaleMultiplier;  // 296.893036
    originY -= [mainContainerView frame].origin.y;  // 340.893036

    CGRect frame = [stateMapView frame];  // Dummy variable created for watches in the debugger.  x=856.237183, y=332.169922 width=34.3800087, height=28.7534008

    // When I was invoking DrawMap.h and the included drawrect method, the county map would easily be displayed in the right place, as you can see by the values above.

    // This is where I think the problem is.  The X value is WAY off as far as I can tell.
    originX -= frame.origin.x; // -40.3081055 
    originY -= frame.origin.y; // 8.72311401

    CGPoint countyOrigin = CGPointMake(originX,originY);

    // Translate the county's origin so it is relative to the origin of stateMapView, not MainContainerView (doesn't work)

    [stateMapView addCountyMap:[region polygon] withColor:winner translatedBy:countyOrigin];
    [stateMapView setNeedsDisplay];
}
我知道这段代码有几个问题,一些超出这个问题范围的东西可能会让你们中的一些人感到惊讶,但这肯定是一项正在进行的工作

下面是DrawMap.m中的相关代码;我删掉了一大堆东西,因为它们是无关的

- (void)drawRect:(CGRect)rect {

// Set up

for (int i=0;i<[countyMaps count];i++) {

    // Draw the polygon.

    [[countyColors objectAtIndex:i] setFill];

    [self drawPolygon:[countyMaps objectAtIndex:i]
           usingScale:stateScale
         translatedBy:CGPointMake([[countyTranslations objectAtIndex:2*i] floatValue],
                                  [[countyTranslations objectAtIndex:2*i+1] floatValue])]; 

}

// Set the blend mode to multiply

CGContextSetBlendMode(context, kCGBlendModeMultiply);

// Draw a path with clippingMask

[[UIColor colorWithWhite:1.0 alpha:1.0] setFill];
// CGPoint translate = CGPointMake(0,0);

[self drawPolygon:clippingMask usingScale:stateScale translatedBy:CGPointMake(0,0)];

}

-(void)drawPolygon:(ShapePolyline *)aPolygon usingScale:(float)mapScale translatedBy:(CGPoint)trans {

for (int j=0;j<[aPolygon numParts];j++) {

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path setLineJoinStyle:kCGLineJoinRound];

    int startIndex = [[[aPolygon m_Parts] objectAtIndex:j] intValue];
    int endIndex = [aPolygon numPoints];

    CGPoint startPoint;
    [[[aPolygon m_Points] objectAtIndex:startIndex] getValue:&startPoint];

    startPoint.x *=mapScale;
    startPoint.y *=mapScale;

    startPoint.x -= trans.x;
    startPoint.y -= trans.y;

    [path moveToPoint:startPoint];


    if (j+1 != [aPolygon numParts]){ 

        endIndex = [[[aPolygon m_Parts] objectAtIndex:j+1] intValue];
    }

    for (int k=startIndex+1; k<endIndex; k++)
    {
        CGPoint nextPoint;
        [[[aPolygon m_Points] objectAtIndex:k] getValue:&nextPoint];

        nextPoint.x *= mapScale;
        nextPoint.y *= mapScale;

        nextPoint.x -= trans.x;
        nextPoint.y -= trans.y;

        [path addLineToPoint:nextPoint];

    }
    [path closePath];
    // [path stroke];
    [path fill];
}

}
-(void)drawRect:(CGRect)rect{
//设立
对于(int i=0;i-已解决-

它是如此简单。我很惊讶我花了这么长时间才弄明白,因为我的第一个问题是对的——它是简单的加减法:

现在,所有平移都在渲染多边形的方法中完成。对于多边形中的每个点,我需要添加州视图的原点,减去县边界框的原点,然后从Y值(控制栏的高度)中减去44


我认为,这是一个过度思考问题、变得沮丧、过度思考的例子,三天后才发现答案就在你面前,挥舞着红旗,高喊“我在这里!!!!”

请发布试图定位视图的代码以及一些示例数据。我们需要先了解发生了什么,然后才能找出问题所在。代码是根据ughoavgfhw的请求添加的。只是猜测一下……但是
[stateMapView addCountyMap:[区域多边形]带颜色:winner translatedBy:countyOrigin]
这是原点的倒数吗?另外,看起来您可能没有对框架原点做出正确的假设,因为它们是局部原点,而不是窗口的全局原点。也就是说,您减去[mainContainerView frame].origin.y和frame.origin.y,不确定,但看起来不正确。我的理解是[view bounds]返回视图的局部坐标系,而[view frame]给出相对于superview的坐标系,在本例中,这就是我需要的坐标系。我不确定关于原点的倒数是什么。。。