【问题标题】:Rotate the view only in ios仅在 ios 中旋转视图
【发布时间】:2015-08-05 06:30:54
【问题描述】:

我的应用程序仅支持纵向模式。我有要求只根据设备方向旋转特定的UIView

你能帮帮我吗?

【问题讨论】:

  • 单单UIView是什么意思?
  • 仅查看...不是 viewController
  • 当您的设备方向改变时,您是否需要旋转视图?
  • 是的。我必须根据设备方向更改特定视图。

标签: ios xcode uiinterfaceorientation cgaffinetransform screen-rotation


【解决方案1】:
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
    // set your view frame work for portrait mode

}
else {
// set new frame of view for landscape mode.
}

希望您能理解。 谢谢

【讨论】:

  • 对不起..我没听懂..我只能使用 CGAffineTransform 或其他方法来旋转视图。
【解决方案2】:

在您的 viewdidload 或 init 中添加下面的代码

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver: self selector:   @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];

在 deviceOrientationDidChange 函数中,您将获得设备方向,据此您可以更新您的视图

- (void)deviceOrientationDidChange:(NSNotification *)notification {
     //Obtain current device orientation
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

      //Do my thing
 }

【讨论】:

  • 谢谢。这是我做过的。我需要根据设备方向旋转视图。
  • @YanoRatheez: CGAffineTransform transform = CGAffineTransformMakeRotation(angle); [你的视图 setTransform:transform];此处设置与您的设备方向相对应的角度。
【解决方案3】:

经过长时间的尝试,我使用CGAffineTransform 找到了答案..

- (void)deviceOrientationDidChange:(NSNotification *)notification {
    //Obtain current device orientation
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    CGFloat angle;
    switch (orientation) {
        case UIDeviceOrientationPortraitUpsideDown:
            angle = M_PI;
            break;
        case UIDeviceOrientationLandscapeRight:
            angle = - M_PI_2;
            break;
        case UIDeviceOrientationLandscapeLeft:
            angle = M_PI_2;
            break;
        case UIDeviceOrientationPortrait:
        default:
            angle = 0.f;
            break;
    }
    animationView.transform= CGAffineTransformMakeRotation(angle);
    animationView.frame = self.view.frame;
}

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多