2017-08-03 74 views
0

我試圖在AVCaptureVideoPreviewLayer之上創建覆蓋圖。以編程方式在AVCaptureVideoPreviewLayer上添加UIButton

我嘗試以下,但結果不是我所期待的按鈕的標題是不可見的:

let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.avCaptureSession) 
previewLayer.frame = self.view.layer.frame 
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
self.view.layer.addSublayer(previewLayer) 



//Add Button 
let captureButton = UIButton(frame: CGRectMake(0, 0, 100 , 100)) 
captureButton.backgroundColor = UIColor.grayColor() 
captureButton.setTitle("Capture", forState: .Normal) 
captureButton.setTitleColor(UIColor.redColor(), forState: .Normal) 
captureButton.addTarget(self, action: "actionCapture:", forControlEvents: .TouchDragInside) 

previewLayer.addSublayer(captureButton.layer) 

這是當前狀態的屏幕截圖:

screenshot of the current state

+0

結果是什麼?你能告訴我們一個截圖嗎? – the4kman

+0

@ the4kman我添加了屏幕截圖。 – Cloy

回答

2

您應該將previewLayer添加到單獨的視圖的子圖層。這樣,您可以輕鬆地在主視圖上添加其他子視圖。

let previewView = UIView(frame: view.frame) 
view.addSubview(previewView) 

previewView.layer.addSublayer(previewLayer) 

[...] 

view.addSubview(captureButton) 
相關問題