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);

}];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容