I used the notification to get the device's orientation.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[notificationCenter addObserver:self
selector:#selector(deviceOrientationDidChange)
name:UIDeviceOrientationDidChangeNotification
object:nil];
But the selector function is not called when the device's screen rotation is locked. Any help?
You can use accelerometer to get values of screen orientation. See this. Also see this blog
Related
I am working on iPhone game, i was curious to know if there is a call back function tht gets called when the device forces auto rotation on the game, so that i can change the HUD elements.
Or shd i run infinite loop that checks if the app has rotated in a different thread? problem is i dont think this is an effective way. Does anyone have any good solution for this.
All you have to do is this:
In your didfinishlaunching write this
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification object:nil];
and then copy the following callback method
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(#"Landscape Left!");
self.isLandscapeLeft=1;
}else if(orientation == UIDeviceOrientationLandscapeRight){
NSLog(#"Landscape Right!");
self.isLandscapeLeft=0;
}
}
Likewise you can check orientation for potrait mode,face-up,face-down,upside down and landscapeleft and landscape right orientation.:)
Yes there is, as long as the device is not orientation locked you can register to listen to
UIDeviceOrientationDidChangeNotification
which can be seen here http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
If you want to overcome the possibility of the device being orientation locked, then you have to manually monitor the accelerometer of the device.
In a game where orientation is essential, manually monitoring is recommended, since the first method has a little delay.
I have a UIWebView in which sometimes inside of that it has a youtube player for videos. Users can see the video in full screen as well. However the issue is that when the user sees the video in full screen, then rotates it, it doesn't call willAnimateToInterfaceOrientation. So I am planning on calling this manually. The question is how do I check whenever someone has dismissed the youtube video?
Try registering yourself for UIDeviceOrientationDidChangeNotification in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification" object:nil];
- (void) didRotate:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Handle rotation
}
I have an issue when playing video while using the c "willRotateToInterfaceOrientation" function. Initially, I have my main view set on the landscape mode. The video is playing but then I tried to rotate it to portrait mode my project doesn't call the "willRotateToInterfaceOrientation" but when I tried to rotate it back to the landscapeleft mode and then rotate back again to the portrait mode it's just the time the "willRotateToInterfaceOrientation" is called. can anyone please help me? thanks
Try this instead of implementing code in willRotateToInterfaceOrientation.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter* notifCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:#selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Add the following method
-(void) orientationChanged
{
//Copy code from your willRotateToInterfaceOrientation method here.
}
Is there a delegate that will get called when the iPhone enters landscape or portrait mode? I need to change the style and place objects in a different place when the iPhone get's rotated. Do I have to do this with the accelerometer? Moreover if there exist such a delegate do I have to create the connection in interface builder. I am new to objective-c...
Register to listen for the orientation change notification.
UIDevice *device = [UIDevice currentDevice];
//Tell it to start monitoring the accelerometer for orientation
[device beginGeneratingDeviceOrientationNotifications];
//Get the notification centre for the app
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification
object:device];
Implement orientationChanged: method, which will be called when the device change the orientation. you could put code to check the orientation type and called your method.
- (void)orientationChanged:(NSNotification *)note
{
NSLog(#"Orientation has changed: %d", [[note object] orientation]);
}
Remove notification in dealloc.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Check the blog post
Reacting to iPhone's orientation
Implement didRotateFromInterfaceOrientation in your view controller
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
You can get UIDevice to generate notifications for orientation events. See the documentation for UIDevice.
If you need to detect the change at any moment, you might consider calling -beginGeneratingDeviceOrientationNotifications in your app's delegate.
Also, if you are using UIViewControllers, there are
shouldAutorotateToInterfaceOrientation:
willRotateToInterfaceOrientation: duration:
didRotateFromInterfaceOrientation:
I know that I can return YES to support Landscape mode in shouldAutorotateToInterfaceOrientation. But I would like to display a new view, when the user turn the device into landscape mode, how can I do so? thank you.
Observer this event UIDeviceOrientationDidChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationDidChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
A useful technique for just this case is, to present a new modal view controller with a "fade" transition from your current view. You can do this in reaction to UIDeviceOrientationDidChangeNotification as mentioned previously by Aaron.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];