Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JGoose

macrumors member
Original poster
Feb 12, 2010
41
0
I simply want to center an image view in its superview. Then animate it to another point.

When I put just this it centers the image just fine.
Code:
CGPoint middle = CGPointMake(bounds.size.width / 2.0, bounds.size.height / 2.0);

UIImageView *buttonImage = [[UIImageView alloc] initWithImage: image];
		[self addSubview: buttonImage];
		buttonImage.center = middle;

but when I add the animation the image starts off center a little up and to the left.

Code:
for (int arrayPointer = 0; arrayPointer != (pointsForPolygon.count); arrayPointer++) 
	{
		
		
		NSValue *theValue =[pointsForPolygon objectAtIndex:arrayPointer];
		CGPoint thePoint = [theValue CGPointValue];
		
		UIImageView *buttonImage = [[UIImageView alloc] initWithImage: image];
		[self addSubview: buttonImage];
		buttonImage.center = middle;
		
		
		[UIView beginAnimations: @"moveAnimation" context: nil];
		[UIView setAnimationDelegate: self];
		[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
		[UIView setAnimationDuration:10.];	
		[UIView setAnimationDelegate:self];
		
		CGRect imageSize = buttonImage.frame;
		imageSize.size.width /= 10;
		imageSize.size.height /= 10;
		buttonImage.frame = imageSize;
		buttonImage.center = thePoint;
		
		[UIView setAnimationDidStopSelector:@selector(moveAnimationDidStop:finished:context:)];
		[UIView commitAnimations];
		[buttonImage release];
		
	}

It's is a for loop because a copy of the image is moving out to each vertex of a polygon.
 
I just want to make clear that the above code works if I take out the animation. it centers the images. some how the animation is affecting it. I just don't get how.



The easiest way to achieve this is:
Code:
buttonImage.center = superview.center;

Anyways, to better troubleshoot this, we'd have to know how you set bounds.

I have already tried that, but it give the same exact results as what I posted.

I set bounds with:
Code:
CGRect bounds = [self bounds];
 
I just got it!

I changed

Code:
buttonImage.frame = imageSize;


to

Code:
buttonImage.bounds = imageSize;


this is because frame stores the coordinates of the subview in the superview, not just its size.
 
Glad you got your issue figured out. I just wanted to point out that there is no need to calculate the center of your superview when it already knows it.
 
ya, I know. that is not what I originally did. I did that just to try something different. I was just trying to troubleshoot and I didn't change it back before posting it.

thanks anyway
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.