【发布时间】:2010-04-29 03:11:21
【问题描述】:
大家好。我有一个相当简单的问题。我正在开发一个“丰富”的 iPad 应用程序,并且我有两个专门为横向和纵向设计的背景图像。我希望这个 ImageView 根据设备的方向自动改变。 (就像几乎所有的苹果 iPad 应用程序一样)。
谁能指出我正确的方向?我假设这将是我在 viewDidLoad 上做的事情。
【问题讨论】:
标签: objective-c iphone uiview uiviewcontroller
大家好。我有一个相当简单的问题。我正在开发一个“丰富”的 iPad 应用程序,并且我有两个专门为横向和纵向设计的背景图像。我希望这个 ImageView 根据设备的方向自动改变。 (就像几乎所有的苹果 iPad 应用程序一样)。
谁能指出我正确的方向?我假设这将是我在 viewDidLoad 上做的事情。
【问题讨论】:
标签: objective-c iphone uiview uiviewcontroller
您可以做的最好的事情是根据您的界面方向更改子视图框架的框架。你可以这样做:
#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];
}
希望这会有所帮助。
【讨论】:
UIInterfaceOrientationIsPortrait() 和 UIInterfaceOrientationIsLandscape() 确实提高了这类情况的可读性......但是,很好的解决方案,尽管如此!
我实际上想出了一个非常简单的替代方法。由于我只是在更改背景图像,因此添加此..
`
- (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 的背景。唯一的缺点是,当前的背景图像在 Interface builder 中不可见,因为它是通过代码处理的。
【讨论】:
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"]];
}
再次感谢马杜普
【讨论】:
您可以通过查看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")
}
}
}
【讨论】: