ios 屏幕旋转,横屏竖屏

最近做了一点关于屏幕旋转的东西,第一次做,搞的焦头烂额,所幸在同事的帮助下完成了,现在记录一下,便于以后查看。

1.首先需要在General里,Deployment info 里根据自己的需求设置Device Orientation。

Portrait代表竖屏Landscape代表横屏

2.如果是想要自动根据手机的方向横竖屏的话设置下面两个方法

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

3如果想手动控制横竖屏,就用下面的方法

添加按钮的方法

[UIDevice currentDevice] orientation]可以取到当前屏幕的方向

if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait)
 {

        [self setfullScreen];

 }

if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)
 {

        [self setsmallScreen];

 }

//横屏

- (void)setfullScreen
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}

//竖屏

- (void)setsmallScreen
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}

然后旋转后的frame需要根据自己需要设置

还有一种横屏方法为强制横屏,在view上添加一个window,然后把window强制旋转,但是这样做键盘还是竖屏,可以根据需要选择

下面直接上方法

- (void)setLandScapeViewWithOrientation:(UIDeviceOrientation)orientnation
{
    if (UIDeviceOrientationIsLandscape(orientnation)) {
        if (_landScapeWindow == nil) {
            CGRect screenFrame = [UIScreen mainScreen].bounds;
            _landScapeWindow = [[UIWindow alloc] initWithFrame:CGRectMake(screenFrame.size.width/2 - screenFrame.size.height/2, screenFrame.size.height/2 - screenFrame.size.width/2, screenFrame.size.height, screenFrame.size.width)];
            _landScapeWindow.windowLevel = UIWindowLevelStatusBar + 1;
            _landScapeWindow.center = CGPointMake(screenFrame.size.width/2, screenFrame.size.height/2);
        CGFloat angle = M_PI_2;
        if (orientnation == UIDeviceOrientationLandscapeRight) {
            angle = M_PI_2 + M_PI;
        }
        _landScapeWindow.transform = CGAffineTransformIdentity;
        _landScapeWindow.transform = CGAffineTransformMakeRotation(angle);
        _landScapeWindow.hidden = NO;
        
    } else if (UIDeviceOrientationIsPortrait(orientnation)) {
        _landScapeWindow.hidden = YES;
    }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS上,<video>标签默认是不支持自动全播放的。但是,你可以通过以下方式实现自动全播放,并且在横屏时自动切换横屏模式: 1. 在<video>标签中添加webkit-playsinline属性,这将允许视频在页面内播放而不是全播放。 ``` <video src="video.mp4" webkit-playsinline></video> ``` 2. 为了实现自动全播放,你需要使用JavaScript来控制<video>标签。在页面加载完成后,使用以下代码将视频播放: ``` var video = document.querySelector('video'); video.play(); ``` 3. 为了在横屏时自动切换横屏模式,你需要在JavaScript中监听屏幕旋转事件(orientationchange),并在事件发生时修改<video>标签的样式,将其宽度设置为屏幕的高度,将其高度设置为屏幕的宽度,同时还需要将页面的viewport元标签设置为允许横屏显示: ``` //监听屏幕旋转事件 window.addEventListener("orientationchange", function() { if (window.orientation == 90 || window.orientation == -90) { //横屏时将视频全,并修改viewport元标签 document.querySelector('video').style.width = window.innerHeight + 'px'; document.querySelector('video').style.height = window.innerWidth + 'px'; document.querySelector('meta[name=viewport]').setAttribute('content', 'width=device-height, initial-scale=1.0'); } else { //竖屏时将视频恢复原始尺寸,并修改viewport元标签 document.querySelector('video').style.width = '100%'; document.querySelector('video').style.height = 'auto'; document.querySelector('meta[name=viewport]').setAttribute('content', 'width=device-width, initial-scale=1.0'); } }); ``` 这样就可以实现iOS上<video>标签的自动全播放,并且在横屏时自动切换横屏模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值