1

在我的应用程序中,我想在设备旋转后保持图像居中。为此,我使用 willRotateToInterfaceOrientation 和 didRotateFromInterfaceOrientation 方法来确定平移向量。我正在使用以下代码:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  NSLog(@"%s ...", __func__);
  NSLog(@"\t self.view=%@", self.view);
  NSLog(@"\t self.view.bounds=%@", self.view.bounds);
  NSLog(@"\t self.view.frame=%@", self.view.frame);
  NSLog(@"\t self.view.frame=%@", [[self view] frame]);
  // Save previous bounds to determine translation after rotation occurred.
  _previousViewBounds = self.view.bounds;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
  NSLog(@"%s ...", __func__);
  NSLog(@"\t self.view=%@", self.view);
  NSLog(@"\t self.view.bounds=%@", self.view.bounds);
  NSLog(@"\t self.view.frame=%@", self.view.frame);
  NSLog(@"\t self.view.frame=%@", [[self view] frame]);
  // Restore previous view.bounds and use new view.bounds to calculate translation.
  CGFloat tx = self.view.bounds.origin.x - _previousViewBounds.origin.x;
  CGFloat ty = self.view.bounds.origin.y - _previousViewBounds.origin.y;
  // ... do translation stuff ...
}

我真的不明白为什么我不能访问 self.view.bounds 或 self.view.frame 成员,因为我的 NSLog 显示 self.view 存在。生成以下输出:

-[MapViewController willRotateToInterfaceOrientation:duration:] ...
  self.view=<UIView: 0x685fcc0; frame = (0 0; 320 367); autoresize = RM+BM; layer = <CALayer: 0x685fcf0>>
  self.view.bounds=(null)
  self.view.frame=(null)
  self.view.frame=(null)

-[MapViewController didRotateFromInterfaceOrientation:] ...
  self.view=<UIView: 0x685fcc0; frame = (0 0; 480 219); autoresize = RM+BM; layer = <CALayer: 0x685fcf0>>
  self.view.bounds=(null)
  self.view.frame=(null)
  self.view.frame=(null)

任何人都知道出了什么问题或我失踪了,因为这真的让我发疯。非常感谢任何建议、意见或帮助,在此先感谢!

4

1 回答 1

1

CGRect不是一个对象,所以你不能记录这样的值。尝试记录 NSValue 对象:

NSLog(@"\t self.view.bounds=%@", [NSValue valueWithCGRect:self.view.bounds]);
于 2012-09-07T13:37:31.120 回答