代码之家  ›  专栏  ›  技术社区  ›  Pieter Jongsma

iphoneos:只旋转按钮图像,而不是视图

  •  1
  • Pieter Jongsma  · 技术社区  · 16 年前

    我正在开发一个iPad应用程序,它基本上是一个大画布,旁边有几个按钮(听起来不是很有创意,是吗?:(P)

    无论用户如何握住设备,画布都应保持在原位,不应旋转。实现这一点的最简单方法是只支持一个方向。

    但是,我希望按钮上的图像在设备旋转时旋转(就像在iPhone相机应用程序中)。uipopOvercontroller还应该使用用户当前的方向(而不是侧着显示)。

    实现这一目标的最佳方法是什么?

    (我想我可以用仿射变换将画布旋转回原位,但我认为这并不理想。)

    3 回复  |  直到 16 年前
        1
  •  0
  •   Dave DeLong    16 年前

    只是滔滔不绝地说出一个想法(不确定是否可行)。。。

    UIViewController 支持所有方向,但画布由仅支持单个方向(即返回)的画布控制 NO shouldAutorotate... 方法)。

        2
  •  0
  •   Pieter Jongsma    16 年前

    我发现了一种方法,类似于戴夫·德龙的建议。

    使用变换是可行的,但并不理想。虽然最终结果(动画的结尾)是我想要的,但它仍然会显示某种不稳定的旋转动画。

    然后我发现:

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

    它有点黑。。。但我猜用户不会注意到。

        3
  •  0
  •   Sam Spencer    14 年前

    使用 willRotateToInterfaceOrientation

    - (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 方法来告诉你的应用程序保持纵向。