`
啸笑天
  • 浏览: 3471700 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

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。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论
1 楼 u013929312 2015-05-06  
棒棒嗒!拿走了哦!

相关推荐

    ios开发有用的小代码集合

    ios开发中常用的小代码集合,工作开发过程中收集,非常适用。

    swift-iOS的旋转动画集合

    iOS的旋转动画集合

    iOS5.1与iOS6.0屏幕旋转兼容demo

    通过这个"iOS5.1与iOS6.0屏幕旋转兼容demo",开发者可以学习到如何在不同iOS版本下处理屏幕旋转,确保应用在所有设备上都能提供一致的用户体验。在实际项目中,可以借鉴这个示例,结合项目需求,灵活调整旋转策略。

    iOS 图片旋转动画

    在iOS开发中,图片旋转动画是一种常见的视觉效果,常用于音乐播放器、加载指示器或者各种过渡动画。本文将深入探讨如何实现一个类似音乐播放器的图片旋转动画,并在动画停止时保持图片当前的旋转角度。 首先,我们...

    iOS图片旋转缩放demo

    在iOS开发中,图片的旋转和缩放是常见的交互需求,尤其在照片编辑或查看应用中更为重要。这个“iOS图片旋转缩放demo”提供了一个实现这一功能的实例,可以帮助开发者更好地理解和应用这一技术。 首先,我们要理解的...

    ios混淆代码工具及垃圾代码生成器工具

    本文将详细解析标题"ios混淆代码工具及垃圾代码生成器工具"所涉及的知识点,并探讨如何在iOS项目中实施混淆策略以及添加垃圾代码来增强应用的安全性。 **一、iOS混淆** 1. **类名和方法名混淆**:混淆的核心是改变...

    ios 360度旋转效果demo

    【iOS 360度旋转效果Demo】是一个用于展示如何在iOS应用中实现图像360度连续旋转的示例项目。这个Demo适用于iPhone 6.1设备,并且已经在该设备上进行了成功的测试。它通过使用`UIImageView`这一UI组件,来实现图片的...

    iOS 旋转罗盘菜单

    总结起来,创建一个iOS旋转罗盘菜单涉及的知识点包括:Swift编程、UIKit框架、手势识别(UIPanGestureRecognizer)、Core Animation、阻尼效果计算、设备方向适应、用户反馈机制(音效、震动),以及良好的代码组织...

    ios-屏幕旋转demo.zip

    在iOS开发中,屏幕旋转(Screen Orientation)是用户体验的重要组成部分,尤其在移动设备上,用户经常需要在横屏和竖屏之间切换以适应不同的应用场景。本示例"ios-屏幕旋转demo.zip"提供了关于如何在iOS应用中实现...

    解决ios拍照上传图片被旋转

    在iOS设备上拍照并上传至服务器时,有时会出现图片被旋转的问题。这通常是由于iOS设备在拍摄照片时记录了照片的元数据(Exif信息),其中包括拍摄时的设备方向信息。当这些信息未被正确处理时,图片在其他平台显示时...

    iOS UIAlertController的强制旋转

    在iOS开发中,用户界面通常会根据设备的方向自动调整布局,以适应横屏或竖屏模式。然而,有些特殊情况可能需要我们对特定视图控制器的行为进行定制,比如强制使其保持固定的方向,即使设备本身旋转了。`...

    ios旋转坐标设置

    ### iOS旋转坐标设置 在iOS应用开发过程中,为了提供更好的用户体验,经常需要处理屏幕方向变化时界面元素(如按钮、文本框等)的布局调整。本文将详细介绍如何通过编程方式来实现屏幕旋转时控件布局的自适应调整,...

    ios5旋转屏幕

    在iOS开发中,屏幕旋转是用户体验的重要组成部分。iOS 5引入了新的屏幕旋转管理机制,与之前的版本有所不同。本文将详细解析iOS 5中的屏幕旋转处理,并基于提供的“LandscapeDemoIos5”项目文件进行讲解。 首先,...

    iOS软件代码规范

    iOS 软件代码规范 iOS 软件代码规范是一份详细的编程规范,旨在指导 iOS 软件开发过程,提高代码质量和可维护性。这份规范涵盖了编程指引原则、布局、注释、命名规则、变量、常量、宏与类型、表达式与语句、函数、...

    ios-弹出旋转菜单,类似建行客户端旋转菜单的简单demo.zip

    在iOS开发中,创建一个类似建设银行客户端的旋转弹出菜单是一种常见的交互设计,它可以提供用户友好的体验,尤其在移动设备上。本项目“ios-弹出旋转菜单,类似建行客户端旋转菜单的简单demo”就是针对这种需求的一...

    iOS马甲包代码混淆工具

    iOS代码混淆加密工具 制作iOS马甲包,包括代码加密、代码混淆、修改方法名、修改类名、生成垃圾代码、修改工程名等 https://zfj1128.blog.csdn.net/article/details/95482006

    ios-collection实现旋转效果.zip

    在iOS开发中,集合视图(UICollectionView)是一种强大的布局组件,可以用来展示各种自定义的、可滚动的数据集合。在这个“ios-collection实现旋转效果.zip”压缩包中,开发者提供了一个使用Swift编写的示例项目,名...

    iOS 马甲包代码混淆工具

    iOS 马甲包代码混淆工具 .

Global site tag (gtag.js) - Google Analytics