改变UIView的方向变化

时间:2010-04-29 03:11:21

标签: objective-c iphone uiview uiviewcontroller

嘿所有人。我有一个相当简单的问题。我正在开发一个“丰富”的iPad应用程序,我有两个专门为风景和肖像设计的背景图像。我希望这个ImageView根据设备方向自动更改。 (就像几乎所有的苹果iPad应用程序一样)。

有人能指出我正确的方向吗?我假设它是我在viewDidLoad上做的事情..

4 个答案:

答案 0 :(得分:22)

您可以做的最好的事情是根据您的界面方向更改子视图帧的帧。你可以这样做:

 #pragma mark -
 #pragma mark InterfaceOrientationMethods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
    }
    else{   
        //change the view and subview  frames for the landscape view
    }

    [UIView commitAnimations];
}

希望这有帮助。

答案 1 :(得分:9)

我实际上找到了一个非常简单的替代方法。由于我只是更改背景图片,添加此..

`

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation ==
        UIInterfaceOrientationPortraitUpsideDown) { 
        [brownBackground setImage:[UIImage imageNamed:@"Portrait_Background.png"]];
    } else {
        [brownBackground setImage:[UIImage imageNamed:@"Landscape_Background.png"]];
    }
}

`

根据方向更改声明的UIImageView的背景。唯一的缺点是,当前背景图像在“界面”构建器中不可见,因为它是用代码处理的。

答案 2 :(得分:7)

Madhup的方法的一小部分,很棒。我发现我需要将它添加到viewDidLoad以设置纵向或横向的初始背景图像:

// set background image
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"portraitBG.png"]];
} else {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"landscapeBG.png"]];
}

再次感谢Madhup

答案 3 :(得分:1)

您可以通过观察是否bounds.width > bounds.height

将此完全封装在您的UIView中

如果您正在编写一个小型的自我感知控件,这可能是理想的。

class MyView: UIView {
  override func layoutSubviews() {
    super.layoutSubviews()
    if bounds.height > bounds.width {
      println("PORTRAIT. some bounds-impacting event happened")
    } else {
      println("LANDSCAPE")
    }
  }
}