旋转视图时更改UIImageView位置

时间:2013-01-28 22:51:43

标签: iphone ios6 uiimageview uiinterfaceorientation

当我旋转设备时,我正试图改变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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

尝试使用viewWillAppear