DidAnimateFirsthalfofrotationInterfaceOrientation iOS5更换
DidAnimateFirsthalfofrotationInterfaceOrientation iOS5更换,ios5,deprecated,Ios5,Deprecated,DidAnimateFirsthalfofrotationInterfaceOrientation在iOS 5.0中被弃用。但是,我希望在我的应用程序中使用此方法。我正在使用苹果在iOS开发中心提供的示例代码,project nameAlternateViews。我希望应用程序在景观视图中淡入时旋转肖像视图。这可以在iOS 5中实现,还是这一功能永远消失了 肖像视图当前调用: [self-presentModalViewController:self.landscapeViewControlle
DidAnimateFirsthalfofrotationInterfaceOrientation
在iOS 5.0中被弃用。但是,我希望在我的应用程序中使用此方法。我正在使用苹果在iOS开发中心提供的示例代码,project nameAlternateViews
。我希望应用程序在景观视图中淡入时旋转肖像视图
。这可以在iOS 5中实现,还是这一功能永远消失了
肖像视图
当前调用:
[self-presentModalViewController:self.landscapeViewController动画:是]代码>
当landscapeView
在init
方法中的代码中调用此函数时:
self.modaltransationstyle=uimodaltransationstyle代码>
所有动画似乎都是在以下视图控制器中完成的。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
#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
下面是我当前的实现文件,它失败得很惨。
视图控制器。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
#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
#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的“处理视图旋转”部分中,您会发现这一有用的段落:
如果要在方向更改期间执行自定义动画,
您可以通过以下两种方式之一实现。方向变化通常发生在
两个步骤,通知发生在开头、中间和结尾
旋转的终点。但是,在iOS 3.0中,添加了对
在一个步骤中执行方向更改。使用一步法
方向变化往往比旧的两步过程更快
并且通常推荐用于任何新代码
若要为方向更改添加动画,请替代
方法和执行
你的动画在那里
你能重写WillAnimateRotationInterfaceOrientation:
方法来制作你的动画吗?我相信这会管用,但是我是一个新手程序员,所以我可能需要更多的帮助。我想您应该让肖像视图接受所有旋转,这样它将旋转到横向,然后使用将动画设置为旋转的第二个一半,从InterfaceOrientation:duration
到presentModalViewController:self.landscapeViewController
。这是正确的逻辑还是我会使用WillAnimateRotationInterfaceOrientation
并在该方法中告诉肖像视图旋转,然后呈现ModalviewController:self.landscapeViewController
?如果您编辑问题添加的代码运行良好,为什么不从WillAnimateRotationInterfaceOrientation调用你的UpdateLandCapeView
方法呢。我根本不会使用InterfaceOrientation的*half rotationfromInterfaceOrientation
方法,因为它们已被弃用,可能会在iOS 6或iOS 7中消失。在任何情况下,您所寻找的功能都不会“永远”消失。。。它只是被改成更能证明未来。您应该能够修改您的工作代码以利用新的API。我同意您的建议,从WillAnimateRotationInterfaceOrientation
调用了UpdateLandCapeView
方法。然而,我还必须向LandscapeView添加代码,使其消失,因为我只能再依赖通知了。当我运行应用程序时,我认为它工作得很好,发现我只会让事情变得更糟。景观视图不会淡入淡出,而是呈对角滑动,视图有时会在不应该的时候显示。你能再帮我一点忙吗?(上面贴了代码)非常感谢。