检测当前设备和屏幕相关

NSLog(@"%@",[UIDevice currentDevice].systemName);
NSLog(@"%@",[UIDevice currentDevice].systemVersion);

 第一行打印 iphone os  //当前设备系统的名称

 第二行打印 6.1  //当前系统的版本

[UIApplication sharedApplication].keyWindow;
self.view.window;

 同为获取当前window的方法,不过有一个区别,第二行的view如果是Nil的话获取不到

3. 屏幕方向相关

 IOS 6.0 之前

//ios 6.0之前的方法 屏幕旋转
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    //2 表示
    //3 表示home键在右
    //4 表示home键在左
    
    NSLog(@"InterfaceOrientation%d",toInterfaceOrientation);
    //表示除了这个方向,其他方向都支持,如果只支持某一方向写 ==
    return (toInterfaceOrientation != UIInterfaceOrientationMaskAllButUpsideDown);
}
//当设置旋转到不支持的方向不会调用这个方法
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    //在这里设置不同方向时屏幕内显示的位置,一般改frame 
}

 IOS 6.0 之后

//ios 6.0之后的方法 屏幕旋转 分拆为两个方法
-(BOOL)shouldAutorotate{
    return YES;
}
//支持的方向
-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
//当设置旋转到不支持的方向不会调用这个方法
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    //在这里设置不同方向时屏幕内显示的位置,一般改frame 
}

 

 也可以监听系统通知来判断方向 。IOS设备的加速计可以确定设备 的当前朝向,当方向改变时,系统会发出

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];

 

-(void)deviceOrientation:(NSNotification *)notification{
    UIDevice *device = [notification object];
    NSLog(@"%D",device.orientation);
}

 

posted @ 2013-05-27 22:30  wei8  阅读(133)  评论(0)    收藏  举报