Search code examples
iosios6uiviewuiimageview

Moving an ImageView center point at the correct time


I have a center point for an UIImageView which is being updated.

At present the issue is that I see the image view move. What I am looking for is to move the imageview when the view is hidden and then show it again once the center point is updated.

I created a method that does the center point update:

-(void)updateCenterPoint {
    float newhintX = (self.originalImageCenterPoint.x * self.scaleDownOriginalImageRatio) + self.insideImageFrame.frame.origin.x;
    float newhintY = (self.originalImageCenterPoint.y * self.scaleDownOriginalImageRatio) + self.insideImageFrame.frame.origin.y;
    if (self.hintView.hidden == NO) {
        self.hintView.hidden = YES;
        self.hintView.center = CGPointMake(newhintX, newhintY);
    } else {
        self.hintView.center = CGPointMake(newhintX, newhintY);
    }

    if (self.hintView.hidden) {
        self.hintView.hidden = NO;
    }
}

Calling this in viewDidLoad does not appear to work how I expect it to. Ideally when the view loads I do not want to the see the hintView (image view) until it is in the correct point?


Solution

  • The issue was that I was updating the center point from a method:

    -(void)viewDidLayoutSubviews
    

    This is called at some point which is too early. So the center point is still 0,0 even though it is being updated.

    This meant that the view was becoming unhidden/shown before the center point had changed, therefore when the method was called again the update occurred to the centerpoint.

    The resolution was to check that the point was not 0,0 if it was keep it hidden. It is never going to be 0,0 in my example - luckily.