iphone+横竖屏切换

需求:程序默认开启是横屏,在压到某个页面的时候是竖屏

1.默认横屏需要在XX-info.plist 的Supported interface orientations 的第一项为Landscape (left home button)

2.在切换的竖屏的时候

-(void)viewWillAppear:(BOOL)animated
{
    //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
    if (orientation != UIInterfaceOrientationPortrait || 
        orientation != UIInterfaceOrientationPortraitUpsideDown)
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        
        //(获取当前电池条动画改变的时间
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        
        //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
        self.view.transform = [self getTransformMakeRotationByOrientation:orientation];
        [UIView commitAnimations];
    }  
    
    
}

计算方向

- (CGAffineTransform)getTransformMakeRotationByOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeLeft) 
    {
        return CGAffineTransformMakeRotation(M_PI/2);
    } 
    else if (orientation == UIInterfaceOrientationLandscapeRight) 
    {
        return CGAffineTransformMakeRotation(M_PI/2);
    } 
    else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        return CGAffineTransformMakeRotation(-M_PI);
    } 
    else 
    {
        
    }
    
    return CGAffineTransformIdentity;
    
}



3.返回的到竖屏

- (void)viewWillDisappear:(BOOL)animated
{

    
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
    if (orientation != UIInterfaceOrientationLandscapeLeft || 
        orientation != UIInterfaceOrientationLandscapeRight)
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
        
        //(获取当前电池条动画改变的时间
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        
        self.view.transform = CGAffineTransformIdentity;
        [UIView commitAnimations];
    }  
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值