ios objective-c swift orientation uidevice

Detectando la orientación de UIDevice de iOS



objective-c swift (7)

Necesito detectar cuando el dispositivo está en orientación vertical para poder disparar una animación especial. Pero no quiero que mi vista se autorrotee.

¿Cómo anulo una vista de autorrotación cuando el dispositivo se gira a vertical? Mi aplicación solo necesita mostrar su vista en el paisaje, pero parece que necesito apoyar el retrato también si deseo poder detectar una rotación en vertical.


1) Versión rápida de la respuesta de David 2) En caso de que aún desee detectar la orientación cuando no hay cambio de orientación (Swift vesion de la respuesta de Moe a ¿Cómo puedo detectar la orientación del dispositivo en iOS? )

// Initial device orientation let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation if(orientation == UIInterfaceOrientation.Unknown){ // code for Unknown } else if(orientation == UIInterfaceOrientation.Portrait){ // code for Portrait } else if(orientation == UIInterfaceOrientation.PortraitUpsideDown){ // code for Portrait } else if(orientation == UIInterfaceOrientation.LandscapeRight){ // code for Landscape } else if(orientation == UIInterfaceOrientation.LandscapeLeft){ // ode for Landscape } // To detect device orientation change UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() NSNotificationCenter.defaultCenter().addObserver( self, selector: "orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: UIDevice.currentDevice())

Función orientationChanged

func orientationChanged(note: NSNotification) { let device: UIDevice = note.object as! UIDevice switch(device.orientation) { case UIDeviceOrientation.Portrait: // code for Portrait break case UIDeviceOrientation.PortraitUpsideDown: // code for Portrait break case UIDeviceOrientation.LandscapeLeft: // code for Landscape break case UIDeviceOrientation.LandscapeRight: // code for Landscape break case UIDeviceOrientation.Unknown: // code for Unknown break default: break } }


Intente hacer lo siguiente cuando la aplicación se carga o cuando se carga su vista:

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

A continuación, agregue el siguiente método:

- (void) orientationChanged:(NSNotification *)note { UIDevice * device = note.object; switch(device.orientation) { case UIDeviceOrientationPortrait: /* start special animation */ break; case UIDeviceOrientationPortraitUpsideDown: /* start special animation */ break; default: break; }; }

Lo anterior le permitirá registrarse para cambios de orientación del dispositivo sin habilitar la autorotación de su vista.

Nota

En todos los casos en iOS, cuando agrega un observador, también elimínelo en el momento apropiado (posiblemente, pero no siempre, cuando la vista aparece / desaparece). Solo puede tener "pares" de código de observar / no observar. Si no lo haces, la aplicación se bloqueará. Elegir dónde observar / no observar está más allá del alcance de este control de calidad. Sin embargo, debe tener una "inobservancia" para que coincida con el código "observar" anterior.


Primero desactive todo menos la orientación que desee (para que no gire)

Entonces, como David dijo, simplemente obtén la orientación actual del dispositivo:

https://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html

Alternativamente, puede usar el acelerómetro usted mismo (ya que es como se hace de todos modos) y verificar dónde está la gravedad para ver qué orientación tiene. Si toma este enfoque, puede jugar con los valores usted mismo para obtener resultados diferentes.


Si llegaste a esta pregunta buscando cómo detectar un cambio de orientación (sin necesariamente querer deshabilitar la rotación), también debes tener en cuenta viewWillTransitionToSize , que está disponible en iOS 8.

Swift ejemplo de here

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in let orient = UIApplication.sharedApplication().statusBarOrientation switch orient { case .Portrait: println("Portrait") // Do something default: println("Anything But Portrait") // Do something else } }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in println("rotation completed") }) super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) }

Y si no necesita preocuparse por la orientación real:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { // do something super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) }

Objetivo-C ejemplo de here

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; // do whatever } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { }]; [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; }

Y si no necesita preocuparse por la orientación real (tomada de esta respuesta ):

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { // Do view manipulation here. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; }

Ver también


Si no desea crear un objeto de dispositivo, también puede usar

-(void) seObserverForOrientationChanging { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]]; } - (void) orientationChanged:(NSNotification *)note { if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){ //Do something in landscape } else { //Do something in portrait } }


Si te entiendo correctamente, tu aplicación solo es paisajística. Simplemente puede especificar en la configuración de la aplicación que es solo horizontal y, por lo tanto, no necesita preocuparse por la rotación. La aplicación comenzará en el paisaje y permanecerá allí independientemente de cómo esté orientado el iPad.


.UIDeviceOrientationDidChange notificación se llama muchas veces en iphone incluso cuando el dispositivo no giró. No sé el motivo, pero si lo necesita solo cuando el dispositivo realmente giró, haga lo siguiente.

NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: .UIDeviceOrientationDidChange, object: nil)

El método llamado desde el observador debería verse así:

func orientationChanged() { if traitCollection.isIphone { defer { self.previousTraitCollectionForIphone = traitCollection } guard let previousTraitCollectionForIphone = previousTraitCollectionForIphone else { updateView() return } if previousTraitCollectionForIphone != traitCollection { updateView() } } else { updateView() } }