随便写写-视图的自动横屏和手动点击横屏

万事开头难,总不知道从哪里开始写起,可能这样看起来杂乱无章,记录一下自己的感悟和每天学到的东西吧,方便自己以后的查找


在做漫画的程序时看漫画一定会涉及到横屏观看,无论使用原生的还是一些第三方,总是有些地方不够全面,我这里用的是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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值