7
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [notificationCenter addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    orientation = AVCaptureVideoOrientationPortrait;

beginGeneratingDeviceOrientationNotifications の意味と動作方法を知りたいですか?

4

3 に答える 3

26

この行を書くとき:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

デバイスに「向きが変わるたびにアプリケーションに通知してください」と言います。

これを書くことによって:

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

deviceOrientationDidChangeアプリケーションに「方向が変更されたことを通知されるたびにメソッドを呼び出してください」と言います。

これで、deviceOrientationDidChange次のことができます。

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait)
     [self doSomething];
else
     [self doSomethingElse];
于 2013-04-14T07:41:41.413 に答える
8

スウィフト 4.2

class ViewController: UIViewController {

     override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func deviceOrientationDidChange() {
        print(UIDevice.current.orientation.rawValue)
    }
}

スイフト3.0

誰かがこれを迅速に書いてくれたらいいのにと思います。これが Obj-C の投稿であることは知っていますが、そのような迅速な投稿は見つかりませんでした。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

    }

    deinit {
         NotificationCenter.default.removeObserver(self)
    }

    func deviceOrientationDidChange() {
        print(UIDevice.current.orientation.rawValue)
    }

}
于 2017-02-06T01:44:10.240 に答える
2
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

これは、通知を受け取るためのオプションです。誰かがそれがどのように機能するかを知っているなら、共有してください。

于 2015-12-12T11:42:09.267 に答える