如何在iOS上旋转自定义启动画面?
我的启动画面正在工作,但我的应用程序在横向模式下工作,启动画面以默认肖像模式显示。如何在iOS上旋转自定义启动画面?
如何启动应用程序,以便启动屏幕在风景模式之间旋转,如我的应用程序?
我用下面的代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
else {
return NO; }
}
和闪屏
-(void)displayScreen {
UIViewController *displayViewController=[[UIViewController alloc] init];
displayViewController.view = displaySplashScreen;
[self presentModalViewController:displayViewController animated:NO];
[self performSelector:@selector(removeScreen) withObject:nil afterDelay:3.0];
}
-(void)removeScreen
{ [[self modalViewController] dismissModalViewControllerAnimated:YES];
}
但我怎么可以把显示屏内的旋转?
@zoul - 迄今为止喜欢这个解决方案。但是,如果/当该视图有任何子视图时 - 它们不显示。有任何想法吗?
更新:
通过增加一个子视图中-startupImageWithOrientation:
不 self.view创建的UIView修复了这个问题。
- (UIView *)startupImageWithOrientation:(UIInterfaceOrientation)io{
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"Default-%@.png", UIInterfaceOrientationName(io)]];
UIView *aView = [[UIImageView alloc] initWithImage:img];
[aView setFrame:[[UIScreen mainScreen] applicationFrame]];
// define the version number label
self.versionNumberLabel_iPadSplashScreen.text = [NSString stringWithFormat:@"Version %@",
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
[aView addSubview:self.versionNumberLabel_iPadSplashScreen];
return [aView autorelease];
}
啊哈。如果你想显示你自己的启动画面,你应该为你创建一个特殊的视图控制器,你已经做了。我认为你可以简化自动旋转查询代码:
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) foo
{
return YES; // all interface orientations supported
}
现在你必须考虑不同方向的启动画面。你有独立的风景和肖像飞溅图像吗?如果是的话,你可以这样做:
- (UIView*) startupImageWithOrientation: (UIInterfaceOrientation) io
{
UIImage *img = [UIImage imageNamed:[NSString
stringWithFormat:@"Default-%@.png", UIInterfaceOrientationName(io)]];
UIView *view = [[UIImageView alloc] initWithImage:img];
[view setFrame:[[UIScreen mainScreen] applicationFrame]];
return [view autorelease];
}
- (void) loadView
{
self.view = [self startupImageWithOrientation:self.interfaceOrientation];
}
- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) io
duration: (NSTimeInterval) duration
{
self.view = [self startupImageWithOrientation:io];
self.view.transform = CGAffineTransformFromUIOrientation(io);
}
有两个叫,你可以的东西,这些成一个单独的文件实用功能:
NSString *UIInterfaceOrientationName(UIInterfaceOrientation io)
{
return UIInterfaceOrientationIsPortrait(io) ? @"Portrait" : @"Landscape";
}
CGAffineTransform CGAffineTransformFromUIOrientation(UIInterfaceOrientation io)
{
assert(io <= 4);
// unknown, portrait, portrait u/d, landscape L, landscape R
static float angles[] = {0, 0, M_PI, M_PI/2, -M_PI/2};
return CGAffineTransformMakeRotation(angles[io]);
}
这是一个有点乱,我很有兴趣更简单的解决方案。
如果您已锁定方向,则defult.png的解析会影响您的应用程序方向。
例如,如果使用1024x768闪屏图像,并且初始视图不支持通过方向锁定进行肖像查看,则可能会导致视觉UI对象出现在屏幕外(特别是涉及动画时),因为视图将尝试呈现即使设备可能处于横向状态,它本身也处于纵向配置中。
一般而言,1024x768图像意味着肖像,而768x1024图像意味着风景。
如果这还不够,或者您想要从初始默认图像无缝地转到例如登录屏幕,那么您可以使用视图控制器来“继续”飞溅。
加载一切正常viewControllers到窗口,然后把你的“扑通”的viewController中然后在splashController使用shouldAutorotateToInterfaceOrientation
方法来设置正确的图像(上ImageViewController):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
switch ([[UIDevice currentDevice] orientation])
{
case UIInterfaceOrientationLandscapeRight:
splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default"] ofType:@"png"]];
break;
case UIInterfaceOrientationPortrait:
splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default"] ofType:@"png"]];
break;
default:
splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default2"] ofType:@"png"]];
break;
}
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
的配置我用过的图片可能不适合你,所以可能需要一些实验。
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
// take action here , when phone is "Portrait"
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
action 4 LandscapeLeft
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//PortraitUpsideDown
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
//LandscapeRight
}
请注意,您应该返回YES方法shouldAutorotateToInterfaceOrientation:
这篇文章对Xcode确实没什么用处。所以,我已经从你的标题中删除了这个词。 – 2010-12-07 06:53:32