Получить доступ к self.view.bounds в willRotateToInterfaceOrientation
В моем приложении я хочу сохранить изображение по центру после поворота устройства. Для этого я использую методы 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 ответ
CGRect
не является объектом, поэтому вы не можете регистрировать такие значения. Попробуйте войти в объект NSValue:
NSLog(@"\t self.view.bounds=%@", [NSValue valueWithCGRect:self.view.bounds]);