且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-13 13:23:16

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

I understand your question as that you are trying to not to rotate the interface as whole but to rotate the purple and red squares individually.

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

I created a UIViewController that resembles your layout.

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

The black square is a UIView and the white square is there only so that I can tell when the black view rotates. This view is wired to view1 property on the controller.

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

There are 4 buttons that are wired to btnx (x runs 1 through 4) properties.

因为我不想再自动旋转界面我只支持肖像方向。

Since I no longer want to auto rotate the interface I only support the portrait orientation.

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

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

To do the rotation manually, I added a method to the ViewController. It determines the angle it needs to rotate the components from portrait to current orientation, creates a rotation transform and applies it to all outlets.

- (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中的应用程序:didFinishLaunchingWithOptions:

The last thing to do is to get the OS to call my method. To achieve that I added the following code to application:didFinishLaunchingWithOptions: in the AppDelegate.

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

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

I am not sure whether this is exactly what you wanted but I believe it is at least similar so you can get some ideas from it how to solve your problem. I can provide source code for a working iPad application that I created to illustrate this.