【发布时间】:2017-07-05 15:47:05
【问题描述】:
目前我正在尝试在相机源上画一个圆圈。为此,我创建了另一个 UIView,设置 frame/color/cornerRadius,将其添加到 self.view 的子视图中,然后将圆形 UIView 带到前面。由于某种原因,它没有出现。
我之前已经这样做了,并且我已经编写了与之前完全相同的代码,但它仍然没有显示。
这是我制作视图的地方:
//----Circle View----------
circleView = UIView(frame: CGRect(x: 150, y: 150, width: 100, height: 100))
circleView.layer.cornerRadius = self.circleView.frame.width/2
circleView.layer.borderColor = UIColor.green.cgColor
circleView.layer.zPosition = 1
//self.circleView.isHidden = true
self.view.addSubview(circleView)
这是我把它带到前面的地方:
if (captureSession?.canAddOutput(dataOutput))!
{
captureSession?.addOutput(dataOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
self.view.layer.addSublayer(previewLayer!)
self.view.bringSubview(toFront: circleView)
self.view.bringSubview(toFront: captureButton)
self.view.bringSubview(toFront: stopButton)
previewLayer?.frame = self.view.bounds
}
有没有办法让 circleView 显示?
非常感谢。
【问题讨论】:
-
使用
Debug View Hierarchy... 它真的存在于层次结构中吗?如果是这样,z 顺序是否有问题,所以它隐藏在某些东西后面?或者,框架是否错误,所以它被放置在视图边界之外? -
您可能需要为您的
circleView添加约束 -
你也可以尝试把这行
self.view.bringSubview(toFront: circleView)放在最后(previewLayer?.frame = self.view.bounds之后 -
将您的 previewLayer 的 zPosition 设为 1,并为控件(或在本例中为圆形视图)的 zPosition 设为 2
-
感谢您的帮助。在 Debug 视图层次结构中,它表示该视图与显示在 previewLayer 上的其他视图处于同一级别。会不会是现有的约束干扰了看到的 circleView?