QRCodereader 额外按钮不起作用(torchlight/flashlight、取消、切换相机)?

QRCodereader extra buttons not working (torchlight/flashlight, cancel, switchcamera)?

我已经将 (https://cocoapods.org/pods/QRCodeReader.swift) 中的示例应用程序实现到我自己的项目中,以便在我的主屏幕上制作一个显示条形码扫描仪的 uiview。我可以扫描条形码并打印结果,但手电筒(手电筒)按钮在按下时什么也不做,与取消和切换相机按钮相同,即使图标显示在视图上。可能是什么问题?

这是我的代码:

import UIKit
import QRCodeReader

class Plockning: UIViewController {

    lazy var reader: QRCodeReader = QRCodeReader(metadataObjectTypes: [.ean13, .upce, .code128], captureDevicePosition: .back)

    @IBOutlet weak var Uiview: QRCodeReaderView! {
        didSet {
          Uiview.setupComponents(with: QRCodeReaderViewControllerBuilder {
            [=11=].reader                 = reader
            [=11=].showTorchButton        = true // ICON SHOWS ON VIEW BUT PRESSING ON IT DOES NOTHING.
            [=11=].showSwitchCameraButton = true // ICON SHOWS ON VIEW BUT PRESSING ON IT DOES NOTHING.
            [=11=].showCancelButton       = true // ICON SHOWS ON VIEW BUT PRESSING ON IT DOES NOTHING.
            [=11=].showOverlayView        = true // THIS DOESN'T SHOW ON SCREEN
            [=11=].rectOfInterest         = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
          })
        }
      }




    override func viewDidLoad()
    {
        super.viewDidLoad()


        reader.startScanning()

        reader.didFindCode = { result in
          print("Completion with result: (result.value) of type (result.metadataType)")
        }
    }

}

您需要更全面地检查源代码。

这一行,例如:

.showTorchButton = true

向界面添加“默认”toggleTorchButton,但该按钮不会执行任何操作,直到您告诉它您想要它做什么。

在提供的 QRCodeReaderViewController 中,我们找到这一行:

builder.readerView.displayable.toggleTorchButton?.addTarget(self, action: #selector(toggleTorchAction), for: .touchUpInside)

和对应的选择器:

@objc func toggleTorchAction(_ button: ToggleTorchButton) {
    codeReader.toggleTorch()
}

因此,对于您的应用,您将实现相同的选择器功能,并将操作添加到您已设置的 QRCodeReaderView

class Plockning: UIViewController {
    
    lazy var reader: QRCodeReader = QRCodeReader(metadataObjectTypes: [.ean13, .upce, .code128], captureDevicePosition: .back)
    
    @IBOutlet weak var Uiview: QRCodeReaderView! {
        didSet {
            Uiview.setupComponents(with: QRCodeReaderViewControllerBuilder {
                [=13=].reader                 = reader
                [=13=].showTorchButton        = true // ICON SHOWS ON VIEW BUT PRESSING ON IT DOES NOTHING.
                [=13=].showSwitchCameraButton = true // ICON SHOWS ON VIEW BUT PRESSING ON IT DOES NOTHING.
                [=13=].showCancelButton       = true // ICON SHOWS ON VIEW BUT PRESSING ON IT DOES NOTHING.
                [=13=].showOverlayView        = true // THIS DOESN'T SHOW ON SCREEN
                [=13=].rectOfInterest         = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
            })
        }
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()

        // assign the touchUpInside action for the torch button
        Uiview.toggleTorchButton?.addTarget(self, action: #selector(self.toggleTorchAction(_:)), for: .touchUpInside)

        reader.startScanning()
        
        reader.didFindCode = { result in
            print("Completion with result: \(result.value) of type \(result.metadataType)")
        }
    }

    // handle the user tapping the torch button    
    @objc func toggleTorchAction(_ button: ToggleTorchButton) {
        reader.toggleTorch()
    }

}

您需要对显示的其他“内置”按钮执行相同的操作。

顺便说一下,Uiview 是一个 糟糕的 对象名称。使用类似于:

@IBOutlet weak var myScannerView: QRCodeReaderView!

或类似的东西。