Objective c 更改方向更改的UIView
大家好。我有一个相当简单的问题。我正在开发一款“丰富”的iPad应用程序,我有两张专门为风景画和肖像画设计的背景图片。我希望此图像视图根据设备方向自动更改。(就像几乎所有的苹果iPad应用程序一样)Objective c 更改方向更改的UIView,objective-c,iphone,uiview,uiviewcontroller,Objective C,Iphone,Uiview,Uiviewcontroller,大家好。我有一个相当简单的问题。我正在开发一款“丰富”的iPad应用程序,我有两张专门为风景画和肖像画设计的背景图片。我希望此图像视图根据设备方向自动更改。(就像几乎所有的苹果iPad应用程序一样) 谁能给我指出正确的方向吗?我假设这是我在viewDidLoad上所做的事情。您可以做的最好的事情是根据您的界面方向更改子视图帧的帧。你可以这样做: #pragma mark - #pragma mark InterfaceOrientationMethods - (BOOL)shouldAut
谁能给我指出正确的方向吗?我假设这是我在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];
}
希望这能有所帮助。我实际上想出了一个非常简单的替代方法。因为我只是在更改背景图像,所以添加了这个 ` `
基于方向更改已声明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"]];
}
再次感谢Madhup您可以通过查看
bounds.width>bounds.height
如果您正在编写一个小型的、有自我意识的控件,这可能是可取的
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
if bounds.height > bounds.width {
println("PORTRAIT. some bounds-impacting event happened")
} else {
println("LANDSCAPE")
}
}
}
宏
UIInterfaceOrientationIsPortrait()
和UIInterfaceOrientationIsLandscape()
的可能重复确实提高了这些类型情况的可读性。。。但是,尽管如此,这是一个很好的解决方案@内特:谢谢你的建议。。更改了答案。:)
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
if bounds.height > bounds.width {
println("PORTRAIT. some bounds-impacting event happened")
} else {
println("LANDSCAPE")
}
}
}