方向改变时,在同一位置旋转图像90度

Luc*_*uke 10 iphone uiimageview cgaffinetransform

结束这样做: 支持多个方向的最简单方法?当应用程序在Landscape中时如何加载自定义NIB?工作精湛!

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

我想要达到的效果是:

当用户从纵向朝向横向时,我希望图像(仅框架和图标)旋转90度,使图像以横向模式显示,而不是在横向上具有框架的纵向视图.文本和图标是分离的(我在不同的UIImageView中组织的不同层)它们将旋转90度.

我已经完成的工作如下:

用这种方法进行了一些实验:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:
Run Code Online (Sandbox Code Playgroud)

并试图这样做:

self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);
Run Code Online (Sandbox Code Playgroud)

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

编辑

制作图像:

在此输入图像描述

由于图像显示图像背景(一些在两个方向看起来都很好的自定义图形)从纵向到横向并旋转,所以它在横向上不显示为肖像,图标与背景分离,因此它们需要旋转为因为他们需要处于正确的方向(这是社交图标).然而,文本位于相同的位置,它只旋转90度而不重新定位.

Jir*_*iri 21

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

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

Interface Bulder截图

黑色正方形是一个UIView,白色正方形只有这样我才能知道黑色视图何时旋转.此视图连接到view1控制器上的属性.

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

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

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

为了手动完成旋转,我向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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

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