【问题标题】:how to prevent rotation of subview to portrayed mode如何防止子视图旋转到描绘模式
【发布时间】:2011-06-20 14:25:22
【问题描述】:

我正在尝试在点击按钮时向主视图添加子视图。

  • 主视图支持横向模式和纵向模式。

  • 子视图只支持横向模式。

我该怎么做?

【问题讨论】:

    标签: iphone


    【解决方案1】:

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 中,您可以对不希望旋转的视图应用转换:

    view.transform = CGAffineTransformMakeRotation(M_PI/2);
    

    你可以把它放在一个动画块中,让它跟随旋转动画:

    [UIView setAnimationDuration:duration];
    [UIView beginAnimations:nil context:NULL];
    view.transform = CGAffineTransformMakeRotation(M_PI/2);
    [UIView commitAnimations];
    

    旋转发生在视图原点周围,这会使定位变得棘手。为了正确定位视图,我首先会忽略旋转并确保设置支柱和弹簧,以便视图处于正确位置而无需旋转。正确定位视图后,重新启用旋转并调整视图框架的原点,使视图位于您想要的位置。可以应用平移转换而不是调整框架(转换可以与CGAffineTransformConcat 结合使用)。使用哪种方法生成最易读的代码。

    小心,这种视图操作会让人感到困惑。从技术上讲,您不想旋转的视图实际上是正在旋转的视图!您可能还需要根据开始和结束方向更改旋转量。

    【讨论】:

      【解决方案2】:

      为您想要添加的所有横向视图获取标签值,然后在将它们添加到主视图之前检查标签值并添加它们。

      只要调用 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation,您就可以通过维护一个标志来了解当前方向。

      【讨论】:

        【解决方案3】:

        您是否点击了此链接

        iPhone app in landscape mode, 2008 systems

        或者只是将这行代码添加到要以横向模式显示的 viewController 的 .m 文件中

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
            if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            {
                return YES;
            }
            return NO;
        }
        

        【讨论】:

        • 嘿,我想你没有得到我的要求,如果我使用这个代码,它支持所有方向,但我需要防止这个方向只有一个子视图。
        • 当视图should rotate时返回YES,当视图should not rotate时返回NO
        • 在 if 条件下,您可以拥有任何类型的方向支持,例如 UIInterfaceOrientationLandscapeLeft 和纵向模式
        【解决方案4】:

        我通过旋转屏幕和调整视图框架来解决这个问题。

        if (toorientation == UIInterfaceOrientationPortrait) {
                sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(0));
            }
            else if (toorientation == UIInterfaceOrientationPortraitUpsideDown){
                sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(180));
            }
            else if (toorientation == UIInterfaceOrientationLandscapeLeft){
                sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(-90));
            }
            else if (toorientation == UIInterfaceOrientationLandscapeRight){
                sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(90));
            }
        

        【讨论】:

          猜你喜欢
          • 2023-03-31
          • 1970-01-01
          • 2014-11-27
          • 2011-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多