处理iOS设备的屏幕旋转

某些情况下,不强制的给用户唯一的屏幕角度给用户。这样用户可以旋转手机得到不同的视觉体验。

最简单的就是safari,横看竖看都可以。

这时需要捕捉用户的屏幕旋转事件并处理。很简单,才两步。比把大象装冰箱都简单。

下面是代码:

 1 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 2 {
 3     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 4     if (self) {
 5         [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 6         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
 7                                                      name:UIDeviceOrientationDidChangeNotification
 8                                                    object:[UIDevice currentDevice]];
 9     }
10     return self;
11 }

在ViewController初始化或者viewDidLoad方法中加入Notification就可以。很显然,Post Notification的事苹果的SDK已经为我们

处理了。我们代码中要做的就是处理发送来的Notification。

添加好Notification的Observer后就剩添加具体的处理方法了。这里就是orientationChanged。

 1 - (void)orientationChanged:(NSNotification *)notification{
 2     UIDevice * device = notification.object;
 3     switch(device.orientation)
 4     {
 5         case UIDeviceOrientationPortrait:
 6             /* start special animation */
 7             break;
 8             
 9         case UIDeviceOrientationPortraitUpsideDown:
10             /* start special animation */
11             break;
12             
13         default:
14             break;
15     };
16 }

接下来就在switch-case语句里针对没一种屏幕可能的角度添加你的处理代码吧。

原文地址:https://www.cnblogs.com/sunshine-anycall/p/3307854.html