【问题标题】:how to identify the orientation of viewController's subclass in ipad如何在 ipad 中识别 viewController 的子类的方向
【发布时间】:2012-04-23 07:04:07
【问题描述】:

我有一个 viewController 类示例 FirstView,它有一个子类 ex。第二视图。 SecondView 是 FirstView 的子类。现在我想在第二个视图中检查方向?

如何识别 secondView 方向?我检查了以下方法,但它不起作用。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

当我改变方向时,不会调用任何方法。

有没有办法识别自定义子类的方向?

我该如何解决这个问题?

【问题讨论】:

    标签: objective-c ipad ios5 xcode4.2


    【解决方案1】:

    [[UIApplication sharedApplication] statusBarOrientation] 返回当前设备方向。

    【讨论】:

    • 我在 viewDidLoad 中获得了当前设备,但是当我改变方向时如何识别方向?改变方向时调用哪个方法?
    • - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 在旋转之前调用,- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 在旋转之后调用。
    【解决方案2】:

    我认为您可以使用 UIAccelerometer。您只需要实现 UIAccelerometerDelegate。

    只是为了使用加速度计获取视图的当前方向,您可以使用以下代码:

    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
    {
        // Get the current device angle
        float x = [acceleration x];
        float y = [acceleration y];
        float angle = atan2(y, x); 
    
    
    
     if([acceleration z]>-0.84)
     {
            if(angle >= -2.25 && angle <= -1)  // Current Orientation = Portrait
            {
                 //Do the work
            } 
            else if(angle >= -0.35 && angle < 0.20) //Current Orientation = Landscape Left
            {
                  //Do the work
            }
            else if(angle >= 0.90 && angle <= 2.00) //Current Orientation = Portrait Upside Down
            {
                 //Do the work
            }
            else if(angle <= -2.70 || angle >= 2.5) //Current Orientation = Landscape Right
            {
                  //Do the work
            }
       }
    }
    

    如果您需要更多帮助,请告诉我。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      在你的超类中,你应该调用它的超类的orientation方法:

      Class A {
          - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
               [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
          }
      }
      
      class b inherits class a {
          - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
           NSLog(@"Your stufs");    
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多