如何通过选项卡和视图检测设备方向来回?

时间:2010-03-31 18:40:28

标签: iphone uitableview uiview orientation accelerometer

我的应用程序在纵向和横向之间来回切换。我将所有内容(标签,uiimageviews,uilabels)排成一行,以便为两个方向重新定位。但是,设备实际旋转时唯一的变化。当我在标签旋转后在标签之间循环时,它只显示自动调整,而不是我在用户旋转时设置的方式。

当我点击不同标签时,如何检测设备的方向并显示视图以反映方向?

好的,我把它设置为viewWillAppear。有没有理由不检测方向并显示我设置的内容?

-(void)viewWillAppear:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{

 UIInterfaceOrientation toOrientation = self.interfaceOrientation;
 [UIView beginAnimations:@"move buttons" context:nil];
 [UIView setAnimationDuration:0.5f];
 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
 if(toOrientation == UIInterfaceOrientationPortrait
    || toOrientation == UIInterfaceOrientationPortraitUpsideDown)
 {
  aboutContent.frame = CGRectMake(20, 100, 280, 107);
  myLogo.frame = CGRectMake(-5, 20, 330, 65);
  bulletOne.frame = CGRectMake(40, 220, 240, 45);
  bulletTwo.frame = CGRectMake(40, 270, 240, 45);
 }
 else
 {
  aboutContent.frame = CGRectMake(40, 80, 400, 70);
  myLogo.frame = CGRectMake(230, 30, 20, 20);
  bulletOne.frame = CGRectMake(90, 140, 330, 65);
  bulletTwo.frame = CGRectMake(90, 170, 330, 65);
 } 
 [UIView commitAnimations];
}

1 个答案:

答案 0 :(得分:2)

在每个UIViewController的viewWillAppear中检查设备的方向,如果方向与布局方式不同,则将willRotateToInterfaceOrientation:duration:发送给自己。假设你的willRotateToInterfaceOrientation:duration:将布局更新为指定的方向。