didAnimateFirstHalfOfRotationToInterfaceOrientation iOS5替换 [英] didAnimateFirstHalfOfRotationToInterfaceOrientation iOS5 replacement

查看:116
本文介绍了didAnimateFirstHalfOfRotationToInterfaceOrientation iOS5替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

didAnimateFirstHalfOfRotationToInterfaceOrientation 。但是我想在我的应用程序中使用此方法。我正在使用Apple在iOS开发中心提供的示例代码,项目名称 AlternateViews 。我希望应用程序在 landscapeView 中淡出时旋转 portraitView 。这可以在iOS 5中完成还是这个功能永远消失了?

didAnimateFirstHalfOfRotationToInterfaceOrientation is deprecated in iOS 5.0. However I would like to use this method in my application. I am using the sample code that Apple offers in the iOS Dev Center, project name AlternateViews. I would like the app to rotate the portraitView while fading in the landscapeView. Can this be done in iOS 5 or is this feature forever gone?

portraitView 当前通话:

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

landscapeView init 方法的代码中调用此方法:

while landscapeView calls this in code in the init method:

self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

看来所有动画都在以下的PortraitViewController中完成。 m:

It appears all of the animation is done in the following PortraitViewController.m:

#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:

Here is my current implementation files which fail miserably. PortraitViewController.m:

#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

LandscapeViewController.m

#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


推荐答案

在Apple的 UIViewController参考页,你会在处理视图轮换部分找到这个有用的段落:

In Apple's UIViewController reference page, you'll find this useful paragraph in the "Handling View Rotations" section:


如果你想在方向改变期间执行自定义动画,你可以用两种方式之一来实现
。定向更改过去发生在
两步,通知发生在轮换的开头,中间和
终点。但是,在iOS 3.0中,为
添加了对一步执行方向更改的支持。使用一步式
方向更改往往比旧的两步流程
更快,并且通常建议用于任何新代码。

If you want to perform custom animations during an orientation change, you can do so in one of two ways. Orientation changes used to occur in two steps, with notifications occurring at the beginning, middle, and end points of the rotation. However, in iOS 3.0, support was added for performing orientation changes in one step. Using a one-step orientation change tends to be faster than the older two-step process and is generally recommended for any new code.

要为方向更改添加动画,请覆盖
willAnimateRotationToInterfaceOrientation:duration:方法并在那里执行
你的动画。

To add animations for an orientation change, override the willAnimateRotationToInterfaceOrientation:duration: method and perform your animations there.

你可以覆盖 willAnimateRotationToInterfaceOrientation:方法来制作你的动画吗?

Could you override the willAnimateRotationToInterfaceOrientation: method to do your animations?

这篇关于didAnimateFirstHalfOfRotationToInterfaceOrientation iOS5替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆