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];
    }  
}




阅读更多
想对作者说点什么? 我来说一句

android横竖屏切换

2016年12月30日 82KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭