简体   繁体   中英

New center point for transformed UIView with new frame

I been working on an iOS app which should display indoor blueprints. 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

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. You should instead use the center property to change the view's position and bounds.size to change the view's size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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