简体   繁体   English

iPhone 3,iPhone 4和iPad方向处理

[英]IPhone 3, iPhone 4, and iPad orientation handling

I have a view with a tableView and a mapView. 我有一个tableView和mapView的视图。 In portrait mode, I want the mapView to occupy the top 60% of the screen, and the table view, the bottom 40%. 在纵向模式下,我希望mapView占据屏幕的顶部60%,而表格视图则占据屏幕的40%。 In landscape, I want the mapView to occupy 45% of the screen on the right, and the tableView 55% on the left. 在横向环境中,我希望mapView占据右侧屏幕的45%,而tableView占据左侧屏幕的55%。

Thus, I thought that something like this ought to work: 因此,我认为这样的事情应该起作用:

    -(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));
 }

    }

This code works fine on the iphone simulator with version 4 of the OS, but when I try to run it on the iphone 4 simulator or the ipad simulator, the views are completely off (i can only see the about a quarter of the map in portrait and none of the table, and in landscape i can see only the table). 这段代码在OS版本4的iphone模拟器上可以正常工作,但是当我尝试在iphone 4模拟器或ipad模拟器上运行它时,视图完全关闭(我只能看到地图的大约四分之一)。肖像,没有桌子,在风景中我只能看到桌子)。

I dont understand why this is the case -- since im simply scaling the views to match the screen size, shouldnt something like the above code work fine regardless of the resolution of the device? 我不明白为什么会这样-因为我只是简单地缩放视图以匹配屏幕尺寸,所以不管设备的分辨率如何,上面的代码都不能正常工作? I was able to make a 'fix' by scaling using different values for the iphone, iphone 4, and ipad, but the numbers that I used were discovered by trial and error, and they dont appear to make much sense. 通过使用iPhone,iPhone 4和iPad的不同值进行缩放,我可以进行“修复”,但是我使用的数字是通过反复试验发现的,它们似乎没有多大意义。

can someone please point me towards the correct method for handing these orientation changes for all devices? 有人可以为我指出所有设备处理这些方向变化的正确方法吗? Thanks so much! 非常感谢!

I have done something similar to this. 我做了类似的事情。 Here is how I would accomplish what you would like to do 这就是我将如何完成您想做的事情

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

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

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