iOS强制横屏方法之一

//这段代码是强制产生横屏效果,通过kvo实现

//强制右横屏  可以过审核

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];

[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

}




如果需要实现某些页面竖屏,特定页面横屏,可以使用模态推出页面方法,

首先

- (BOOL) shouldAutorotate

{

return NO;

}

然后添加这段代码

- (void) viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

}

- (void) dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)deviceOrientationDidChange

{

if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {

[self orientationChange:NO];

} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {

[self orientationChange:YES];

}


- (void)orientationChange:(BOOL)landscapeRight

{

if (landscapeRight) {

[UIView animateWithDuration:0.4f animations:^{

self.view.transform = CGAffineTransformMakeRotation(M_PI_2);

self.view.bounds = CGRectMake(0, 0, Screen_Width, Screen_Height);

}];

} else {

[UIView animateWithDuration:0.4f animations:^{

self.view.transform = CGAffineTransformMakeRotation(0);

self.view.bounds = CGRectMake(0, 0, Screen_Width, Screen_Height);

}];

}

你可能感兴趣的:(iOS强制横屏方法之一)