不开启屏幕旋转, 监听iOS屏幕旋转状态

// 按钮适应屏幕的旋转
-(void)rotateBtn:(float)n {	
	UIButton *testBtn= [self.view viewWithTagName:@"TestBtn"];
	testBtn.transform = CGAffineTransformMakeRotation(n * M_PI / 180.0);
}

// 屏幕旋转状态变化
- (void)orientationChanged:(NSNotification *)note  {
	UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
	switch (o) {
		case UIDeviceOrientationPortrait: // Device oriented vertically, home button on the bottom
			[self rotateBtn:0.0];
			break;
		case UIDeviceOrientationPortraitUpsideDown: // Device oriented vertically, home button on the top
			[self rotateBtn:180.0];
			break;
		case UIDeviceOrientationLandscapeLeft: // Device oriented horizontally, home button on the right
			[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
			[self rotateBtn:270.0];
			break;
		case UIDeviceOrientationLandscapeRight: // Device oriented horizontally, home button on the left
			[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
			[self rotateBtn:90.0];
			break;
		default:
			break;
	} 
 }
 

-(void)viewWillDisappear:(BOOL)animated {    
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	UIDevice *device = [UIDevice currentDevice];
	[nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device]; // 移除屏幕旋转状态通知
}
 
 - (void)viewDidAppear:(BOOL)animated {       
 	//----- 注册屏幕旋转状态变化通知 -----
	UIDevice *device = [UIDevice currentDevice]; 
	[device beginGeneratingDeviceOrientationNotifications]; 
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:device];
}

你可能感兴趣的:(iOS,ios,objective-c,swift)