【发布时间】:2013-09-25 00:57:57
【问题描述】:
我的应用设计要求 UIView 能够感知旋转。
当我的应用启动时,它会收到一个UIApplicationWillChangeStatusBarOrientationNotification。
然而,在第一次旋转时(从任一方向到任一方向)都不会收到通知。
每个后续轮换事件都会生成预期的UIApplicationWillChangeStatusBarOrientationNotification 通知。
这同样适用于UIDeviceOrientationDidChangeNotification 通知,当更改以下代码以进行设备方向检查时 - 结果是相同的。
代码: (myUIView 初始化)
// set up to listen for rotation
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
(myUIView 旋转)
-(void)didRotate
{
if ([self landscape]) // rotated TO landscape
{
// do landscape stuff
}
}
(myUIView 中的旋转检查)
// answer the question: are we landscape? Yes or no. No indicates portrait.
-(BOOL)landscape
{
UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
// Just get a displayable string version
NSString* iOrientationString ;
switch (iOrientation) {
case UIInterfaceOrientationPortrait : iOrientationString = @"UIInterfaceOrientationPortrait" ; break ;
case UIInterfaceOrientationPortraitUpsideDown : iOrientationString = @"UIInterfaceOrientationPortraitUpsideDown" ; break ;
case UIInterfaceOrientationLandscapeLeft:iOrientationString = @"UIInterfaceOrientationLandscapeLeft" ; break ;
case UIInterfaceOrientationLandscapeRight:iOrientationString = @"UIInterfaceOrientationLandscapeRight " ; break ;
}
NSLog(@"%@=%@" , @S(iOrientation ) , iOrientationString ) ;
return UIInterfaceOrientationIsLandscape(iOrientation);
}
我注意到在UIViewController 中,-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 被正确调用,第一次旋转等等。
但我希望有旋转感知视图。对于缺少的第一个事件是否有解释和/或解决方法?
【问题讨论】:
-
put [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];进入 applicationdidFinishLaunchingWithOptions 或将其放入 awakefromnib。
-
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];在 applicationdidFinishLaunchingWithOptions 或 awakefromnib 中是它所在位置的任一侧(我发布的代码的第一行。这会产生如此巨大的差异吗?回想一下它(有点)工作 - 只是错过了第一个事件。
标签: objective-c ipad ios6 uiinterfaceorientation nsnotifications