Search code examples
iosscalecentercgaffinetransform

iOS - get center of object that's been scaled


I scaled my view using CGAffineTransformMakeScale and I need to find the center of it. The x scale factor == the y scale factor but the factor will change in a UIView animation

[UIView animateWithDuration:1.0f animations:^{
            self.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];

There is a method that run 30x a second and requires getting the center of the object thats scaled as well as its subview but I don't know how to get it.

Edit: I forgot to mention that the center it gets seems to get a point bottom/right of the object.

- (CGPoint)getanchorPoint {
    float x = self.center.x + (_bubble.center.x - (self.frame.size.width / 2));
    float y = self.center.y + (_bubble.center.y - (self.frame.size.height / 2));
    return CGPointMake(x, y);
}

_bubble is the subview of self. _bubble also moves around within self, and the center I want to get is the center of the bubble relative to super's coordinate system.


Solution

  • UIView and all subclasses have a center property that provides exactly this. And you can convert between the coordinate spaces of each view using UIView instance methods convertPoint:toView and convertPoint:fromView.