0
Unfortunately this is not as easy as it should be. You may not simply "not rotate" a single view while the rest of the UI is rotated.
不幸的是,这并不像应该的那样容易。在旋转UI的其余部分时,您可能不会简单地“旋转”单个视图。
Since you are using a camera I suggest you to capture the the device orientation change through the notification center while keeping the controller orientation locked to whatever is your default or current state.
由于您使用的是相机,我建议您通过通知中心捕获设备方向更改,同时保持控制器方向锁定为您的默认或当前状态。
On orientation change you should then try and rotate the view with your buttons or anything else that should rotate. This is best done if all of the controls are added to a single view which has the same size as the view controller and is centered in the view controller. This means the constraints for this view are center x
, center y
, width
, height
. You need outlets for width and height and set them manually in the code. When view will layout subviews (it is a method to override in the view controller) simply set the width
and height
to the same as is the view controller controller.view.frame.size
. Till this point your application should look exactly the same as it does now if rotations are disabled.
在方向更改时,您应该尝试使用按钮或其他任何应旋转的视图旋转视图。如果将所有控件添加到与视图控制器大小相同且在视图控制器中居中的单个视图,则最好这样做。这意味着此视图的约束是中心x,中心y,宽度,高度。您需要宽度和高度的出口,并在代码中手动设置。当视图将布局子视图(它是在视图控制器中覆盖的方法)时,只需将宽度和高度设置为与视图控制器controller.view.frame.size相同。到目前为止,如果禁用旋转,您的应用程序应该与现在完全相同。
Now for the fun part: You need to check the current orientation and apply a rotation transform on the view with the buttons. What you need is a property holding the current interface orientation and upon changing that orientation you should apply a different transform to the view. The device orientation will not give you the interface orientation as device orientation includes extra positions. So when device orientation changes you need to use a switch statements and only on the 4 orientations that make sense should you change the interface orientation.
现在有趣的部分:您需要检查当前方向并使用按钮对视图应用旋转变换。您需要的是保持当前界面方向的属性,并且在更改该方向时,您应该对视图应用不同的变换。设备方向不会为您提供界面方向,因为设备方向包含额外的位置。因此,当设备方向发生变化时,您需要使用开关语句,并且只有在有意义的4个方向上才能更改界面方向。
Once you have all that you should create a method that takes a user interface orientation as input and and will reposition the view with the buttons. (assuming you use portrait by default) You need to swap the width
and height
when the interface orientation is landscape as in widthConstraint.cOnstant= controller.view.frame.size.height
and apply a rotation transform depending on the orientation.
完成所有操作后,您应该创建一个将用户界面方向作为输入的方法,并使用按钮重新定位视图。 (假设您默认使用纵向)当界面方向为横向时,您需要交换宽度和高度,如widthConstraint.cOnstant= controller.view.frame.size.height,并根据方向应用旋转变换。
This method should be called when:
应在以下情况下调用此方法:
- The controller will become visible with the application status bar orientation
应用程序状态栏方向将显示控制器
- When view will layout with the same orientation as was previously saved from view will become visible or device orientation change
如果视图的布局方向与之前从视图中保存的方向相同,则会显示可见或设备方向更改
- When device orientation changes if the new orientation makes sense and is different then the currently set interface orientation
如果新方向有意义并且与当前设置的界面方向不同,则设备方向会发生变化
There are other ways of doing this though. For instance you may keep the rotation of the view controller and rotate the camera view with the same procedure and this is even easier since you may use viewWillTransitionToSize
and animateAlongsideTransition
but the animation will probably not be acceptable as you may see black parts when the view controller is oriented. Though I am not sure that will actually occur and even if it does it might be possible to fix it with disabling bound clipping on the controller view.
还有其他方法可以做到这一点。例如,您可以保持视图控制器的旋转并使用相同的步骤旋转摄像机视图,这更容易,因为您可以使用viewWillTransitionToSize和animateAlongsideTransition,但动画可能不会被接受,因为您可能会看到视图控制器时的黑色部分是面向的。虽然我不确定实际会发生什么,即使它确实发生了,也可以通过在控制器视图上禁用绑定剪辑来修复它。