是否有可能从swift中的qr代码读取中触发segue

时间:2015-02-22 23:10:52

标签: ios swift camera qr-code scanning

我有一个快速的ios swift编程问题。是否有可能从快速的QR码读取中触发segue?我在谷歌上查找了这个app coda tutroial。我仍然不明白我将如何用segue做这件事。有人可以解释他们是如何在应用程序中做到的吗?谢谢!

更新

感谢Christian Woerz的有用提示!

我发现了怎么做!

当这个功能:

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection 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 = CGRectZero
    messageLabel.text = "No QR code is detected"
    return
}

// Get the metadata object.
let metadataObj = metadataObjects[0] as AVMetadataMachineReadableCodeObject

if metadataObj.type == AVMetadataObjectTypeQRCode {
    // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
    let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as AVMetadataMachineReadableCodeObject
    qrCodeFrameView?.frame = barCodeObject.bounds;

    if metadataObj.stringValue != nil {
        messageLabel.text = metadataObj.stringValue
    }
  }
}

被触发在函数内部执行performSegueWithIdentifier(_ identifier: String?, sender sender: AnyObject?),如此

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection 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 = CGRectZero
    messageLabel.text = "No QR code is detected"
    return
}

// Get the metadata object.
let metadataObj = metadataObjects[0] as AVMetadataMachineReadableCodeObject

if metadataObj.type == AVMetadataObjectTypeQRCode {
    // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
    let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as AVMetadataMachineReadableCodeObject
    qrCodeFrameView?.frame = barCodeObject.bounds;

    if metadataObj.stringValue != nil {
        messageLabel.text = metadataObj.stringValue
    }
  }

**performSegueWithIdentifier(_ identifier: String?, sender sender:  AnyObject?)**

}

并传递segue的标识符,如

  

标识符:过渡

未经测试的代码,但有逻辑:)

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。

但我们只能给你一个通用的答案,因为有许多不同的方式和库来读取QR码。但是举个例子,您可以使用this tutorial中的QR码阅读器。 如果您使用本教程中提供的代码读取QR代码,则会调用一个接收QR代码数据的方法。在教程中,它是captureOutput

然后在这个方法中,如果调用该方法,你可以添加代码来执行segue。