1

I have an app, it runs in landscape mode, I hold it in landscape mode when debugging on my phone. I pick up the center value from the UIView on my main view controller, I need this to put items in the center of the screen, and since it is universal I need this to be variable and not set to iPhone screen sizes.

When I do this and read the x and y from the CGPoint returned by view.center I get x = 170 y = 240

The delegate is the one viewcontroller in my app, the center function is one of the object I want to move to the center.

- (void) center
{
    CGPoint center = ((UIViewController*)delegate).view.center;
    CGSize size = self.frame.size;
    double x = center.x - size.width/2 - location.x;
    double y = center.y - size.height/2 - location.y;

    [self _move:1.0 curve:UIViewAnimationCurveEaseInOut x:x y:y];
}

- (void)_move: (NSTimeInterval)duration
            curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
    self.transform = transform;

    // Commit the changes
    [UIView commitAnimations];

}

Now to me this does not make much sense.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ophychius
  • 2,623
  • 2
  • 27
  • 49
  • possible duplicate of [UIView width and height reversed](http://stackoverflow.com/questions/8141278/uiview-width-and-height-reversed) – jrturton Nov 18 '11 at 10:43

1 Answers1

6

Use bounds, not frame. Frame is in the superview's coordinate system which for your root view is the window - this does not rotate in different orientations.

Bounds is in the views own coordinate system so is the appropriate rect to use when setting center on subviews, since center is also expressed in the superview's coordinate system.

jrturton
  • 118,105
  • 32
  • 252
  • 268