【发布时间】:2018-04-24 22:29:00
【问题描述】:
我编写了一个用于扫描条形码的代码。我想在扫描条形码后显示一个视图。这是我的条码扫描器代码
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()?
【问题讨论】:
-
您使用的是哪个 Swift 版本?您确定正在调用委托方法吗?