第一种:经过人为的办法改变view.transform的属性。app
具体办法:ide
view.transform通常是View的旋转,拉伸移动等属性,相似view.layer.transform,区别在于 View.transform是二维的,也就是使用仿射的办法一般就是带有前缀CGAffineTransform的类(能够到API文档里面搜索这个前 缀的全部类),而view.layer.transform能够在3D模式下面的变化,一般使用的都是前缀为CATransform3D的类。动画
这里要记住一点,当你改变过一个view.transform属性或者view.layer.transform的时候须要恢复默认状态的话,记得先把他 们重置可使用view.transform = CGAffineTransformIdentity,或者view.layer.transform = CATransform3DIdentity,假设你一直不断的改变一个view.transform的属性,而每次改变以前没有重置的话,你会发现后来 的改变和你想要的发生变化了,不是你真正想要的结果。spa
好了,上面介绍了旋转的属性,接下来就是关键了。官方提供了一个办法就是查看当前电池条的状态UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;经过这个办法,你能够知道当前屏幕的电池条的显示方向,并且你还能够 强制设置他的显示方向,经过设置这个属性就OK了,能够选择是否动画改变电池条方向。有了这两个那咱们就能够任意的改变咱们想要的显示方式了。orm
1.获取当前电池条的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation文档
2.获取当前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame;it
3.设置咱们的View的中心点
CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));io
4.根据当前电池条的方向,获取须要旋转的角度的大小。一般form
if (orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(M_PI*1.5);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformMakeRotation(M_PI/2);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformMakeRotation(-M_PI);
} else {
return CGAffineTransformIdentity;
}stream
5.能够动画的改变咱们view的显示方式了
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(获取当前电池条动画改变的时间)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
//在这里设置view.transform须要匹配的旋转角度的大小就能够了。
[UIView commitAnimations];
第二种:经过setOrientation:的办法强制性的旋转到一个特定的方向。
注意:Apple在3.0之后都不支持这个办法了,这个办法已经成为了私有的了,可是要跳过App Stroe的审核,须要一点巧妙的办法。
不要直接调用[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]这样的办法来强制性的横屏,这样致使你的程序是很难经过App Store审核的。可是你能够选择使用performSelector的办法来调用它。具体就几行代码以下:
//强制横屏
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
总结:若是第一种办法能够知足你须要的话,最好使用第一种办法,由于那个上 App Store确定没问问题,可是第二种的话是须要冒风险的,可是若是你的结构太复杂了,致使使用第一种办法人为很难控制的话,能够尝试简单的使用第二种办 法。我在有米提供的sample里面就看到他使用了第二种简单的办法来显示横屏竖式的广告条。