如果我有一个指向 UIViewController 的指针,是否可以在它更改 interfaceOrientation 时通知我而不修改控制器的代码?
我最好的选择是检测设备方向的变化,然后查看 UIViewController 是否会/已经旋转(d)?
如果我有一个指向 UIViewController 的指针,是否可以在它更改 interfaceOrientation 时通知我而不修改控制器的代码?
我最好的选择是检测设备方向的变化,然后查看 UIViewController 是否会/已经旋转(d)?
您可以使用 NSNotificationCenter :
[[NSNotificationCenter defaultCenter] addObserver:self // put here the view controller which has to be notified
selector:@selector(orientationChanged:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
- (void)orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//do stuff
NSLog(@"Orientation changed");
}
您可以willAnimateRotationToInterfaceOrientation:duration:
在 UIViewController 上使用该方法,然后将任何 UIView(或任何其他代码)重新定位为横向或纵向。例如
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
// change positions etc of any UIViews for Landscape
} else {
// change position etc for Portait
}
// forward the rotation to any child view controllers if required
[self.rootViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}