在处理多个方向时,我应该使用哪些最佳实践?

时间:2011-12-14 05:36:27

标签: iphone ios

请原谅我的无知。传统上我一直在做这样的事情,但感觉就像很多不必要的工作,我不确定是否有更简单的方法来实现这一目标。

// logoView dimensions
static const CGFloat LOGO_VIEW_X_PORT = 42;
static const CGFloat LOGO_VIEW_Y_PORT = 20;
static const CGFloat LOGO_VIEW_W_PORT = 237;
static const CGFloat LOGO_VIEW_H_PORT = 82;
// 
static const CGFloat LOGO_VIEW_X_LAND = 142;
static const CGFloat LOGO_VIEW_Y_LAND = 20;
static const CGFloat LOGO_VIEW_W_LAND = 237;
static const CGFloat LOGO_VIEW_H_LAND = 82;

- (void)viewDidLoad {
    [super viewDidLoad];

    // init logo view
    self.logoView = [[UIImageView alloc] initWithImage:...];

    // determine device orientation and set dimensions 
    if (self.interfaceOrientation == UIDeviceOrientationPortrait || 
        toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_PORT, LOGO_VIEW_Y_PORT, LOGO_VIEW_W_PORT, LOGO_VIEW_H_PORT);
    } else {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_LAND, LOGO_VIEW_Y_LAND, LOGO_VIEW_W_LAND, LOGO_VIEW_H_LAND);
    }

    [self.view addSubview:self.logoView];

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIDeviceOrientationPortrait || 
        toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_PORT, LOGO_VIEW_Y_PORT, LOGO_VIEW_W_PORT, LOGO_VIEW_H_PORT);
        // long list of other views...
    } else {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_LAND, LOGO_VIEW_Y_LAND, LOGO_VIEW_W_LAND, LOGO_VIEW_H_LAND);
        // long list of other views...
    }
}

我是否应该遵循更好的协议来实现这一目标,或者这或多或少是每个人如何处理多个方向?

1 个答案:

答案 0 :(得分:1)

查看所有图形对象的autoResizingMask选项。这应该会有所帮助。