繁体   English   中英

改变UIView的方向变化

[英]Changing UIView on orientation change

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

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

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

 #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];
}

希望这可以帮助。

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

`

- (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的背景。 唯一的缺点是,当前的背景图像在“界面”构建器中不可见,因为它是使用代码处理的。

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

您可以通过观察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")
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM