0

ユーザーが画面を横向きまたは縦向きに回転させたときに通知を受け取りたいのですが、可能ですか?

いくつかの記事を見つけましたが、これに対する答えが見つかりませんでした。

4

2 に答える 2

3

デバイスが回転したときに通知shouldAutorotateToInterfaceOrientation:を受け取りたい場合は、View Controller にメソッドを実装するか、UIDeviceOrientationDidChangeNotification.

始める前に、デバイスの向きとインターフェイスの向きは異なる場合があります。アプリの作成方法によっては、デバイスが横向きでもインターフェースが縦向きのままになる場合があります。デバイス通知は、インターフェイスの向きが変更されてデバイスの向きに一致する直前に送信されます。インターフェイスの向きを変更したくない場合はshouldAutorotateToInterfaceOrientation:、View Controller に return を返すメソッドを実装する必要がありますNO。これにより、インターフェイスの向きの更新が停止します。

ご質問のとおり、通知を受け取りたいようですので、2 番目の方法を使用したいと思います。UIDeviceOrientationChangeNotification次を使用してを有効にできます。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

対応するものがあります:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

を使用して通常の方法で通知を受信するようにNSNotificationCenter登録できますUIDeviceOrientationDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

最後に、通知を受け取ったときに呼び出されるメソッドを次のように実装します。

- (void)orientationChanged:(NSNotication *)notification {

    UIDeviceOrientation = [[UIDevice currentDevice] orientation];

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

        // Portrait

    } else {
        // Landscape
    }

}

ご覧のとおり、向きには の orientationインスタンス メソッドを使用してアクセスできますUIDevice

于 2012-04-15T09:41:55.833 に答える