0

在我的应用程序中,我有一个按钮和两个视图,当我点击按钮时,我从超级视图中删除了第一个视图,并将超级视图上的第二个视图添加为子视图。但问题是,当我将设备方向从纵向更改为横向,然后点击按钮时,它将以纵向视图而不是横向模式显示视图,反之亦然。这是我正在使用的代码。

    - (void)showFiles {
[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:1.25];

[UIView setAnimationTransition:([songTableView superview] ?
                                UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
                       forView:toggleButton cache:YES];

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
    [songTableView setFrame:CGRectMake(0.0f, 44.0f, 480.0f, 256.0f)];
}
else {
    [songTableView setFrame:CGRectMake(0.0f, 44.0f, 320.0f, 416.0f)];
}

if ([songTableView superview]) {
    [toggleButton setImage:[UIImage imageNamed:@"AudioPlayerAlbumInfo.png"] forState:UIControlStateNormal];
}
else {
    [toggleButton setImage:albumArtImageView.image forState:UIControlStateNormal];
}

[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];

[UIView setAnimationTransition:([songTableView superview] ?
                                UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
                       forView:self.view cache:YES];
if ([songTableView superview])
{
    albumArtImageView.image = albumArtImage;
    albumArtImageView.contentMode = UIViewContentModeScaleAspectFit; 
    albumArtImageView.clipsToBounds = YES;

    [songTableView removeFromSuperview];
    [gradientLayer removeFromSuperlayer];

    [self.view addSubview:uppercontrollsView];
    [self.view addSubview:lowercontrollsView];
}
else
{
    [albumArtImageView setImage:[UIImage imageNamed:@"AudioPlayerTableBackground.png"]];
    albumArtImageView.contentMode = UIViewContentModeScaleAspectFill; 
    albumArtImageView.clipsToBounds = YES;

    [uppercontrollsView removeFromSuperview];
    [lowercontrollsView removeFromSuperview];
    [self.view addSubview:songTableView];

    [songTableView reloadData];
}

[UIView commitAnimations];

}

4

2 回答 2

0

如果你想处理 iOS 6,你可以做的是:

如果您在视图 A 上,则存储其 InterfaceOrientation,然后

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return lastInterfaceOrientation;
}
于 2012-12-21T11:16:36.897 回答
0

您可以使用 willRotateToInterfaceOrientation 方法。当您更改设备方向时,它会调用..

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

 {

 return YES;

 }

  -(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration

   {

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 

  {

  [btn setFrame:CGRectMake(20, 52, 728, 617)];

  }
 else

{

[btn setFrame:CGRectMake(20, 52, 728, 617)];

}

    }
于 2012-12-21T10:17:14.240 回答