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 に答える