2016-01-05 14 views
7

4 öğe sekmesi olan bir UITabBarController sahibim. Bunlardan biri AvCaptureVideoPreviewLayer (barkod tarayıcısıdır). Yapmak istediğim, yalnızca AVCaptureVideoPreviewLayer (iOS kamera uygulaması gibi) için otorotatlamayı devre dışı bırakmak, ancak ekranla birlikte dönecek item bar için değil. Bu biraz zor bir durum çünkü UITabBarConrtoller'un rotasyonu kolayca devre dışı bırakmanıza izin vermediğini düşünüyorum. benim kamera görünümü içinYalnızca AVCaptureVideoPreviewLayer için dönüşü devre dışı bırakma

kodu : Ben doğru anlamak eğer

import UIKit 
import AVFoundation 

class ScannerViewController: UIViewController, 

// MARK: Properties 

/// Manages the data flow from the capture stage through our input devices. 
var captureSession: AVCaptureSession! 

/// Dispays the data as captured from our input device. 
var previewLayer: AVCaptureVideoPreviewLayer! 


// MARK: Methods 

override func viewDidLoad() { 

    super.viewDidLoad() 

    view.backgroundColor = UIColor.blackColor() 
    self.captureSession = AVCaptureSession() 

    // This object represents a physical capture device and the properties associated with that device 
    let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
    // Is useful for capturing the data from the input device 
    let videoInput: AVCaptureDeviceInput 

    do { 
     videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice) 
    } catch { 
     // TODO: maybe show an alert 
     return 
    } 

    if (self.captureSession.canAddInput(videoInput)) { 
     self.captureSession.addInput(videoInput) 
    } else { 
     failed(); 
     return; 
    } 

    let metadataOutput = AVCaptureMetadataOutput() 

    if (self.captureSession.canAddOutput(metadataOutput)) { 
     self.captureSession.addOutput(metadataOutput) 

     metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 
     metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code, 
      AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, 
      AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode93Code, 
      AVMetadataObjectTypeCode128Code] 
    } else { 
     failed() 
     return 
    } 

    // Adds the preview layer to display the captured data. Sets the videoGravity to AspectFill so that it covers the full screen 
    self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession); 
    self.previewLayer.frame = self.view.layer.bounds; 
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    self.view.layer.addSublayer(previewLayer); 
    self.captureSession.startRunning(); 

} 

override func viewDidLayoutSubviews() { 

    self.previewLayer?.frame = self.view.layer.bounds; 

} 

override func didReceiveMemoryWarning() { 

    super.didReceiveMemoryWarning() 

} 

override func viewWillAppear(animated: Bool) { 

    super.viewWillAppear(animated) 

    if (self.captureSession.running == false) { 
     self.captureSession.startRunning(); 
    } 

} 

override func viewWillDisappear(animated: Bool) { 

    super.viewWillDisappear(animated) 

    if (self.captureSession.running == true) { 
     self.captureSession.stopRunning(); 
    } 

} 

func failed() { 

    let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .Alert) 
    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
    presentViewController(ac, animated: true, completion: nil) 
    self.captureSession = nil 

} 

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { 

    self.captureSession.stopRunning() 

    if let metadataObject = metadataObjects.first { 
     let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject; 

     AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) 
     foundCode(readableObject.stringValue); 
    } 

} 

/// Completes some tasks when a barcode is found. 
/// 
/// - parameter code: The the barcode found. 
func foundCode(code: String) { 

} 

} 

cevap

2

değilim% 100 emin. Ama ne yapabilirdi geçerli:

1 - Alt Sınıf böyle bir şey ile UITabBarController ve geçersiz shouldAutorotate:

override var shouldAutorotate: Bool { 
    guard let viewController = tabBarController?.selectedViewController else { return false } 
    return viewController.shouldAutorotate 
} 

Bu AutoRotate gerekiyorsa seçilen UIViewController tanımlamak anlamına gelir.

2 - Şimdi AutoRotate gerekiyorsa UITabBarController onun ViewControllers soracaktır o, siz de her denetleyicisi shouldAutorotate geçersiz kılmak gerekir.

Ve işte bu!

Sorunuzu yanlış anlamış olursam özür dilerim. Daha fazla bilgi verirseniz yardımcı olur.

Kendinize iyi bakın :)

+1

nice! Bana yardımcı oldu –

İlgili konular