iosobjective-cuiviewcgpoint

Having trouble converting point that's on a rotated view with a (0, .5) anchor point


I have the following view set up:

enter image description here

Where the anchor point for V3 is (0, .5) and it can be rotated by a UIPanGestureRecognizer that's attached to V4. I'm trying to translate a point on V4 to V1s coordinate space (so I can attach a subview to V1 that appears over the center of V4) and I can't get it to place correctly. I have the following code:

CGSize targetImageViewSize = CGSizeMake(v4.frame.size.width / 2, v4.frame.size.height / 2);
//find the point that will place the target in the center of v4 when attached as a subview to v1 (i.e. in its center)
CGPoint convertedTargetImageViewOrigin = [v1 convertPoint:CGPointMake((v4.frame.size.width - targetImageViewSize.width) / 2, (v4.frame.size.height - targetImageViewSize.height) / 2) fromView:v4];
UIImageView *targetImageView = [[UIImageView alloc] initWithFrame:CGRectMake(convertedTargetImageViewOrigin.x, convertedTargetImageViewOrigin.y, targetImageViewSize.width, targetImageViewSize.height)];
targetImageView.image = [UIImage imageNamed:@"TargetIcon"];
targetImageView.transform = CGAffineTransformMakeRotation(v3.degreesRotated);
[v1 addSubview:targetImageView];

I've also tried rotating the point to no avail:

convertedTargetImageViewOrigin = CGPointApplyAffineTransform(convertedTargetImageViewOrigin, CGAffineTransformMakeRotation(v3.degreesRotated));

To be clear, I don't this target to appear over v4 while the user is rotating, just briefly when the user taps v1. Any help as to why I can't get the correct point would be appreciated, thanks.


Solution

  • HangarRash pointed me in the right direction by suggesting that I use v4's center property instead of manually calculating the origin. After that, I just had to adjust for the fact that v4's center property is in terms of v3's coordinate space when using convertPoint:fromView, and then it worked!

    CGSize targetImageViewSize = CGSizeMake(v4.frame.size.width / 2, v4.frame.size.height / 2);
    CGPoint targetImageViewCenter = v4.center;
    CGPoint convertedTargetImageViewCenter = [v1 convertPoint:targetImageViewCenter fromView:v3];
    
    UIImageView *targetImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, targetImageViewSize.width, targetImageViewSize.height)];
    targetImageView.image = [UIImage imageNamed:@"TargetIcon"];
    targetImageView.center = convertedTargetImageViewCenter;
    [self.targetImageViews addObject:targetImageView];
    targetImageView.transform = CGAffineTransformMakeRotation(v3.angle);
    [v1 addSubview: targetImageView];