Detecting device rotation

Discussion in 'iPhone/iPad Programming' started by rand0m3r, May 20, 2008.

  1. macrumors regular

    Joined:
    Jun 30, 2006
    Messages:
    113
    #1
    right now i'm doing this:

    UIDevice myDevice = [UIDevice currentDevice];
    [myDevice beginGeneratingDeviceOrientationNotifications];
    UIDeviceOrientation currentOrientation = [myDevice orientation];

    but it doesn't detect an iphone that is rotated (in the simulator)? how do i actually pick up the notifcations?
     
  2. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Messages:
    981
    Location:
    Austin, TX
    #2
    There is a ton of sample code for iPhone that covers all of this fundamental stuff.

    Try looking in the iPhone dev site. The sample code are complete working solutions covering various topics so you don't have to ask a forum on a Mac rumors web site.
     
  3. macrumors regular

    Joined:
    Jun 30, 2006
    Messages:
    113
    #3
    i've looked and it doesn't work, which is why i'm here fool.
     
  4. macrumors 6502

    Joined:
    Jan 16, 2008
    Messages:
    319
    Location:
    Nottingham, England
    #4
    I've just tested on beta 5 and the notification definitely is generated in the simulator.

    Here's my code. Out of interested, I added this to the WhichWayUp sample so I could see the crate turning (which will help you test it on real hardware).

    Code:
    - (void) didRotate:(NSNotification *)notification
    {	
       UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    
       if (orientation == UIDeviceOrientationLandscapeLeft)
       {
           NSLog(@"Landscape Left!");
       }
    }
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    
       [window addSubview:crateViewController.view];
    	
       [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    	
       [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(didRotate:)
          name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    } 
    
    That is in the AppDelegate class. Let us know how you get on!

    P.S. I don't think the samples cover using the notification (WhichWayUp seems to use some built-in UIView magic - the rotation appears to happen automatically in the View).
     
  5. macrumors regular

    Joined:
    Feb 27, 2006
    Messages:
    243
    Location:
    Kent, United Kingdom
    #5
    When you come to a forum to ask for help, you might want to think about being polite to people who are trying to help you and not be such a cock.
     
  6. macrumors newbie

    Joined:
    Apr 12, 2008
    Messages:
    28
    #6
    Minor change

    There is already a predefined constant for the device orientation notification, which you should use in place of the literal string in case Apple decides to change it in the future.

    Code:
       [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(didRotate:)
          name:[B]UIDeviceOrientationDidChangeNotification[/B] object:nil];
    } 
    
     
  7. macrumors newbie

    Joined:
    Jun 26, 2008
    Messages:
    28
    #7
    Problem when Rotating

    Hi all,

    I am implementing orientation in my application view. But when my device rotate to left or right with that my image is also rotates but that time a blank black view is display in 1/4 part of the screen, i don't know why.
    I am just doing
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return YES;
    }


    I am attaching the screen which i am getting after rotation, in that the left corner black frame is displaying after rotation.


    Please help me how can i solve this problem.


    Thanks in Advance,
    Deepak
    www.umundo.com
     

    Attached Files:

  8. macrumors member

    Joined:
    Apr 9, 2007
    Messages:
    45
    #8
    Have you/anyone got the answer for the same problem.
    Please let me know.
    Thanks
     
  9. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    Messages:
    2,675
    #9
    You need to set the autoResizeMask so that the view is resized correctly when its superview is resized. Set flexibleWidth and flexibleHeight for the view.
     
  10. macrumors member

    Joined:
    Apr 9, 2007
    Messages:
    45
    #10
    As the shouldrotate.. method is not called so we have written in loadview method but no effect :eek:(.
    Please help
     

Share This Page