iphone - Is there a way to determine device orientation if view is loaded before [window makeKeyAndVisible] is called? -
view of uiviewcontroller loaded nib before [window makekeyandvisible] called in application:didfinishlaunchingwithoptions: cannot correct device orientation - i.e. self.interfaceorientation, [[uiapplication sharedapplication] statusbarorientation] , [[uidevice currentdevice] orientation] give me false orientation info i'm in portrait mode (ipad's default mode, think).
is there way work around problem? please don't need use accelerometer ;)
edit: interested on getting orientation info based on accelerometer reading: http://web.archive.org/web/20091111033649/http://blog.sallarp.com/iphone-accelerometer-device-orientation
it appears can't rely on [uidevice currentdevice].orientation until orientation changes first time. if so, hack getting raw accelerometer readings.
#define kupdatefrequency 30 // hz #define kupdatecount 15 // init after half 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 first kupdatecount readings // acceleration_[xyz] 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 acceleration_[xyz] , set ui... } original response:
does [uidevice currentdevice].orientation return correct orientation during applicationdidfinishlaunching:? if so, can set initial ui according orientation.
if property doesn't set until later time, might try experimenting performselector:afterdelay: initialize ui after small delay.
this code sample kendall's answer below, added here completeness:
[self performselector:@selector(getoriented) withobject:nil afterdelay:0.0f]; i'm not sure if zero-second delay sufficient -- means code getoriented run during first pass through event run loop. may need wait longer accelerometer readings register on uidevice.
Comments
Post a Comment