如何在UIViewController中停止所選子視圖的自動操作

[英]How to stop autorotion of selected subview in UIViewController


I have a View Controller I have started an AVCaptureSession on the view of the View Controller,

我有一個View Controller我已經在View Controller的視圖上啟動了一個AVCaptureSession,

  AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

Now I add it like this,

現在我像這樣添加它,

[self.view.layer addSubLayer: captureVideoPreviewLayer]

I also have added two buttons on storyboard in this View Controller, On rotation of the device the self.view should not rotate as it has the camera going on but I want the two buttons which are capture and cancel button to align and rotate, How can I achieve this using storyboard.

我還在這個視圖控制器的故事板上添加了兩個按鈕,在設備旋轉時,self.view不應該旋轉,因為它有攝像頭繼續但是我想要兩個按鈕,它們是捕獲和取消按鈕來對齊和旋轉,如何我可以使用故事板來實現這一點嗎?

1 个解决方案

#1


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,但動畫可能不會被接受,因為您可能會看到視圖控制器時的黑色部分是面向的。雖然我不確定實際會發生什么,即使它確實發生了,也可以通過在控制器視圖上禁用綁定剪輯來修復它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/03/11/72a34115f8d74e1bd5974a16be02893c.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com