菜鸟教程小白 发表于 2022-12-12 13:15:00

ios - 由加速度计确定的前后摄像头切换


                                            <p><p>我正在尝试使用 UIAccelerometer 在 Objective-C 中的前后摄像头之间切换。本质上,如果设备正面朝上,我希望后置摄像头打开。如果设备面朝下(屏幕朝下),我希望前置摄像头处于事件状态。解决这个问题的最佳方法是什么?我正在通过 AVFoundation 访问相机。谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我有一些代码可以帮助你

<p>这是加速度计委托(delegate)的代码,您可以在其中跟踪设备的位置。</p>

<pre><code>- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;
{
   CGFloat x = -;
   CGFloat y = ;
   CGFloat angle = atan2(y, x);

   if ( angle &gt;= -2.25f &amp;&amp; angle &lt;= -0.25f )
   {
      self.interfaceOrientation = UIInterfaceOrientationPortrait;
   }
   else if ( angle &gt;= -1.75f &amp;&amp; angle &lt;= 0.75f )
   {
      self.interfaceOrientation = UIInterfaceOrientationLandscapeRight;
   }
   else if( angle &gt;= 0.75f &amp;&amp; angle &lt;= 2.25f )
   {
      self.interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown;
   }
   else if ( angle &lt;= -2.25f || angle &gt;= 2.25f )
   {
      self.interfaceOrientation = UIInterfaceOrientationLandscapeLeft;
   }
}
</code></pre>

<p>要查看更多详细信息,如何使用委托(delegate),如何用户声明事物等,请查看我的 github 上的代码:</p>

<p>实现文件:
<a href="https://github.com/lucasecf/flipped-cam/blob/master/flipped-cam/LEImagePickerController.m" rel="noreferrer noopener nofollow">https://github.com/lucasecf/flipped-cam/blob/master/flipped-cam/LEImagePickerController.m</a> </p>

<p>项目页面:
<a href="https://github.com/lucasecf/flipped-cam" rel="noreferrer noopener nofollow">https://github.com/lucasecf/flipped-cam</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 由加速度计确定的前后摄像头切换,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18008297/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18008297/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 由加速度计确定的前后摄像头切换