当方向改变时,将图像旋转90度并保持在同一位置

10
我最终做到了这一点: 支持多个方向的最简单方法?当应用程序处于横向时,如何加载自定义NIB?非常好用!
我在Photoshop中制作了一张图像,想将其用作iPad应用程序中信息屏幕的背景。图像包含文本和一些图标。在图像周围有一个绿色边框。
我试图实现的效果是:
当用户从纵向方向转向横向方向时,我希望图像(仅限框架和图标)旋转90度,使图像出现在横向模式下,而不是在横向视图中呈现纵向框架。文本和图标是分离的(我已经将它们组织在不同的UIImageView中),它们应该旋转90度。
我已经完成的工作如下:
尝试了一下这种方法:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:

并尝试这样做:

self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);

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

编辑

制作了一张图片:

enter image description here

正如图像所示,图像背景(上面有一些定制的图形,在纵横两个方向上都很好看)从纵向变为横向并旋转,以便在横向时不会呈现为纵向。图标与背景分离,因此它们也需要旋转,以达到正确的方向(这是社交图标)。然而,文本仍然处于相同位置,只旋转90度而没有重新定位。

确保视图层的锚点位于中心,你看到的是期望效果吗? - Daniel
我不明白为什么你分享了一个涉及多个布局解决方案的答案。在我看来,问题的重点是只有一个布局和一个UIInterfaceOrientation,其中一些图像或按钮可以根据UIDeviceOrientation旋转,对吧?无论如何,这就是我来到这里的原因,因为我想要类似于iOS相机应用程序的东西。被接受的答案很好地解决了我的问题。 - Maxi Mus
1个回答

21

我理解你的问题是想要单独旋转紫色和红色正方形而不是整个界面。

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

Interface Bulder screenshot

黑色正方形是一个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。我会朝着那个方向尝试一些东西,看起来是个不错的答案。如果我成功了,我会告诉你的。 - LuckyLuke
我有一个问题,使用这种方法状态栏不会旋转,对吗? - LuckyLuke
@Andreas 状态栏不会旋转。我更新了代码,使其可以旋转,但当状态栏移动时,屏幕视图的框架可能需要进行一些更改。 - Jiri
谢谢!当我在做旋转相关的事情时,你的方法将成为未来的参考。然而,我找到了这个链接:https://dev59.com/znE95IYBdhLWcg3wCJJf#2496719 它解决了主要问题,即在旋转时交换图像。 - LuckyLuke
很少有这样的情况,我可以复制粘贴一个答案,然后它完美地工作!谢谢! - Zoltan Varadi
很棒的答案,以下是一些快速指针:在XCode项目设置中,设备方向所指的当然是界面方向。整个状态栏东西对于大多数人来说可能不是必需的,因为如果您只有一个固定的界面方向,那么为什么要翻转状态栏,如果有它。最后,在 switch 语句中需要 return 语句,否则应用程序将始终执行回到0°的旋转,这意味着将视图(元素)返回到原始方向。 - Maxi Mus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接