2011-12-02 66 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:你找到了解决方案吗?如果是这样,你能帮我吗? –