2011-12-02 96 views
8

在我的iPhone應用程序中,我有一個用於拍照的UIImagePickerController。我隱藏了所有默認的相機控件,並在UIImagePickerController中添加了帶有兩個按鈕的UIView作爲CameraOverLayView。我的應用程序總是隻在橫向模式下運行。相機也只啓動橫向模式。現在,CameraOverlayView僅在橫向左側不會變爲橫向到右側。當設備方向將改變時,我如何更改CameraOverlayView LandscapeMode Left和Right?請幫我解決它。這是我的應用程序中的主要問題。此代碼保持左如何更改相機Overlayview方向橫向左右iPhone?

imgpicker.cameraOverlayView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 117.81); 

的OverlayView的橫向模式和該代碼保持在風景模式中的cameraoverlayview右 GAffineTransform變換= self.view.transform;

transform = CGAffineTransformRotate(transform, (M_PI/2.0)); 

    imgpicker.cameraOverlayView.transform = transform; 

我UIInterfaceOrientation方法,像這樣使用這些代碼,

UIInterfaceOrientation orientation= [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationLandscapeLeft) 
    { 
     imgpicker.cameraOverlayView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 117.81); 
    } 
    else if (orientation == UIInterfaceOrientationLandscapeRight) 
    { 
     CGAffineTransform transform = self.view.transform; 
      transform = CGAffineTransformRotate(transform, (M_PI/2.0)); 
     imgpicker.cameraOverlayView.transform = transform; 
    } 

但是,這些都不是我的幫助。當我左右旋轉設備方向風景時,cameraoverlayview不會改變。你能解決我的問題嗎?謝謝。

+0

有人解決了這個呢? –

+0

感謝您的朋友提供的解決方案! –

+0

@JayQ:你解決了嗎? –

回答

16

感謝所有。我自己解決了Camera OverlayView方向問題。我使用了我的問題中提到的相同代碼。只是我爲方向更改添加了UIDeviceOrientation通知。請找到下面的代碼,

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 


- (void) orientationChanged:(NSNotification *)notification 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
if(orientation == UIDeviceOrientationLandscapeLeft) 
     {     
      CGAffineTransform transform = self.view.transform; 
      transform = CGAffineTransformRotate(transform, (M_PI/2.0)); 
      imgpicker.cameraOverlayView.transform = transform; 
     } 
     else if(orientation == UIDeviceOrientationLandscapeRight) 
     { 
      imgpicker.cameraOverlayView.transform = CGAffineTransformRotate(CGAffineTransformIdentity,117.81); 
     } 
} 

我剛剛在我的代碼中添加了這個方法。因此,當用戶更改設備方向時,此方法將調用並更改相機覆蓋視圖的方向。請檢查您的條件內的方法,否則應用程序將崩潰。謝謝。

+1

我正在使用這個,我的覆蓋從來沒有正確排列。它總是向左或向右。 – zakdances

+0

@yuvaraj:嗨...我正在使用iphone 4,並且可以告訴我方向的確切旋轉代碼。我可以旋轉它,但按鈕遠離左右對齊。 –

+1

@yourfriendzak:你找到了解決方案嗎?如果是這樣,你能幫我嗎? –