简体   繁体   English

具有新框架的转换后的UIView的新中心点

[英]New center point for transformed UIView with new frame

I been working on an iOS app which should display indoor blueprints. 我正在开发应显示室内设计图的iOS应用程序。 You should be able to switch between floors and each floor image is controlled by gesture recognisers to handle pan, rotate and scale. 您应该能够在楼层之间切换,并且每个楼层图像都由手势识别器控制以处理平移,旋转和缩放。

I have been using this example for the gesture recognisers: https://github.com/GreenvilleCocoa/UIGestures/blob/master/UIGestures/RPSimultaneousViewController.m 我一直在为手势识别器使用此示例: https : //github.com/GreenvilleCocoa/UIGestures/blob/master/UIGestures/RPSimultaneousViewController.m

So now to the problem. 所以现在要解决问题。 Whenever the user switch floor I want to keep the transformation of the image as well as the corresponding center lat/lng. 每当用户切换地板时,我都希望保持图像的变换以及相应的中心经度/纬度。 However, the new image can have another rotation offset and aspect ratio. 但是,新图像可以具有另一个旋转偏移和宽高比。

I have been able to update the new frame of the image with the new size and update the transform with the new rotation offset and verified it. 我已经能够以新的尺寸更新图像的新框架,并以新的旋转偏移量更新变换并进行验证。 It is when I try to calculate the new center point I can not get it to work. 当我尝试计算新的中心点时,我无法使其正常工作。 The following code is how I currently do it and it works as long as the view is not rotated: 以下代码是我当前的操作方式,只要视图未旋转,它就可以工作:

-(void)changeFromFloor:(int)oldFloorNr toFloor:(int)newFloorNr
{
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

    // This is the old non transformed center point.
    CGPoint oldCenterOnImage = [self.layer convertPoint:centerPoint toLayer:self.mapOverlayView.layer]; // Actual non transformed point

    // This point is verified to be the corresponding non transformed center point
    CGPoint newCenterOnImage = [self calculateNewCenterFor:oldCenterOnImage fromFloor:oldFloorNr toFloor:newFloorNr];

    // Change image, sets a new image and change the fram of mapOverlayView
    [self changeImageFromFloor:oldFloorNr toFloor:newFloorNr]

    // Adjust transformed rotation on map if new map have different rotation
    [self adjustRotationFromFloorNr:oldFloorNr toFloorNr:newFloorNr];

    CGPoint centerOfMapOverlay = CGPointMake((self.mapOverlayView.frame.size.width / 2), (self.mapOverlayView.frame.size.height / 2));

    CGPoint newCenterOnImageTransformed = CGPointApplyAffineTransform(newCenterOnImage, self.mapOverlayView.transform);

    CGFloat newCenterX = centerPoint.x + centerOfMapOverlay.x - newCenterOnImageTransformed.x;
    CGFloat newCenterY = centerPoint.y + centerOfMapOverlay.y - newCenterOnImageTransformed.y;

    // This only works without any rotation
    self.mapOverlayView.center = CGPointMake(newCenterX, newCenterY);
}

Any idea where I go wrong? 知道我哪里出错了吗? I have been working with this problem some days now and I can not seem to figure it out. 我几天来一直在处理这个问题,但似乎无法解决。

Please let me know if I need to add something or if something is unclear. 请让我知道我是否需要添加某些内容或不清楚的内容。

Thanks! 谢谢!

Code added after help was given: 提供帮助后添加的代码:

CGPoint centerOfMapOverlay = CGPointMake(
    (self.mapOverlayView.bounds.size.width / 2,
    (self.mapOverlayView.bounds.size.height / 2)
);

centerOfMapOverlay = CGPointApplyAffineTransform(
    centerOfMapOverlay,
    self.mapOverlayView.transform
);

If you change the transform on it's view then the frame property becomes undefined. 如果更改视图的变换,则frame属性将变为未定义。 You should instead use the center property to change the view's position and bounds.size to change the view's size. 您应该改为使用center属性来更改视图的位置,并使用bounds.size来更改视图的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM