swift 从入门到放弃首页投稿(暂停使用,暂停投稿)程序员

第三篇:ios 进入某个VC强转为横屏,出VC后复原

2016-11-17  本文已影响356人  jeckHao

今天做项目,某一个VC需要展现VR展览内容,产品要求这个VC可以横屏查看,因为横屏查看的时候,看的范围比较大,但是其余的VC都是竖屏显示的,为了达到某个VC横屏显示其余VC不变的效果,然后查询资料,撸代码。。

查询过资料之后,大概分为四种实现方式,我使用的是第四种实现方法。

第一种:重写方法:shouldAutorotate 和supportedInterfaceOrientations

override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return [.landscapeLeft,.landscapeRight]
    }

这样的话,就可以支持在A页面的时候旋转VC了。

第二种:强制转换

//swift3的时候orientation这个参数可能报错,直接用0,1,2,3代替方向即可
NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];
 [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

第三种:通过人为的办法改变view.transform的属性。

第四种:通过appDelgate和第二种方法结合的方式实现

 /// 设置横屏/竖屏显示
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if changeOrientation {
            return [.landscapeLeft,.portrait,.landscapeRight]
        }else {
            return .portrait
        }
    }
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //打开试图的横屏显示
        AppDelegate.shareInstance().changeOrientation = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //将试图还原为竖屏
        AppDelegate.shareInstance().changeOrientation = false
        UIDevice.current.setValue(NSNumber(value: 1), forKey: "orientation")
    }
override func willAnimateRotation(to toInterfaceOrientation:UIInterfaceOrientation, duration: TimeInterval) {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
            self.standScreen()
        }
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
            self.acrossScreen()
        }
    }
    
    func standScreen() {
        contentView?.frame = CGRect(x: 0, y: NavigationBarHeight, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        self.customNavBar?.width = SCREEN_WIDTH
    }
    
    func acrossScreen() { 
       contentView?.frame = CGRect(x: 0, y: NavigationBarHeight, width: SCREEN_HEIGHT, height: SCREEN_WIDTH)
        self.customNavBar?.width = SCREEN_HEIGHT
    }
上一篇下一篇

猜你喜欢

热点阅读