访问willRotateToInterfaceOrientation中的self.view.bounds

时间:2012-09-07 13:30:12

标签: ios interface-orientation

在我的应用中,我希望在设备旋转后保持图像居中。为此,我使用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)

任何人都知道出了什么问题或者我错过了,因为这真的让我疯狂。 任何建议,意见或帮助非常感谢,提前感谢!

1 个答案:

答案 0 :(得分:1)

CGRect不是对象,因此您无法记录此类值。尝试记录NSValue对象:

NSLog(@"\t self.view.bounds=%@", [NSValue valueWithCGRect:self.view.bounds]);