0

当我旋转设备时,我正在尝试更改 iPad 中 UIImageView 的位置,但我不明白为什么有时会改变而有时不会改变,我还想检测视图何时开始方向是什么所以我可以设置正确的位置,但不起作用,这是代码:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"%d",toInterfaceOrientation);
    [self performSelectorOnMainThread:@selector(changeShelfPosition:) withObject:[NSNumber numberWithInt:toInterfaceOrientation] waitUntilDone:YES];
}

-(void)changeShelfPosition:(NSNumber *)orientation
{
    if (orientation.integerValue == 1 ) {

        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 544, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);

    } else if (orientation.integerValue == 2) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 544, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    } else if (orientation.integerValue == 3) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 594, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    } else if (orientation.integerValue == 4) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 594, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
}

- (void)viewDidLoad
{
...
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == 0) {

    }//Default orientation
        //UI is in Default (Portrait) -- this is really a just a failsafe.
    else if(orientation == UIInterfaceOrientationPortrait) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
            //Do something if the orientation is in Portrait
    else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y+50, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
                // Do something if Left
    else if(orientation == UIInterfaceOrientationLandscapeRight) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y+50, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
}
4

1 回答 1

0

您无法访问框架或其他几何属性viewDidLoad并期望获得一致的结果,因为尚未设置视图几何。

试试viewWillAppear吧。

于 2013-01-28T23:02:14.537 回答