首页
学习
活动
专区
圈层
工具
发布

iOS5替换
EN

Stack Overflow用户
提问于 2012-01-02 12:44:59
回答 1查看 2.9K关注 0票数 0

iOS 5.0中不推荐使用didAnimateFirstHalfOfRotationToInterfaceOrientation。但是,我想在我的应用程序中使用此方法。我正在使用苹果在iOS开发人员中心提供的样例代码,项目名称为AlternateViews。我希望应用程序旋转portraitView,同时在landscapeView中淡入淡出。在iOS 5中可以做到这一点吗?或者这个特性会永远消失吗?

portraitView当前调用:

[self presentModalViewController:self.landscapeViewController animated:YES];

landscapeViewinit方法的代码中调用它:

self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

似乎所有动画都是在以下PortraitViewController.m中完成的:

代码语言:javascript
复制
#import "PortraitViewController.h"
#import "LandscapeViewController.h"

@implementation PortraitViewController

@synthesize landscapeViewController;

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor colorWithRed:197.0/255.0 green:204.0/255.0 blue:211.0/255.0 alpha:1.0];

    LandscapeViewController *viewController = [[LandscapeViewController alloc]
                                                    initWithNibName:@"LandscapeView" bundle:nil];
    self.landscapeViewController = viewController;
    [viewController release];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                                    name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)viewDidUnload
{
    self.landscapeViewController = nil;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    [landscapeViewController release];

    [super dealloc];
}

- (void)orientationChanged:(NSNotification *)notification
{
    // We must add a delay here, otherwise we'll swap in the new view
    // too quickly and we'll get an animation glitch
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeViewController animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait); // support only portrait
}

@end

下面是我当前的实现文件,它们都失败得很糟糕。PortraitViewController.m:

代码语言:javascript
复制
#import "PortraitViewController.h"
#import "LandscapeViewController.h"

@implementation PortraitViewController

@synthesize landscapeViewController;

- (void)viewDidLoad
{   
    LandscapeViewController *viewController = [[LandscapeViewController alloc]
                                                    initWithNibName:@"LandscapeView" bundle:nil];
    self.landscapeViewController = viewController;
    [viewController release];
    NSLog(@"Portrait viewDidLoad");
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
}

- (void)viewDidUnload
{
    self.landscapeViewController = nil;
}

- (void)dealloc
{   
    [landscapeViewController release];

    [super dealloc];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        NSLog(@"Portrait-willAnimateRotationToInterfaceOrientation Portrait");
    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Portrait-willAnimateRotationToInterfaceOrientation Landscape");
    }
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];

}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        NSLog(@"Portrait-present Landscape");
        [self presentModalViewController:self.landscapeViewController animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        NSLog(@"Portrait-dismiss Landscape");
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    else
        return YES;
}

@end

LandscapeViewController.m

代码语言:javascript
复制
#import "LandscapeViewController.h"

@implementation LandscapeViewController

// the designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.wantsFullScreenLayout = YES; // we want to overlap the status bar.

        // when presented, we want to display using a cross dissolve
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    oldStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
    NSLog(@"Landscape viewWillAppear");
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[UIApplication sharedApplication] setStatusBarStyle:oldStatusBarStyle animated:NO];    
}

// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // return YES for supported orientations
    if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    else
        return YES;
    //return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        NSLog(@"Landscape-willAnimateRotationToInterfaceOrientation Portrait");
        [self dismissModalViewControllerAnimated:YES];
    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape-willAnimateRotationToInterfaceOrientation Landscape");

    }
}


@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-02 12:50:58

在苹果的UIViewController reference page中,你会在“处理视图旋转”一节中找到这个有用的段落:

如果要在方向更改期间执行自定义动画,可以通过以下两种方法之一执行此操作。方向更改通常分两步进行,通知发生在旋转的开始、中间和结束点。但是,在iOS 3.0中,添加了对在一个步骤中执行方向更改的支持。使用一步方向更改往往比旧的两步过程更快,并且通常推荐用于任何新代码。

若要为方向更改添加动画,请覆盖willAnimateRotationToInterfaceOrientation:duration:方法并在其中执行动画。

你能覆盖willAnimateRotationToInterfaceOrientation:方法来做你的动画吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8697473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档