EIDT:固定。看起来是模拟器问题。感谢加布里埃尔
一个模拟器我只得到这个:
--- UIDeviceOrientationDidChangeNotification -> [<UIDevice: 0x7577d30>] {
但在设备上,我确实收到了大量新通知......
--- UIDeviceOrientationDidChangeNotification -> [<UIDevice: 0x1dd5a0c0>] {
--- UIApplicationDidEndResumeAnimationNotification -> [<UIApplication: 0x1dd63500>] (null)
--- UIDeviceOrientationDidChangeNotification -> [<UIDevice: 0x1dd5a0c0>] {
--- _UIApplicationDidBeginIgnoringInteractionEventsNotification -> [<UIApplication: 0x1dd63500>] (null)
--- UIWindowWillRotateNotification -> [<UIWindow: 0x1e866b40; frame = (0 0; 320 568); layer = <UIWindowLayer: 0x1e866ad0>>] {
--- UIApplicationWillChangeStatusBarOrientationNotification -> [<UIApplication: 0x1dd63500>] {
--- UIApplicationWillChangeStatusBarFrameNotification -> [<UIApplication: 0x1dd63500>] {
--- UIViewAnimationDidCommitNotification -> [UIViewAnimationState] {
--- UIApplicationDidChangeStatusBarOrientationNotification -> [<UIApplication: 0x1dd63500>] {
--- UIApplicationDidChangeStatusBarFrameNotification -> [<UIApplication: 0x1dd63500>] {
--- UIWindowWillAnimateRotationNotification -> [<UIWindow: 0x1e866b40; frame = (0 0; 320 568); layer = <UIWindowLayer: 0x1e866ad0>>] {
--- UIViewAnimationDidCommitNotification -> [UIViewAnimationState] {
--- UIViewAnimationDidStopNotification -> [<UIViewAnimationState: 0x1e8692f0>] {
--- _UIApplicationDidEndIgnoringInteractionEventsNotification -> [<UIApplication: 0x1dd63500>] (null)
--- UIWindowDidRotateNotification -> [<UIWindow: 0x1e866b40; frame = (0 0; 320 568); layer = <UIWindowLayer: 0x1e866ad0>>] {
--- UIViewAnimationDidStopNotification -> [<UIViewAnimationState: 0x1e867ea0>] {
那好吧 ...
我真的在这个上拉我的头发。我在 SO 上看到了许多相关问题,但找不到对我有用的问题。
我正在使用最简单的示例,Xcode 4.6.2,新项目,空应用程序。
结果是,如果应用程序以纵向启动,则它可以旋转到四个支持的方向中的任何一个。
但是,如果它在Landscape中启动,它不会执行初始旋转,即使在启动后旋转设备确实遵循四种可能的旋转。
请注意,我已经实现了 iOS 5 和 iOS 6 所需的一切,并且该应用程序在所有 iOS 版本中都可以工作,除了初始方向在所有地方都失败。
另外,请注意,我只是在设备支持所有方向并且应该“开箱即用”工作的“普通香草”情况之后(除了它不支持!)
#import "AppDelegate.h"
@interface TestUIVIew: UIView
@end
@implementation TestUIVIew
- (void) drawRect: (CGRect) rect {
CGContextRef context = ::UIGraphicsGetCurrentContext() ;
CGRect bounds = self.bounds ;
CGMutablePathRef p = ::CGPathCreateMutable() ;
CGFloat minx = ::CGRectGetMinX(bounds) ;
CGFloat miny = ::CGRectGetMinY(bounds) ;
CGFloat maxx = ::CGRectGetMaxX(bounds) ;
CGFloat maxy = ::CGRectGetMaxY(bounds) ;
CGPoint TL = (CGPoint) {minx, miny} ;
CGPoint BL = (CGPoint) {minx, maxy} ;
CGPoint TR = (CGPoint) {maxx, miny} ;
CGPoint BR = (CGPoint) {maxx, maxy} ;
::CGPathMoveToPoint (p, 0, TL.x, TL.y) ;
::CGPathAddLineToPoint (p, 0, BR.x, BR.y) ;
::CGPathMoveToPoint (p, 0, TR.x, TR.y) ;
::CGPathAddLineToPoint (p, 0, BL.x, BL.y) ;
::CGContextSetLineWidth(context, 5.0f) ;
::CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor) ;
::CGContextAddPath(context, p) ;
::CGContextStrokePath(context) ;
::CGPathRelease(p) ;
}
@end
@interface SimplestViewController : UIViewController
@end
@implementation SimplestViewController
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
return YES ;
}
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll ;
}
- (BOOL) shouldAutorotate {
return YES ;
}
@end
@implementation AppDelegate
- (BOOL) application: (UIApplication *) application
didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController * vc = [SimplestViewController new] ;
vc.view = [TestUIVIew new] ;
self.window.rootViewController = vc ;
self.window.backgroundColor = [UIColor greenColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
我已经尝试了模拟器 5.0、5.1、6.0 和 6.1 的每个变体,结果完全相同:似乎我只是没有收到任何轮换消息,或者如果我收到了,则不值得尊重。
所以,我在main中添加了这个:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#define S(x) #x
#define str(x) case x: return @S(x)
static NSString * devO(UIDeviceOrientation o) {
switch (o) {
str(UIDeviceOrientationUnknown) ;
str(UIDeviceOrientationPortrait) ;
str(UIDeviceOrientationPortraitUpsideDown) ;
str(UIDeviceOrientationLandscapeLeft) ;
str(UIDeviceOrientationLandscapeRight) ;
str(UIDeviceOrientationFaceUp) ;
str(UIDeviceOrientationFaceDown) ;
default:
return [NSString stringWithFormat:@"??? UIDeviceOrientation<%08.8x> ???", o] ;
}
}
static NSString * intfO(UIInterfaceOrientation o) {
switch (o) {
str(UIInterfaceOrientationPortrait) ;
str(UIInterfaceOrientationPortraitUpsideDown) ;
str(UIInterfaceOrientationLandscapeLeft) ;
str(UIInterfaceOrientationLandscapeRight) ;
default:
return [NSString stringWithFormat:@"??? UIInterfaceOrientation<%08.8x> ???", o] ;
}
}
static void (^notifHandler)(NSNotification *) = ^(NSNotification *note) {
UIDeviceOrientation orientO = [UIDevice currentDevice].orientation ;
UIInterfaceOrientation orientI = [[UIApplication sharedApplication] statusBarOrientation];
::NSLog(@"! NOTE ! [device:%@--app:%@]\n--- %@ -> [%@] %@"
, devO(orientO)
, intfO(orientI)
, note.name
, note.object
, note.userInfo) ;
} ;
int main(int argc, char *argv[]) {
@autoreleasepool {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] ;
[[NSNotificationCenter defaultCenter] addObserverForName: nil
object: nil
queue: nil
usingBlock: notifHandler] ;
return UIApplicationMain(argc, argv, nil, ::NSStringFromClass([AppDelegate class]));
}
}
这是日志:
2013-04-26 17:43:06.429 AutoRotate[82160:1a03] ! NOTE ! [device:UIDeviceOrientationUnknown--app:??? UIInterfaceOrientation<00000000> ???]
--- kBKSDisplayServerDiedNotification -> [(null)] (null)
2013-04-26 17:43:06.489 AutoRotate[82160:2b03] ! NOTE ! [device:UIDeviceOrientationUnknown--app:??? UIInterfaceOrientation<00000000> ???]
--- NSWillBecomeMultiThreadedNotification -> [(null)] (null)
2013-04-26 17:43:06.523 AutoRotate[82160:c07] ! NOTE ! [device:UIDeviceOrientationUnknown--app:??? UIInterfaceOrientation<00000000> ???]
--- _UIWindowDidCreateWindowContextNotification -> [<UIStatusBarWindow: 0x9667540; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x96674a0>>] {
"_UIWindowContextIDKey" = "-1367435195";
}
[...]
2013-04-26 17:43:06.659 AutoRotate[82160:1a03] ! NOTE ! [device:UIDeviceOrientationPortrait--app:UIInterfaceOrientationPortrait]
--- kBKSHIDServerDiedNotification -> [(null)] (null)
2013-04-26 17:43:06.662 AutoRotate[82160:c07] ! NOTE ! [device:UIDeviceOrientationPortrait--app:UIInterfaceOrientationPortrait]
--- UIApplicationDidEndResumeAnimationNotification -> [<UIApplication: 0x7580b80>] (null)
2013-04-26 17:43:06.663 AutoRotate[82160:c07] ! NOTE ! [device:UIDeviceOrientationUnknown--app:UIInterfaceOrientationPortrait]
--- UIDeviceOrientationDidChangeNotification -> [<UIDevice: 0x7577d30>] {
UIDeviceOrientationRotateAnimatedUserInfoKey = 1;
}
注意到最后一行了吗?
如您所见,确实发送了轮换通知......但没有效果?是什么赋予了?
困惑!