views:

142

answers:

1

I put this code into all of my viewControllers:

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

 if (orientation == UIInterfaceOrientationLandscapeRight) {
  CGAffineTransform transform = self.view.transform;

  // Use the status bar frame to determine the center point of the window's content area.
  //CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
  //CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
  CGPoint center = CGPointMake(320/2, 480/2);

  // Set the center point of the view to the center point of the window's content area.
  self.view.center = center;

  // Rotate the view 90 degrees around its new center point.
  transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
  self.view.transform = transform;
 }

and I also have:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

I can see that the codes work correctly as they should, but once in a while, there is a rare occasion that the main view controller failed to rotate for landscape mode after I put its view back to be the main view. The chance that it occurs is very rare, but this is nagging me. Moreover, I can see that it occurs more often on the iphone. On the simulator, I recalled it never happen. Do you know what are the possible causes for this? Thank you in advance.

A: 

I found the cause of my iphone program.

One of the possible causes is memory. This method below will be called when the system is low on memory:

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

An "unseen" UIView is unloaded when this method is called. When you are about to see this view again, the device reloaded it, but maybe because you set the parameters (for the rotation, etc.) in a place where it won't be called when it gets reloaded. You may use:

- (void)viewDidLoad {

    UIApplication* application = [UIApplication sharedApplication];
    application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;

    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation([MyMath radd:90]);
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, -80, 83);

    [self.view setTransform: landscapeTransform];
    [super viewDidLoad];
}

It occurs in the device because of we are talking real memory on the real device. You can use "Hardware > Simulate Memory Warning" when you are using the simulator.

unknownthreat