iPad方向检查UIView类? [英] iPad Orientation Checking UIView Class?

查看:88
本文介绍了iPad方向检查UIView类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView 类,我将其添加到我的主 UIViewController 中,我需要检查方向设备(iPad)在应用程序启动时,在 viewDidLoad 方法中。但是因为该类是 UIView (不是 UIViewController )我不能使用 willAnimateRotationToInterfaceOrientation 。

I have a UIView class which I add to my main UIViewController and I need to check the orientation of the device (iPad) at the launch of the app, in the viewDidLoad method. However because the class is a UIView (not UIViewController) I can't use methods such as willAnimateRotationToInterfaceOrientation.

所以我试图在我的 UIView 类中使用它:

So I attempted to use this in my UIView class:

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
    ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {

然而,测试一些断点,无论方向是什么,声明永远不会被调用,它的跳过权利通过它。那么你建议我做些什么来克服这个问题?

However, testing with some breakpoints, whatever the orientation is, the of statement is never called, its skips right passed it. So what do you suggest I do to overcome this issue?

我需要以某种方式检测UIView类的方向。

I need to somehow detect the orientation from the UIView class.

谢谢。

推荐答案

你在哪里放票?该位置可以很容易地解释为什么它没有被调用。要获得轮换信息,您可以注册通知,或让视图控制器在您的视图中调用方法。后者的示例代码:

Where are you placing the check? The location could easily explain why it's not being called. To get rotation info, you could register for a notification, or have your view controller call a method in your view. Sample code for the latter:

// ViewController.m
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.customView willRotateToOrientation:toInterfaceOrientation];
}

// CustomView.m
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
    // Handle rotation
}

视图控制器方法是您覆盖的方法;视图的方法应该在标题中声明。

The view controller method is one you override; the view's method should be declared in a header.

更新:
或者,您可以在控制器中找到旋转的` viewWillAppear':

Update: Alternatively, you can find out the rotation in the controller's `viewWillAppear':

// ViewController.m
- (void)viewWillAppear {
    [self.customView willRotateToOrientation:[[UIDevice currentDevice] orientation];
}

将相应调用视图中的方法。

The method in your view will be called accordingly.

这篇关于iPad方向检查UIView类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆