什么是UIView在重访时很少不会被旋转的一些可能的原因?
问题描述:
我把这个代码到我所有的viewControllers的:什么是UIView在重访时很少不会被旋转的一些可能的原因?
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;
}
我也有:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
我可以看到,代码正常工作,因为他们应该,但过一段时间,有在主视图控制器无法旋转为横向模式之后,我将其视图作为主视图是一种罕见的情况。它发生的机会非常罕见,但是这让我感到不快。而且,我可以看到它在iphone上更经常发生。在模拟器上,我记得它永远不会发生。你知道这有什么可能的原因吗?先谢谢你。
答
我找到了我的iphone程序的原因。
其中一个可能的原因是内存。当系统内存不足时,将调用以下方法:
- (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.
}
调用此方法时会卸载“unseen”UIView。当你要再次看到这个视图时,设备重新加载它,但可能是因为你将参数(对于旋转等)设置在重新加载时不会调用的地方。您可以使用:
- (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];
}
它出现在设备中,因为我们在真实设备上说实际的内存。当您使用模拟器时,您可以使用“硬件>模拟内存警告”。