手机朝向监测

//添加监测

- (void)addNotification {
    [//返回一个UIDevice实例;
     [UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];//改变朝向则获悉这一点;
    
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)   name:@"UIDeviceOrientationDidChangeNotification"
     object:nil];
}


//实现方法orientationChanged;
-(void)orientationChanged:(NSNotification*)notification
{UIDeviceOrientation orientation=[[UIDevice currentDevice]orientation];


    switch (orientation) {


        case UIDeviceOrientationFaceUp:
            self.orientationLabel.text=@"Face Up!";
            break;


        case UIDeviceOrientationFaceDown:
            self.orientationLabel.text=@"Face Down!";
            break;

        case UIDeviceOrientationPortrait:
            self.orientationLabel.text=@"Standing Up!";
            break;


        case UIDeviceOrientationLandscapeLeft:
            self.orientationLabel.text=@"Left Side!";
            break;


       case UIDeviceOrientationLandscapeRight:
            self.orientationLabel.text=@"Right Side!";
            break;
      
        default:
            self.orientationLabel.text=@"Unknown!";
            break;
    }

}


posted @ 2016-05-19 17:14  有棱角的圆  阅读(201)  评论(0编辑  收藏  举报