Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to catch an WillRotateToInterfaceOrientation event from an UIView?

every UIViewController has a method called willRotateToInterface.

Is it possible to do this within a UIView too?

Does this match the idea of model view controller ?

The only way I can think of is to send the event from the UIViewController to the UIView.

Is there a global variable for the current orientation?

like image 317
jantimon Avatar asked Sep 01 '09 15:09

jantimon


3 Answers

Observe UIDeviceOrientationDidChangeNotification:

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

...

- (void)orientationDidChange:(NSNotification *)note
{
    NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
}

UIDevice Class Reference

I should note that yo uneed to add -beginGeneratingDeviceOrientationNotifications when you want these notifications to be sent, and call -endGeneratingDeviceOrientationNotifications when you want them to stop. There is a battery impact of generating these, so you should only do so when your view is on screen. UIViewController does all this for you, so if you have a view controller, it is worth letting it do the work.

like image 119
Rob Napier Avatar answered Nov 15 '22 00:11

Rob Napier


If you just want to adjust view to new size/layout when orientation changes, you should just override its layoutSubviews method.

It will be called whenever size of the view changes, which usually happens when view is rotated.

like image 3
Kornel Avatar answered Nov 14 '22 22:11

Kornel


You can subscribe to a global notification and get a call when the device is rotated, it wont do anything for you though..

[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
like image 2
Daniel Avatar answered Nov 14 '22 23:11

Daniel