Rotating landscape mode - iphone

what i did is displaying 10 images as grid and 3 images in portroide mode, and what i did is when ever i rotate the simulator to landscape then i have to display 4 images.It is also displayed using the code
if(self.interfaceorientation == UIIntefaceorientationPortrait) {
[self abc];
else {
[self abclandscape];
}
here abc and abclandscape are two functions it works fine but, it works from initial means form starting if i rotate to landscape mode or portrait mode it works fine .while in the middle if i rotate from landscape to portrait it does 't goes to [self abc] function. how can i solve this?

What you could do is either use the UIViewController delegates, or use the NSNoticationCenter.
I.E. add in your "viewDidLoad":
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
And add the function:
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
// Do one thing
}
else
{
// Do something else
}
}

#mahesh In shouldAutoRototate Method
use
if(self.interfaceorientation == UIIntefaceorientationPortrait||self.interfaceorientation == UIIntefaceorientationPortraitUp..)
{
}
else
{
}

Related

In iOS, how to move UI elements when a device rotate?

In iOS, how to move UI elements (buttons and labels) when a device rotate to landscape mode from portrait mode?
There are so many ways.
In my case, I'm using notification
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(rotated:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
In rotated method
-(void) rotated: (NSNotification*) notification
{
orientation = [[UIDevice currentDevice] orientation];
if(UIDeviceOrientationPortraitUpsideDown==orientation)
// UI relocation
else if(UIDeviceOrientationPortrait==orientation)
// UI relocation
else if(UIDeviceOrientationLandscapeRight==orientation)
// UI relocation
else if(UIDeviceOrientationLandscapeLeft==orientation)
// UI relocation
}

How to allow only particular view to rotate? (iPhone)

I am installing AdMob's ad, and there is a GADBannerView.
After installing, a banner show, and if you click on it, a page will slide out coving the whole screen, and displaying advertising contents in it.
The question is, some advertising contents, such as video, had to be played landscape. However, I don't want other part of my application to rotate, as the app is not designed to be viewed in landscape.
So, how can I implement something which can achieve such function?
Try to use Notification for this. a notification calls a selector every time when ur device orientation is changed.
write this in your viewDidLoad:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(setScreenWithDeviceOrientation:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
and then define the selector as follows:
-(void)setScreenWithDeviceOrientation:(NSNotification *)notification
{
UIDeviceOrientation orientation=[[UIDevice currentDevice] orientation];
if(orientation==UIInterfaceOrientationPortrait) //Portrait orientation
{
// setView frame for portrait mode
}
else if(orientation==UIInterfaceOrientationPortraitUpsideDown) // PortraitUpsideDown
{
// setView frame for upside down portrait mode
}
else if(orientation==UIInterfaceOrientationLandscapeLeft)
{
// setView frame for Landscape Left mode
}
else if(orientation==UIInterfaceOrientationLandscapeRight) //landscape Right
{
// setView frame for Landscape Right mode
}
else
{
NSLog(#"No Orientation");
}
}
this method fired everytime when ur device changes orientation. Based on the current orientation you should adjust your view.
I hope this will help you.
Are you working with iOS 6? You should be able to just restrict what orientations your view controller handles in this case. For example, in your view controller that handles your GADBannerView, you can just put:
// Tell the system what we support
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return NO;
}
// Tell the system which initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
And that should make it so that your viewcontroller only supports portrait.
Have you looked at this question and answer? This explains how a single view can work in a certain orientation that is not supported by the rest of the application:
AutoRotate ONLY MpMoviePlayerControler iOS 6

UIDeviceOrientationFaceDown in iphone

I want to detect UIDeviceOrientationFaceDown. How can i do this???
Actually i want to perform some action when my iphone face is down on flat surface.
how is it possible??? plz help me to do this.
i have this code, but don't know where and how apply this code
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(#"will receive orientation notifications: %#", getOrientationUpdates?#"YES":#"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
if there is any tutorial then please suggest me that
Thanks in Advance and most welcome to your precious help and suggestions.
You can accomplish this by detecting the ProximityState of iPhone. Using the [UIDevice currentDevice] singleton, setting the proximityMonitoringEnabled to YES. you can access the proximity information through the proximityState property.
[[UIDevice currentDevice]proximityState];
iPhone has a sensor turns off the screen when you put it on your ear during a call, AFAIK that is an infrared sensor. and you can access it.
EDIT:
You can also accomplish it using the below code. UIDeviceOrientationFaceDown
The device is held parallel to the ground with the screen facing downwards. (regardless if it touched any object) if you want to know if the iPhone touched an object, detect the proximity state of device.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(detectOrientation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)detectOrientation;{
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationPortrait:
{
NSLog(#"portrait");
}
break;
case UIDeviceOrientationPortraitUpsideDown:
{
NSLog(#"portraitUpSideDown");
}
break;
case UIDeviceOrientationLandscapeLeft:
{
NSLog(#"landscapeLeft");
}
break;
case UIDeviceOrientationLandscapeRight:
{
NSLog(#"landscapeRight");
}
break;
case UIDeviceOrientationFaceDown:
{
NSLog(#"facedown!!");
}
break;
default:
break;
}
}
}
EDIT: to answer the question in comment. add this line in your viewDidLoad
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil];
then write a method
-(void)handleProximityChangeNotification{
if([[UIDevice currentDevice]proximityState]){
NSLog(#"...");
}
}
You can use the z value from accelerometer sensor (-1 <= z <= 1). If your device is facing down, the z value will be in 0 < z <=1 (0 when it's facing parallel to the ground, 1 when it's facing perpendicular to the ground). To get this data you can use CMMotionManager
#import <CoreMotion/CoreMotion.h>
CMMotionManager *cm=[[CMMotionManager alloc] init];
cm.deviceMotionUpdateInterval=0.2f;
[cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *data, NSError *error) {
if(data.gravity.z>=0.3f)//
{
//DO something
}
}];
Don't forget to add CoreMotion framework to your project.

Notification about iphone oriention

I want to get notification when a user is rotate the screen to landscape or portrait,
it is possible?
I find couple of article but i didn't found answer for this.
If you want to be notified when the device has been rotated you can either implement the shouldAutorotateToInterfaceOrientation: method in your view controller or you can register to receive the UIDeviceOrientationDidChangeNotification.
Before we start, the device orientation and the interface orientation can be different. The device may be landscape but the interface may remain portrait depending on how the app has been written. Device notifications are sent shortly before the interface orientation is changed to match the device orientation. If you don't want the interface orientation to change you should implement the shouldAutorotateToInterfaceOrientation: method in your view controller to return NO. This will stop the interface orientation being updated.
From your question it sounds like you want to receive notifications so I think you want to use the second method. You can enable UIDeviceOrientationChangeNotifications using:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
There is a corresponding:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
You can register to receive the notifications in the normal way, using the NSNotificationCenter and registering to receive the UIDeviceOrientationDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Finally, you would implement the method to be called when the notification is received as follows:
- (void)orientationChanged:(NSNotication *)notification {
UIDeviceOrientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait ||
orientation == UIDeviceOrientationPortraitUpsideDown) {
// Portrait
} else {
// Landscape
}
}
As you can see, the orientation can be accessed using the orientation instance method of UIDevice.
You need to add local notification in the landscape orientation in shouldAutorotateToInterfaceOrientation method
try like this:
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationLandscapeLeft)
{
here schedule your local notification
}

UIDevice Orientation

I have the following code in a method. When I run this in the simulator the debugger skips right over the code?? What am I missing?
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight))
{
} else {
}
The best way to determine interface orientation is to look at status bar orientation:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown) {
//Portrait orientation
}
if(orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft) {
//Landscape orientation
}
UIDevice class measures orientation based on accelerometer and if device lays flat, it won't return the correct orientation.
Note that there's a macro UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait, so instead of comparing it separately to LandscapeLeft and LandscapeRight you could just do it like this:
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
}
Update 2
This shouldn't matter, but try turning on orientation notifications:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(detectOrientation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Update
My bad, I assumed it was empty.
Try removing the or statement and just test for a single orientation. See if that fixes it. Maybe there is a bracket problem or something silly.
I have the following test working in production code, so your technique should work:
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
}
Original Answer
You have to actually put statements in the if blocks to get it to step in.
The debugger is smart enough to skip over empty blocks.
Another way of doing this without turning on orientation notification would be to
Step 1: Save the current orientation in a local variable myCurrentOrientation and assign it like this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
myCurrentOrientation = toInterfaceOrientation;
}
Step 2: Use myCurrentOrientation for your check
if (UIInterfaceOrientationIsLandscape(myCurrentOrientation) == YES) {
// landscape
}
else {
// portrait.
}
Say you are inside a Springboard tweak and want to show something depending on the orientation of the current app, then you can use this (jailbreak only):
UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation];
Heh you need to call [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]
before obtaining the value. Have a look at documentation of this method. Took me a while to track this down.
I recommend you to use my highlighted code instead of yours to safe some code of lines.
-(void) viewDidLoad
{
[super viewDidLoad];
[self rotations];
}
-(void)rotations
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
-(void) orientationChanged:(NSNotification *)notification
{
//USE THIS PART
//USE THIS PART
//USE THIS PART
//USE THIS PART
//USE THIS PART
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
}
}
INSTEAD OF
if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait ||
[[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown)
{
}
Here is a method to find the orientation and the true center of the screen. I used Tuszy's method so I could set UIActivityIndicatorView properly.
- (BOOL) isPortraitOrientation {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown) {
return true;
}
if(orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft) {
return false;
}
return false;
}
And the way to get center...
- (void) findMyUIViewCenter {
CGPoint myCenter;
if ([self isPortraitOrientation]) {
myCenter = self.view.center;
}
else {
myCenter = CGPointMake(self.view.frame.size.height / 2.0, self.view.frame.size.width / 2.0);
}
NSLog(#"true center -- x:%f y:%f )",myCenter.x,myCenter.y);
}