Note that there are some explanatory texts on larger screens.

plurals
  1. POUIViewController shouldAutorotateToInterfaceOrientation - would like to add hysteresis
    primarykey
    data
    text
    <p>I would like to defer auto rotating the user interface until the device has settled on an orientation for a number of seconds, rather than driving the user insane and flicking willy nilly whenever they tilt the device a few degrees off axis by mistake. </p> <p>the closest i can get to this (which is by no means what I want, as it locks the UI) is:</p> <pre><code>- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Overriden to allow any orientation. [NSThread sleepForTimeInterval:2.0]; return YES; } </code></pre> <p>what i would like to do is use something like this - which works in principle, by checking the console log, but i need the appropriate line of code that has been commented out. </p> <pre><code>-(void) deferredAutorotateToInterfaceOrientation:(NSTimer *) timer { autoRotationTimer = nil; UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)[timer.userInfo integerValue]; NSLog(@"switching to new orientation %d now",interfaceOrientation); // replace this with code to induce manual orientation switch here. //[self forceAutoRotateToInterfaceOrientation:interfaceOrientation]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Overriden to allow any orientation. [autoRotationTimer invalidate]; autoRotationTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(deferredAutorotateToInterfaceOrientation:) userInfo:[NSNumber numberWithInt:(int)interfaceOrientation ] repeats:NO]; NSLog(@"denying autorotate, deffering switch to orientation %d by 2 seconds",interfaceOrientation); return NO; } </code></pre> <p>I realize there are sometimes many ways to do things, so if this approach is not the most efficient, and someone can suggest another way to do this, I am all ears. My main criteria is I want to delay the onset of autorotation, whilst keeping a responsive user interface if indeed they have only leaned to the left slightly because they are in a bus that just went around a corner etc.</p> <p>EDIT: I found a solution which may not be app store friendly, however i am a few weeks away from completion, and someone may answer this in the meantime. this works calls an undocumented method. the (UIPrintInfoOrientation) typecast is just to suppress the compiler warning, and does not affect the value being passed.</p> <pre><code>-(void ) forceUIOrientationInterfaceOrientation:(UIDeviceOrientation) interfaceMode { [(id)[UIDevice currentDevice] setOrientation:(UIPrintInfoOrientation) interfaceMode]; } </code></pre> <p>full implementation which includes re-entrance negation is as follows:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; acceptNewAutoRotation = YES; } -(void ) forceUIOrientationInterfaceOrientation:(UIDeviceOrientation) interfaceMode { [(id)[UIDevice currentDevice] setOrientation:(UIPrintInfoOrientation) interfaceMode]; } -(void) deferredAutorotateToInterfaceOrientation:(NSTimer *) timer { autoRotationTimer = nil; acceptNewAutoRotation = YES; [self forceUIOrientationInterfaceOrientation:[[UIDevice currentDevice] orientation]]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Overriden to allow any orientation. [autoRotationTimer invalidate]; if (acceptNewAutoRotation) { autoRotationTimer = nil; acceptNewAutoRotation = NO; return YES; } else { autoRotationTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(deferredAutorotateToInterfaceOrientation:) userInfo:[NSNumber numberWithInt:(int)interfaceOrientation ] repeats:NO]; return NO; } } </code></pre>
    singulars
    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.
 

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