EIDT:已修复。看起来这是一个模拟器问题。 感谢Gabriel
我得到的模拟器只有 这个:
--- 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>] {
哦,好吧......
我真的在这个上拉我的头发。我在S.O.上看到了很多相关的问题。但是找不到一个对我有用的东西。
我使用的是最简单的例子,Xcode 4.6.2,New Project,Empty Application。
结果是,如果应用程序在纵向中启动,则它可以旋转到四个支持的方向中的任何一个。
但是,如果它是在横向中启动,它不执行初始旋转,即使在启动后旋转设备确实尊重四种可能的轮换。
请注意,我已经实现了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&amp;的每个变体。 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;
}
注意最后一行?
正如您所看到的那样确实发送了轮换通知......但是没有效果? 是什么给了什么?
困惑!
答案 0 :(得分:1)
尝试使用真实设备而不是模拟器。