2010-04-16 73 views
1

我正在开发一个iPad应用程序,它基本上是一个大的绘图画布,旁边有几个按钮。 (听起来不太原创,是吗?:P)iPhone操作系统:只旋转按钮图像,而不是视图

无论用户如何拿着设备,画布应该保持原位,不应旋转。实现这一目标的最简单方法是仅支持一个方向。

但是,当设备旋转时,我想让按钮上的图像旋转(就像在iPhone相机应用程序中一样)。 UIPopoverControllers还应该使用用户当前的方向(而不是横向出现)。

达到此目的的最佳方法是什么?

(我想我可以在画布的AffineTransform旋转回原位,但我不认为这是理想的。)

提前感谢!

回答

0

我发现了一个这样做的方法,类似于戴夫德隆提出的。

使用转换工作,但它并不理想。尽管最终结果(动画结束)是我想要的,但它仍然会显示某种不稳定的旋转动画。

后来我发现这一点:

https://developer.apple.com/iphone/library/qa/qa2010/qa1688.html

这表示,第二(或第三等)的UIViewController添加到窗口不会收到旋转事件,因此永远不会转动。这工作!

我用空白的视图创建了一个'假的'UIViewController,并添加了第一个视图控制器。这会接收旋转事件,然后将其传递给其他视图控制器,然后可以选择是否旋转 - 整个视图或只是按钮标签。

这是一个有点哈克...但我猜用户不会注意到。使用willRotateToInterfaceOrientation方法

0

就喷过一个想法(不知道是否会工作或没有)......

也许你可以让你的屏幕由支持所有方向,但在画布由一个控制的UIViewController控制,即仅支持单个方向(即,在其shouldAutorotate...方法中返回NO)。

如果这不起作用,我可能会只用affineTransform路线。

+0

据我的经验告诉,嵌套UIViewControllers不会启用“底部”UIViewController来接收自动旋转方法。我也可能会使用'affineTransform'。 – bddckr 2010-04-16 20:26:27

+0

@ChriB很高兴知道!正如我所说,我不确定这是否会起作用。 :) – 2010-04-16 20:54:54

0

,你应该能够使用一些简单的逻辑来换出图片:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    //The following if statement determines if it is an iPad, if it is then the interface orientation is allowed. This line can be taken out to support both iPhones an iPads. 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     //Swap images and use animations to make the swap look "smooth" 
     //NSLog(@"Landscape Right"); 

    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     //Swap images and use animations to make the swap look "smooth" 
     //NSLog(@"Landscape Left"); 

    } else { 
     //Swap images and use animations to make the swap look "smooth" 
     //NSLog(@"Portrait"); 
    } 
} else { 

} 
} 

要在界面编程方式更改图像:

[myUIImageView setImage:[UIImage imageNamed:@"myImage.png"]]; 

此外,为了请确保视图不会自动旋转,请使用shouldAutorotateToInterfaceOrientation方法来告诉您的应用保持肖像状态。