Child View Controller to Rotate While Parent View Controller Does Not

前端 未结 3 1718
既然无缘
既然无缘 2020-12-12 21:21

What I am Trying to Do:

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

Child View that i

3条回答
  •  不知归路
    2020-12-12 21:47

    From iOS developer library Technical Q&A

    Autorotation is implemented by applying a rotation transform to the application's window when the system determines that the interface must rotate. The window then adjusts its bounds for the new orientation and propagates this change down through the view controller hierarchy via calls to each view controller's and presentation controller's -viewWillTransitionToSize:withTransitionCoordinator: method. Invocations of this method are provided a transition coordinator object containing the delta of the window's current transform and new transform, which can be retrieved by sending a -targetTransform message to the transition coordinator. Your view controller or presentation controller can derive the appropriate inverse transform and apply it to the target view. This nullifies the effect of the window transform on that particular view and gives the appearance that the view has remained fixed in its current orientation as the rest of the interface rotates normally.

    i.e. if you have a view named noRotatingView

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    
        noRotatingView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))
    }
    
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    
        coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in
    
            let deltaTransform = coordinator.targetTransform()
            let deltaAngle = atan2f(Float(deltaTransform.b), Float(deltaTransform.a))
    
            var currentRotation = self.noRotatingView.layer.valueForKeyPath("transform.rotation.z")?.floatValue
    
            // 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 = currentRotation! + (-1 * Float(deltaAngle)) + 0.0001
            self.noRotatingView.layer.setValue(currentRotation, forKeyPath:"transform.rotation.z")
    
            }) { (UIViewControllerTransitionCoordinatorContext) -> Void in
    
                var currentTransform = self.noRotatingView.transform;
                currentTransform.a = round(currentTransform.a);
                currentTransform.b = round(currentTransform.b);
                currentTransform.c = round(currentTransform.c);
                currentTransform.d = round(currentTransform.d);
                self.noRotatingView.transform = currentTransform;
        }
    }
    

    I've tried to make a demo project at https://github.com/rishi420/TestCameraRotation

提交回复
热议问题