改变UIView的方向变化 [英] Changing UIView on orientation change

查看:125
本文介绍了改变UIView的方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

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

  #pragma mark  -  
#pragma mark InterfaceOrientationMethods

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

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration :( NSTimeInterval)持续时间{
[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){
//更改纵向视图的视图和子视图帧
}
else {
//更改视图和景观视图的子视图框架
}

[UIView commitAnimations];
}

希望这有帮助。


Hey all. I have a fairly simple question. I am developing a "rich" iPad app, and I have two background images specifically designed for landscape and portrait. I'd like this ImageView to automatically change depending on the devices orientation. (like pretty much all of Apples iPad apps).

Can anyone point me in the right direction? I'm assuming it would be something I do on viewDidLoad..

解决方案

The best thing you can do is to change the frames of your subview frames according to your interface orientations. You can do it like:

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

Hope this helps.

这篇关于改变UIView的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆