判断横屏竖屏
最有效的方法是:
在willRotateToInterfaceOrientation:duration:
方法中将方向存储起来:
DrviceOrientation = toInterfaceOrientation;
然后在别的方法中使用
范例:先定义一个UIInterfaceOrientation deviceDirection;
-(void)viewWillAppear:(BOOL)animated
{
[selfsetFrameToRotation:deviceDirection];
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
//竖屏
[selfsetFrameToRotation:UIInterfaceOrientationPortrait];
}
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
//横屏
[selfsetFrameToRotation:UIInterfaceOrientationLandscapeRight];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
deviceDirection = interfaceOrientation;
returnYES;
}
/*
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
*/
-(void)setFrameToRotation:(UIInterfaceOrientation)interfaceOrientation
{
//竖屏
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
//横屏
else
{
[bgImageView setFrame:CGRectMake(0, 0, 1024, 748)];
[logoBgImageView setFrame:CGRectMake(440, 180, 426, 408)];
[_btnMoreViewControllersetFrame:CGRectMake(30, 695, 184, 53)];
}
}
常识普及:
要翻转的时候,首先响应的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES则支持翻转,NO则不支持。
紧接着
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
最后调用的是
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
方法2:当然 ,前提是你没有把系统的状态栏去掉.它可以用在任何方法内作为判断条件.
#pragma mark 横屏调整
-(void)viewWillAppear:(BOOL)animated
{
// //判断设备版本号
// float version = [[[UIDevice currentDevice] systemVersion] floatValue];
// if (version < 6.0)
// {
// [super viewWillAppear:animated];
//
// [UIView animateWithDuration:0.0f
// animations:^{
// [self.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
// }];
// }
// int a = [[UIScreen mainScreen] bounds].size.width;
// int b = [[UIScreen mainScreen] bounds].size.height;
// int c = [[UIScreen mainScreen] applicationFrame].size.width;
// int d = [[UIScreen mainScreen] applicationFrame].size.height;
//设置横屏和竖屏
if ([[UIScreenmainScreen] applicationFrame].size.height == 1024)
{
//横屏时,height为1024,width为748。
[selfsetFrameToRotation:UIInterfaceOrientationLandscapeRight];
}
else
{
//竖屏时,height为1004,width为768。
[selfsetFrameToRotation:UIInterfaceOrientationPortrait];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
方法3:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
{
NSLog(@"左横屏");
[selfsetFrameToRotation:UIInterfaceOrientationLandscapeRight];
}
if (interfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
NSLog(@"右横屏");
[selfsetFrameToRotation:UIInterfaceOrientationLandscapeRight];
}
if (interfaceOrientation==UIInterfaceOrientationPortrait)
{
NSLog(@"竖屏");
[selfsetFrameToRotation:UIInterfaceOrientationPortrait];
}
if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(@"上下竖屏");
[selfsetFrameToRotation:UIInterfaceOrientationPortrait];
}
returnYES;
}
关注:
http://blog.csdn.net/zhibudefeng/article/details/7792320

浙公网安备 33010602011771号