需求:程序默认开启是横屏,在压到某个页面的时候是竖屏1.默认横屏需要在XX-info.plist 的Supported interface orientations 的第一项为Landscape (left home button)
2.在切换的竖屏的时候
-(void)viewWillAppear:(BOOL)animated
{
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation != UIInterfaceOrientationPortrait ||
orientation != UIInterfaceOrientationPortraitUpsideDown)
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
//(获取当前电池条动画改变的时间
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
//在这里设置view.transform需要匹配的旋转角度的大小就可以了。
self.view.transform = [self getTransformMakeRotationByOrientation:orientation];
[UIView commitAnimations];
}
}
计算方向
- (CGAffineTransform)getTransformMakeRotationByOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
return CGAffineTransformMakeRotation(M_PI/2);
}
else if (orientation == UIInterfaceOrientationLandscapeRight)
{
return CGAffineTransformMakeRotation(M_PI/2);
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
return CGAffineTransformMakeRotation(-M_PI);
}
else
{
}
return CGAffineTransformIdentity;
}
- (void)viewWillDisappear:(BOOL)animated
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation != UIInterfaceOrientationLandscapeLeft ||
orientation != UIInterfaceOrientationLandscapeRight)
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
//(获取当前电池条动画改变的时间
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
}