子视图控制器旋转而父视图控制器不旋转 [英] Child View Controller to Rotate While Parent View Controller Does Not

查看:36
本文介绍了子视图控制器旋转而父视图控制器不旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:

由父视图控制器管理的父视图不应旋转.

Parent View that is managed by Parent View Controller SHOULD NOT ROTATE.

由子视图控制器管理的子视图应该旋转到所有方向.

Child View that is managed by Child View Controller SHOULD ROTATE to all orientations.

我的尝试:

父视图控制器

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Portrait
}

override func shouldAutorotate() -> Bool {
    return true
}

fun addChildViewController() {
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    self.childViewController = storyBoard("Child View Controller") as? ChildViewController
    self .addChildViewController(self.childViewController!)
    self.childViewController! .didMoveToParentViewController(self)
    self.view .addSubview(self.childViewController!.view)
    self.childViewController!.view.frame = CGRectMake (40, 200, 400, 250)
}

子视图控制器

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return  .All
}

override func shouldAutorotate() -> Bool {
    return true
}

Xcode 部署信息中支持的方向设置为全部四个.

Supported orientations in Xcode Deployment Info are set to all four.

我得到了什么:

没有视图旋转.如果我将父级的旋转设置为全部,则所有视图将一起旋转.所以要么全有,要么全无.

None of the View's rotate. If I set the parent's rotation to all, all views rotate together. So it's all or nothing.

更新

当我尝试为 UIDeviceOrientationDidChangeNotification 设置观察者并使用 UIDevice.currentDevice().orientation 使用 CGAaffaineTransformMakeRotate 旋转 childView 时,我得到了想要的结果.然而,即,如果我旋转到横向,并尝试下拉通知中心(或者如果我收到系统通知),它仍然处于纵向(因为应用程序本身仍然处于纵向),旋转 childView 旋转回纵向以显示状态栏/通知/通知中心.

When I try putting an observer for UIDeviceOrientationDidChangeNotification and use UIDevice.currentDevice().orientation to rotate childView using CGAffaineTransformMakeRotate , I get desired results. HOWEVER, i.e., if I rotate to landscape, and try to pull down the notification center (or if I get a system notification), which is still in the portrait orientation (because the app is natively still left in Portrait), rotated childView rotates back to portrait to honor the status bar/notification/notification center.

Stock iOS Camera App 是我能展示的最好的例子.主画布不会旋转到不同的方向,但幕后的状态栏会随着设备旋转而旋转.此外,子视图在它们内部旋转以尊重不同的方向.我正在努力实现这种行为......

Stock iOS Camera App is the best example I can present. The main canvas does not rotate in to different orientations, yet the status bar, behind the scenes, rotates honoring device rotation. Also the subviews rotate within them selves to honor different orientations. I am trying to achieve this behavior....

推荐答案

经过多次与 Apple 的来回,他们自己指向了同一个链接 技术问答 QA1890,我不得不用流畅的方式做到这一点:

After many back and forth's with Apple them selves, and them pointing to the same link Technical Q&A QA1890, I had to do this with the flowing way:

MotherViewController

MotherViewController

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        coordinator.animateAlongsideTransition({(context: UIViewControllerTransitionCoordinatorContext) -> Void in
            let deltaTransform: CGAffineTransform = coordinator.targetTransform()
            let deltaAngle: CGFloat = atan2(deltaTransform.b, deltaTransform.a)
            var currentRotation: CGFloat = self.mainView.layer.valueForKeyPath("transform.rotation.z") as! CGFloat

            // Adding a small value to the rotation angle forces the animation to occur in a the desired direction, preventing an issue where the view would appear to rotate 2PI radians during a rotation from LandscapeRight -> LandscapeLeft.
            currentRotation += -1 * deltaAngle + 0.0001
            self.mainView.layer.setValue(currentRotation, forKeyPath: "transform.rotation.z")

            }, completion: {(
                context: UIViewControllerTransitionCoordinatorContext) -> Void in
                // Integralize the transform to undo the extra 0.0001 added to the rotation angle.
                var currentTransform: CGAffineTransform = self.mainView.transform
                currentTransform.a = round(currentTransform.a)
                currentTransform.b = round(currentTransform.b)
                currentTransform.c = round(currentTransform.c)
                currentTransform.d = round(currentTransform.d)
                self.mainView.transform = currentTransform
        })
    }

主视图控制器

NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged:", name:UIDeviceOrientationDidChangeNotification, object: nil)

func orientationChanged ( notification: NSNotification){
        switch UIDevice.currentDevice().orientation {
        case .Portrait:
            self.rotateChildViewsForOrientation(0)
        case .PortraitUpsideDown:
            self.rotateChildViewsForOrientation(0)
        case .LandscapeLeft:
            self.rotateChildViewsForOrientation(90)
        case .LandscapeRight:
            self.rotateChildViewsForOrientation(-90)
        default:
            print ("Default")
                }
    }

<小时>

这似乎在保持主视图静态的同时旋转子视图.此外,当通知进来时,应用程序似乎知道正确的方向(因为母视图实际上在幕后旋转).


This seem to rotate the child view's while keeping the main view static. Also the app seem to know the proper orientation when notifications come in (because mother view actually rotates behind the scenes).

特别感谢 jameskWarif Akhand Rishi 的所有帮助!

Special thanks to jamesk and Warif Akhand Rishi for all the help!

这篇关于子视图控制器旋转而父视图控制器不旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆