ios设备旋转走的代理(代码附加输出口集合)
//1//1//被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ NSLog(@"willRotateToInterfaceOrientation %d",toInterfaceOrientation);}//2////这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { NSLog(@"willAnimateRotationToInterfaceOrientation %d",interfaceOrientation); if (interfaceOrientation == UIInterfaceOrientationPortrait) { self.view = self.portrait; self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0)); self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0); } else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { self.view = self.landscape; self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90)); self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0); } else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) { self.view = self.landscape; self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0); }}/*////2//视图旋转动画前一半发生之前自动调用- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ NSLog(@"willAnimateFirstHalfOfRotationToInterfaceOrientation %d",toInterfaceOrientation);}////3//视图旋转动画前一半发生之后自动调用- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { NSLog(@"didAnimateFirstHalfOfRotationToInterfaceOrientation %d",toInterfaceOrientation); }// The rotating header and footer views are offscreen.////4//视图旋转动画后一半发生之前自动调用- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{ NSLog(@"willAnimateSecondHalfOfRotationFromInterfaceOrientation %d",fromInterfaceOrientation); } *///3//5//这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ NSLog(@"didRotateFromInterfaceOrientation %d",fromInterfaceOrientation); }// 《=ios5- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}//ios6- (BOOL)shouldAutorotate{ return YES;}//ios6-(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAllButUpsideDown;}
?-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:。
但是当willAnimateRotationToInterfaceOrientation走时,willAnimateFirstHalfOfRotationToInterfaceOrientation:duration,willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration和willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration是不走的,也就是二选一!但用后者三个方法系统提示过时了。
?
------------------------ios6的略微改变---------------
?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation //已经相当弱化
?
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
//新增Api,与info.plist设备支持旋转方向神似(区别在于default.png)且注意与UIInterfaceOrientation不同的是这的NSUInteger返回值为UIInterfaceOrientationMaskPortrait|...等
?
//iOS6新增Api来控制旋转,需要注意的是顶层才是有效的
- (BOOL)shouldAutorotate
{
? ? return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
? ? return UIInterfaceOrientationMaskPortrait;
}
?
//习惯使用的presentModalViewController navigation controller在旋转上已经出现了大问题,可用category解决
@implementation UINavigationController (autorotate)
- (NSUInteger)supportedInterfaceOrientations{
?
? ? NSArray *arr = self.viewControllers;
? ? if ([arr count] == 0) {
? ? ? ? return UIInterfaceOrientationMaskPortrait;
? ? }
? ? id vc = [arr objectAtIndex:0];
? ? if ([vc isKindOfClass:[TestNoRotation class]]) {
? ? ? ? return UIInterfaceOrientationMaskPortrait;
?
? ? }
?
? ? if ([vc isKindOfClass:[TestAutoRotation class]]) {
? ? ? ? return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
? ? }
?
? ? return UIInterfaceOrientationMaskPortrait;
}
@end
?
?
在ios6.0中shouldAutorotateToInterfaceOrientation:几乎不在起作用了,ios使用shouldAutorotate来控制旋转效果。
我测试ios6.0真正起作用的旋转有三种:
1.采用info.plist的UISupportedInterfaceOrientations来控制方向
2.是直接在UIWindow中加视图,这种方法可以脱离info.plist的控制,shouldAutorotate来自定义方向。
3.使用rootViewController添加shouldAutorotate方法,但是受info.plist的限制。
以上的方法都限制于顶层视图控制,如果要在子视图控制中添加旋转效果,则需要在顶层视图控制器中开启shouldAutorotate方法,在子视图控制器就可以使用
- (NSUInteger)supportedInterfaceOrientations{}
?
代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
? ? self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
? ? // Override point for customization after application launch.
? ? self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil] autorelease];
? ? self.window.rootViewController = self.mainViewController;
? ? self.mainViewController.window = self.window;
? ? [self.window makeKeyAndVisible];
? ??
// ?直接添加视图来控制
// ? ?self.testVC = [[[testViewController alloc] initWithNibName:@"testViewController" bundle:nil] autorelease]; ? ?
? ? //[self.window addSubview:self.testVC.view];
? ? return YES;
}
?
顶层视图控制器开启旋转
?
//MainViewController中开启旋转
- (BOOL)shouldAutorotate
{
? ? return YES;
}
?
-(NSUInteger)supportedInterfaceOrientations
{
? ??
? ? return UIInterfaceOrientationMaskLandscape;
? ??
? ??
}
?
添加一个子视图
?
?
- (IBAction)showInfo:(id)sender
{
? ? testViewController *vc = ?[[testViewController alloc] initWithNibName:@"testViewController" bundle:nil];
? ??
? ??
? ? //添加子视图
? ? [self presentViewController:vc animated:YES completion:nil];
}
?
子视图控制器直接使用
//可以不写
//- (BOOL)shouldAutorotate
//{
// ? ?return YES;
//}
?
// testViewController中直接使用
-(NSUInteger)supportedInterfaceOrientations
{
? ? return UIInterfaceOrientationMaskPortrait;
}
?
?
?
?
?-----------------------------------切糕分割线----------------------------------
假如如下方法禁止旋转:
?
- (BOOL)shouldAutorotate
{
? ? return NO;
}
或
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
?return NO;
}
?
?
?
? 可以增加监听来选装[notificationCenter addObserver:selfselector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotificationobject:nil];
?
?在自定义deviceOrientationDidChange方法中:
获取自身屏幕方法使用self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation],不要使用[[UIDevice currentDevice] orientation](第一次使用的时候,总是返回0)。
?
----------------------------------不旋转原因的分割线---------------
转自http://blog.sina.com.cn/s/blog_6de189920101266h.html
UIViewController没有随着设备一起旋转的原因
对于iPhone app,UIViewController类提供了基本的视图管理模式。当设备改变方向的时候view controller的视图会自动随之旋转的。如果视图和子视图的autoresizing属性设置是对的,这时候视图又没有随着设备一起旋转,可能是以下的原因:
1.view controller没有完成代理方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
也要实现了shouldAutorotateToInterfaceOrientation方法,同时shouldAutorotateToInterfaceOrientation方法要返回YES来支持所有的旋转方向
?
2.view controller的UIView属性嵌入在UIWindow中,并非是一个附加的view controller
你可能会发现shouldAutorotateToInterfaceOrientation方法只会在view controller启动的时候被调用,不会因为设置的旋转而在被调用。这是因为view controller束缚着他们所管理的视图,view controller是用来处理时间的响应链的一部分。view controller是从UIResponder继承而来,同时他被插入到他所管理的视图和他的superview之间。因此,通常的做法是在你的app中有一个主view controller来作为响应链的一部分。通常来说会添加一个主view controller,例如UINavigationController, UITabBarController或者UIViewController到UIWindow上。
例如
[myWindow addSubview:primaryViewController.view];?
如果你添加了另外一个view controller的UIView属性到UIWindow(anotherController和主view controller在同一个等级上)
[myWindow addSubview:anotherController.view];
anotherController将不会接受旋转事件,只有第一个view controller(primaryViewController)会接受旋转事件。
?
3.你添加了view controller的UIView属性到UIWindow作为subview,但是过早的release它。
UIWindow会retain视图,而不是view controller。你不能过早的release他。在UIApplicationDelegate子类中定义他为retain属性。
?
4.在UITabBarController或者UINavigationController中的子view controller没有对同一方向的支持。
为了确保所有的子view controller旋转正确,你的每一个view controller,每一个tab或者额navigation都要完成shouldAutorotateToInterfaceOrientation,而且必须支持对于同一方向的旋转,也就是说对于同一方向的位置要返回为YES。
?
5.重写-(id)init:或者 -(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle 方法的时候没有调用super。
对于对象的初始化,在你的view controller的init或者initWithNibName方法中必须要调用super。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?