Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>it appears that you can't rely on <code>[UIDevice currentDevice].orientation</code> until the orientation actually changes for the first time. If so, you could probably hack it by getting raw accelerometer readings.</p> <pre><code>#define kUpdateFrequency 30 // Hz #define kUpdateCount 15 // So we init after half a second #define kFilteringFactor (1.0f / kUpdateCount) - (void)applicationDidFinishLaunching:(UIApplication *)app { [UIAccelerometer sharedAccelerometer].updateInterval = (1.0 / kUpdateFrequency); [UIAccelerometer sharedAccelerometer].delegate = self; accelerometerCounter = 0; ... } - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel { // Average out the first kUpdateCount readings // acceleration_[xyz] are ivars typed float acceleration_x = (float)accel.x * kFilteringFactor + acceleration_x * (1.0f - kFilteringFactor); acceleration_y = (float)accel.y * kFilteringFactor + acceleration_y * (1.0f - kFilteringFactor); acceleration_z = (float)accel.z * kFilteringFactor + acceleration_z * (1.0f - kFilteringFactor); accelerometerCounter++; if (accelerometerCounter == kUpdateCount) { [self initOrientation]; [UIAccelerometer sharedAccelerometer].delegate = nil; } } - (void)initOrientation { // Figure out orientation from acceleration_[xyz] and set up your UI... } </code></pre> <hr> <p><strong>Original response:</strong></p> <p>Does <code>[UIDevice currentDevice].orientation</code> return the correct orientation during <code>applicationDidFinishLaunching:</code>? If so, you can set up your initial UI according to that orientation.</p> <p>If that property doesn't get set until some later time, you might try experimenting with <code>performSelector:afterDelay:</code> to initialize the UI after a small delay.</p> <p>This code sample is from Kendall's answer below, added here for completeness:</p> <pre><code>[self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f]; </code></pre> <p>I'm not sure if a zero-second delay is sufficient -- this means the code for <code>getOriented</code> will run during the first pass through the event run loop. You may need to wait longer for the accelerometer readings to register on <code>UIDevice</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload