2013-05-15 19 views

回答

1

我不完全確定在這種情況下「水平」和「垂直」是什麼意思,但我想到了兩個計算,即關於「z」軸的旋轉(即我們與照片中的地平線有多高),以及向前和向後傾斜多少(即關於「x」軸的旋轉,即它是向上還是向下)。你可以使用Core Motion來做到這一點。就在add it to your project然後你可以這樣做:

  1. 確保導入CoreMotion頭:

    #import <CoreMotion/CoreMotion.h> 
    
  2. 定義一些類屬性:

    @property (nonatomic, strong) CMMotionManager *motionManager; 
    @property (nonatomic, strong) NSOperationQueue *deviceQueue; 
    
  3. 開始運動經理:

    - (void)startMotionManager 
    { 
        self.deviceQueue = [[NSOperationQueue alloc] init]; 
        self.motionManager = [[CMMotionManager alloc] init]; 
        self.motionManager.deviceMotionUpdateInterval = 5.0/60.0; 
    
        [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical 
                      toQueue:self.deviceQueue 
                     withHandler:^(CMDeviceMotion *motion, NSError *error) 
        { 
         [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
          CGFloat x = motion.gravity.x; 
          CGFloat y = motion.gravity.y; 
          CGFloat z = motion.gravity.z; 
    
          // how much is it rotated around the z axis 
    
          CGFloat rotationAngle = atan2(y, x) + M_PI_2;     // in radians 
          CGFloat rotationAngleDegrees = rotationAngle * 180.0f/M_PI; // in degrees 
    
          // how far it it tilted forward and backward 
    
          CGFloat r = sqrtf(x*x + y*y + z*z); 
          CGFloat tiltAngle = (r == 0.0 ? 0.0 : acosf(z/r);    // in radians 
          CGFloat tiltAngleDegrees = tiltAngle * 180.0f/M_PI - 90.0f); // in degrees 
         }]; 
        }]; 
    } 
    
  4. 完成後,停止運動經理:

    - (void)stopMotionManager 
    { 
        [self.motionManager stopDeviceMotionUpdates]; 
        self.motionManager = nil; 
        self.deviceQueue = nil; 
    } 
    

我沒有做這裏的值什麼,但你可以將它們保存在類的屬性,您就可以訪問你的應用程序的其他地方。或者你可以從這裏將UI更新發回主隊列。一堆選擇。

由於這是iOS 5及更高版本,如果應用程序支持早期版本,您可能還想弱連接Core Motion,然後檢查一切是否正常,如果沒有,只是意識到您不打算被捕捉設備的方向:

if ([CMMotionManager class]) 
{ 
    // ok, core motion exists 
} 

而且,如果你想知道關於我的每秒十二倍相當任意的選擇,在Event Handling Guide for iOS,他們建議10-20 /秒,如果只是檢查方向的設備。

+0

我的意思是視野角度的相機。 – Benson

+0

@ BingchenYang哈哈。對不起,我誤解了這個問題。用於查詢/控制相機的API在iOS中有點基本。希望其他人可以在這裏幫助你,但你很可能無法做到這一點(缺乏有效焦距表和查找設備以及是否使用前置或後置攝像頭)。 – Rob

+1

感謝您的努力。這似乎很多工作。我在網站上發現了一些相關材料http://www.caramba-apps.com/blog/files/field-of-view-angles-ipad-iphone.html但是如果有一種方法,它仍然會更好通過API – Benson

相關問題