0

横向きで起動したいインターフェースがあります。ユーザーがデバイスを縦向きに回転させたときに起動した後、日表示のカレンダーを表示しています。横向きに戻ると、カレンダーは閉じられます。私のアプリケーション ユーザー インターフェイスは横向きで正しく表示され、カレンダーは縦向きで正しく表示され、すべてがどの向きでもうまく機能します。

問題は、ユーザーが起動時に iPhone を横向きに持っている場合です。何をしても、ランドスケープモードのユーザーインターフェイスで起動できません。私の UIDeviceOrientationDidChangeNotification メソッドは 2 回起動します。1 回目は [UIDevice currentDevice].orientation が横向きで、2 回目は縦向きです。最終的な結果として、ユーザー インターフェイスが縦向きモードに回転し、日ビューが表示されます。私が欲しいものではありません。ユーザーが物理的にデバイスを横向きから縦向きに回転させるまで、ユーザー インターフェイスを横向きのままにしておきたいです。

ユーザーがデバイスを縦向きに保持しているときに、横向き [UIDevice currentDevice].orientation で起動する理由がわかりません。

これが私のコードがviewControllerでどのように見えるかです...

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

    if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {            
        return NO;
    } 

    return YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
        showingCalendar = NO;
        initializing=YES;
        [[UIDevice currentDevice]    beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

}

-(void)didRotate:(NSNotification *)notification {
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;  
    if ((deviceOrientation == UIDeviceOrientationPortrait) || (deviceOrientation == UIDeviceOrientationPortraitUpsideDown))  {            
        if ((!showingCalendar) && (!initializing)) {
            showingCalendar = YES;
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];            
            GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease];
            navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
            [self presentModalViewController:navigationController animated:YES];
        }
    }else if ((deviceOrientation == UIDeviceOrientationLandscapeRight) || (deviceOrientation == UIDeviceOrientationLandscapeLeft)) {
        if (showingCalendar) {
            showingCalendar = NO;
            if (deviceOrientation == UIDeviceOrientationLandscapeRight){
                [self dismissModalViewControllerAnimated:YES];
            }else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
                [self dismissModalViewControllerAnimated:YES];
            }
        }else {
            initializing = NO;  
        }            
    }
}
4

2 に答える 2

1

問題の回避策を見つけました。viewDidLoadで、scheduledTimerWithTimeIntervalを開始し、beginGeneratingDeviceOrientationNotificationsをセレクターメソッドに移動しました。

これで、通知が2回以上発生することはありません。ユーザーは、デバイスがどちらの方向に保持されていても、起動時に横向きになり、起動後はすべての回転が完全に機能します。

これが私の変更したコードです。他のすべては同じままでした...

- (void)viewDidLoad {
    [super viewDidLoad];
    showingCalendar = NO;
    initializing=YES;
    timer =  [NSTimer scheduledTimerWithTimeInterval:.55 target:self selector:@selector(startOrientationNotifications) userInfo:nil repeats: NO];

}


-(void)startOrientationNotifications {
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

}
于 2012-03-01T19:20:58.727 に答える
0

beginGeneratingDeviceOrientationNotificationsを生成しません。

簡単な方法は、BOOLを使用して、shouldAutorotateToInterfaceOrientationでポートレートが許可されているかどうかを確認することです。

このようなもの:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

    if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {   

        return portraitIsAllowed;
    } 

    return YES;
}

次に、他の方法で必要なときに変更します。

また、shouldAutorotateToInterfaceOrientationは、ユーザーがデバイスを回転させるたびに、またコントローラーを最初にロード(インスタンス化)するときにも呼び出されることに注意してください。

于 2012-03-01T10:29:04.340 に答える