万事开头难,总不知道从哪里开始写起,可能这样看起来杂乱无章,记录一下自己的感悟和每天学到的东西吧,方便自己以后的查找
在做漫画的程序时看漫画一定会涉及到横屏观看,无论使用原生的还是一些第三方,总是有些地方不够全面,我这里用的是MWPhoto,这里面提供的是自动横屏,在手机设置中关闭竖屏锁定,根据重力感应可以实现观看,核心代码如下:这里我是用present进来的
自动方法:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}
前两个方法为了设置屏幕转向,后面三个方法就是屏幕将要旋转、旋转了一半、旋转结束时,根据你自己的需要把一些视图刷新的代码写在里面,注意千万不要这三个方法里面刷新同一个视图,就是不要在里面写同一个方法,这样你旋转的过程会卡死。
手动的方法:
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait)
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];//这句话是防止手动先把设备置为横屏,导致下面的语句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
} else {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];//这句话是防止手动先把设备置为竖屏,导致下面的语句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}
写一个按钮,把这个方法添加进去,就可以实现手动点击横屏了
有时可能会需要其他的界面保持竖屏,只有某个特定的界面需要横屏时,需要加一个TabBarController这个控制器,并设置成根目录,在里面写上这个方法,就可以保证屏幕不会随着重力感应旋转了
- (BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
然后,在你需要横屏的界面里,重新定义这个方法,这样就能实现在只在你想要的界面出现横屏了
- (BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}