一、屏幕旋转的支持
处理屏幕旋转支持的代码在ViewController.m文件里,函数如下。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
//return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
return YES;
}
语句:return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);表示不支持home键在上面。
改成return YES;则支持所有屏幕方向。
另外,
UIInterfaceOrientationPortrait-------表示home键在下(默认的纵向模式)
UIInterfaceOrientationPortraitUpsideDown-------表示home键在上
UIInterfaceOrientationLandscapeLeft-------表示home键在左
UIInterfaceOrientationLandscapeRight---------表示home键在右
二、旋转屏幕事件
(1)willAnimateFirstHalfOfRotationTolnterfaceOrientation: 旋转前触发(直接添加下列函数即可)
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration{
//......
}
参数toInterfaceOrientation表示窗口即将改变的方向,
参数duration表示前一半旋转的持续时间,单位秒。
(2)willAnimateSecondHalfOfRotationFromInterfaceOrientation: 旋转进行到一半时触发
(3)willRotateToInterfaceOrientation: 等于连续触发上述两次事件
(4)willAnimateRotationToInterfaceOrientation: 在旋转动画开始前触发
【注意】1.willRotate必须在willAnimate前;
2.如需处理willAnimate,则willAnimateFirst和willAnimateSecond不再触发。
三、运行期间旋转屏幕
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
四、程序加载时设置屏幕方向
【注意】会触发屏幕旋转事件。