与坐标略微混淆

时间:2013-07-26 08:30:25

标签: objective-c

您好我有这样的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];         

    // Get view bounds
    CGRect bounds = [self.view bounds];

    // Get center point
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width/2.0;
    center.y = bounds.origin.y + bounds.size.height/2.0;


    NSLog(@"center y = %f", center.y);



}

上述日志最初打印274.0

然后我有这个方法:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x
                                         duration:(NSTimeInterval)duration
{

    CGRect bounds = [[self view] bounds];

    // Get center point
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width/2.0;
    center.y = bounds.origin.y + bounds.size.height/2.0;


    // If the orientation is rotating to Portrait mode...
    if (UIInterfaceOrientationIsPortrait(x))
    {
         NSLog(@"In rotation, center y = %f", center.y);      

    }


}

上述方法中的center.y计算方法与viewDidLoad中的方法相同,您可能会注意到。但是,当手机首先旋转到横向然后再回到纵向时,现在上面方法中的center.y始终打印230(而不是最初的274.0) - 为什么? 可能导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:0)

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    CGRect bounds = [[self view] bounds];
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width/2.0;
    center.y = bounds.origin.y + bounds.size.height/2.0;
    if (UIInterfaceOrientationIsLandscape(fromInterfaceOrientation)){
        NSLog(@"In rotation, center y = %f", center.y);
    }
}