0

スプラッシュ画面から始まり、ログイン画面に移動する iPad アプリに取り組んでいます。私のアプリは、すべての向きと iOS 4.3 以降をサポートする必要があります。そのために、plist に 4 つの向きを追加し、アプリ デリゲートに次のコードを追加しました。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self.window makeKeyAndVisible]; 
    // Override point for customization after application launch
    SplashViewController *aController = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
    [window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
    //[window addSubview:[mainViewController view]];
    [window setRootViewController:mainViewController];

    return YES;
}

- (NSUInteger)application:(UIApplication *)application     supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    return UIInterfaceOrientationMaskAll;
}

スプラッシュ画面で

- (void) loadView {
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}
- (BOOL)shouldAutorotate{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Portrait");
    else
            NSLog(@"Landscape");

}

ここでは正しい向きを取得しますが、回転すると逆の結果が得られます。縦向きの場合は横向き、横向きの場合は縦向きになります。私はこのようなコードを変換しようとしました:

- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Landscape");
    else
            NSLog(@"Portrait");

}

最初の結果は間違っていますが、その後は正しい結果になります。手伝ってくれますか?テストするいくつかの uielement を配置しましたが、テストの結果は同じであることに注意してください。ありがとうございました

4

1 に答える 1

0

2日間の検索とテストの後、なんとかこの問題を解決できましたが、フォーラムの回答はどれも完全ではありませんでした。私が解決した方法は次のとおりです。

//this for the orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self orientationChanged];
}

// here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self StartUpOrientation];
}
#pragma mark -
#pragma mark Orientation Methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;

}

- (void) orientationChanged {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
    else {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        //your code here
    }
}

- (void) StartUpOrientation {
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        [self.view setBounds:CGRectMake(0, 0,768,1004)];
        // your code here
    }
else {
        [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
        // your code here
    }
}

それがいつか誰かを助けることを願っています

于 2013-02-13T17:00:59.670 に答える