【问题标题】:Rotating an image 90 degrees on same position when orientation changes方向改变时将图像在同一位置旋转 90 度
【发布时间】:2011-09-18 06:53:36
【问题描述】:

最终这样做了: Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape? 非常棒!

我在 Photoshop 中制作了一张图像,我想将其用作 iPad 应用程序中信息屏幕的背景。该图像还包含文本和一些图标。在图像周围我有一个绿色的边框。

我想要达到的效果是:

当用户从纵向转到横向时,我希望图像(只是框架和图标)旋转 90 度,以便图像以横向模式显示,而不是在横向模式下显示框架的纵向视图。文本和图标是解耦的(我在不同的 UIImageView 中组织了不同的图层)它们应该旋转 90 度。

我已经做的如下:

用这个方法试验了一下:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:

并尝试这样做:

self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);

我认为它将 btnFacebook 属性向右旋转 90 度(如果我错了,请纠正我),因为它指定了正弧度。我似乎无法让它正常工作。这不应该将按钮围绕框架中的中心坐标旋转 90 度吗?那不会导致按钮位置发生变化(按钮是方形的)?

编辑

制作图片:

由于图像显示图像背景(其上的一些自定义图形在两个方向上看起来都不错)从纵向变为横向并旋转,因此它不会在横向中显示为纵向,因此图标与背景分离,因此它们需要也可以旋转,因为它们需要处于正确的方向(它是社交图标)。然而文本在同一个位置,它只旋转 90 度而不重新定位。

【问题讨论】:

  • 确保视图层的锚点位于中心,您看到的是什么而不是预期的效果?
  • 我不明白您为什么分享了一个答案,它指的是具有多种布局的解决方案。恕我直言,问题的全部意义在于只有一个布局和一个UIInterfaceOrientation,其中一些图像或按钮可以根据UIDeviceOrientation 旋转,不是吗?无论如何,这就是让我来到这里的原因,因为我想要类似于股票 iOS 相机应用程序的东西。接受的答案工作正常。

标签: iphone uiimageview cgaffinetransform


【解决方案1】:

我理解你的问题,因为你试图不旋转整个界面,而是单独旋转紫色和红色方块。

我创建了一个类似于您的布局的UIViewController

黑色方块是 UIView,白色方块只是为了让我知道黑色视图何时旋转。此视图连接到控制器上的view1 属性。

有 4 个按钮连接到 btnx(x 运行 1 到 4)属性。

由于我不再想自动旋转界面,我只支持纵向。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

为了手动进行旋转,我向 ViewController 添加了一个方法。它确定将组件从纵向旋转到当前方向所需的角度,创建旋转变换并将其应用于所有出口。

- (void)deviceDidRotate:(NSNotification *)notification
{
    UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
    double rotation = 0;
    UIInterfaceOrientation statusBarOrientation;
    switch (currentOrientation) {
        case UIDeviceOrientationFaceDown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationUnknown:
            return;
        case UIDeviceOrientationPortrait:
            rotation = 0;
            statusBarOrientation = UIInterfaceOrientationPortrait;
            break;
        case UIDeviceOrientationPortraitUpsideDown:   
            rotation = -M_PI;
            statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
            break;     
        case UIDeviceOrientationLandscapeLeft:     
            rotation = M_PI_2;   
            statusBarOrientation = UIInterfaceOrientationLandscapeRight;
            break;  
        case UIDeviceOrientationLandscapeRight:    
            rotation = -M_PI_2;
            statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
            break;
    }
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        [self.btn1 setTransform:transform];
        [self.btn2 setTransform:transform];
        [self.btn3 setTransform:transform];
        [self.btn4 setTransform:transform];
        [self.view1 setTransform:transform];
        [[UIApplication sharedApplication] setStatusBarOrientation:statusBarOrientation];
    } completion:nil];
}

最后要做的是让操作系统调用我的方法。为此,我将以下代码添加到 AppDelegate 中的application:didFinishLaunchingWithOptions:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self.viewController selector:@selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

我不确定这是否正是您想要的,但我相信它至少是相似的,因此您可以从中获得一些想法来解决您的问题。我可以为我创建的一个工作 iPad 应用程序提供源代码来说明这一点。

【讨论】:

  • 这可能是我需要的 Jiri,我会朝那个方向尝试一些东西,似乎是一个不错的答案。如果我让它工作,我会告诉你。
  • 我有一个问题,状态栏不会用这种方法旋转,对吧?
  • @Andreas 状态栏不会旋转。我更新了代码,但它可能需要在状态栏移动时对屏幕视图的框架进行一些更改。
  • 谢谢!当我用旋转做这样的事情时,你的方法将成为未来的参考,但是我发现了这个:stackoverflow.com/questions/2496554/… 它解决了主要问题,在旋转时交换图像。
  • 我可以复制粘贴答案真的很少见,而且效果很好!谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-08-03
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多