【问题标题】:IPhone 3, iPhone 4, and iPad orientation handlingiPhone 3、iPhone 4 和 iPad 方向处理
【发布时间】:2011-03-08 17:29:36
【问题描述】:

我有一个带有 tableView 和 mapView 的视图。在竖屏模式下,我希望 mapView 占据屏幕顶部的 60%,而表格视图占据屏幕底部的 40%。在横向中,我希望 mapView 在右侧占据屏幕的 45%,而 tableView 在左侧占据 55%。

因此,我认为这样的事情应该可以工作:

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
     UIScreen* mainscr = [UIScreen mainScreen];
     CGSize screenSize = mainscr.currentMode.size;

 if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft 
    || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
 {
  tableview.frame = CGRectMake(0, 0 , (screenSize.height*.55), screenSize.width);
  mapView.frame = CGRectMake((screenSize.height*.55), 0, (screenSize.height*.45), screenSize.width);
 }
 else 
 {
  tableview.frame = CGRectMake(0, (screenSize.height*.6) ,screenSize.width, (screenSize.height*.4));
  mapView.frame = CGRectMake(0, 0,screenSize.width, (screenSize.height*.6));
 }

    }

此代码在具有版本 4 操作系统的 iphone 模拟器上运行良好,但是当我尝试在 iphone 4 模拟器或 ipad 模拟器上运行它时,视图完全关闭(我只能看到大约四分之一纵向地图,没有桌子,横向地图我只能看到桌子)。

我不明白为什么会这样——因为我只是简单地缩放视图以匹配屏幕大小,所以无论设备的分辨率如何,上述代码之类的东西都不应该正常工作吗?我能够通过使用 iphone、iphone 4 和 ipad 的不同值进行缩放来进行“修复”,但我使用的数字是通过反复试验发现的,它们似乎没有多大意义。

有人可以指点我为所有设备处理这些方向更改的正确方法吗?非常感谢!

【问题讨论】:

  • 你为什么使用currentMode?为什么不mainscr.bounds? IMO 你根本不应该在这里使用UIScreen,而是使用两个视图的超级视图作为参考矩形。
  • 我只选择使用 currentMode 是因为我认为它足以确定设备的分辨率,我认为这就是计算方向变化的位置所需的全部内容。我不太清楚你所说的两个视图的超级视图是什么意思。您能否详细说明一下那是什么/如何获得它?谢谢!

标签: iphone iphone-sdk-3.0 ipad ios-simulator ios4


【解决方案1】:

我做过类似的事情。以下是我将如何完成您想做的事情

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGSize screenSize = self.view.bounds.size;

    [UIView beginAnimations:@"InterfaceRotations" context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        tableview.frame = CGRectMake(0, 0 , (screenSize.height*.55), screenSize.width);
        mapView.frame = CGRectMake((screenSize.height*.55), 0, (screenSize.height*.45), screenSize.width);
    } else {
        tableview.frame = CGRectMake(0, (screenSize.height*.6) ,screenSize.width, (screenSize.height*.4));
        mapView.frame = CGRectMake(0, 0,screenSize.width, (screenSize.height*.6));
    }

    [UIView commitAnimations];
}

【讨论】:

  • 谢谢,这很好用。但是要说明的一点是,如果方向是横向,则必须在其指定的任何地方翻转“高度”和“宽度”。否则,它是完美的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多