centering a view in another with animation

Discussion in 'iOS Programming' started by JGoose, Apr 19, 2010.

  1. JGoose macrumors member

    Joined:
    Feb 12, 2010
    #1
    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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #3
    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.



    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];
     
  4. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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.
     
  6. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #6
    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
     

Share This Page