我写了一个扫描条形码的代码。我想在扫描条形码时显示视图。这是我的条形码扫描码
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}
//Get metadata object
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if supportedCodeTypes.contains(metadataObj.type) {
//if the found metadata is equal to the QR code metadata then update the status label's text and set the the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil {
var productbarcode = metadataObj.stringValue
messageLabel.text = productbarcode
print("Barcode detected")
}
}
}
我想要显示的视图是在func Setupproductcontainer()
中设置的。当我将它添加到viewDidLoad
中时,它工作得很好,但我不希望它在检测到条形码之前显示。因此,我尝试在print("Barcode detected")
语句下添加Setupproductcontainer()
,但是没有任何反应。当检测到条形码时,如何启动Setupproductcontainer()
?
发布于 2017-11-12 00:50:37
您可能正试图在后台线程上进行UI更改。
我建议您在DispatchQueue.main
代码块中调用函数,以便在主线程上运行它。你应该这样做的原因是,在UI上所做的每一次更改都应该在主线程中完成。如果您尝试在后台线程中更改它,则不会发生任何事情。
DispatchQueue.main.async {
self.Setupproductcontainer()
}
https://stackoverflow.com/questions/47244026
复制相似问题