備注
:使用第三方庫[swiftScan]
(https://github.com/MxABC/swiftScan/blob/master/README-Chinese.md)
1. 步驟
(1)使用cocoapods安裝swiftScan第三方庫
pod 'swiftScan' #二維碼掃描
說明
:使用cocoapods安裝第三方庫請參考Cocoapods管理IOS依賴庫
(2)編譯后引入swiftScan
import swiftScan
(3)遵從LBXScanViewController協(xié)議
遵從LBXScanViewController協(xié)議.png
運(yùn)行報錯
:Cannot Record堆生。分析
:在模擬器中沒有攝像頭不能錄制框冀。(4)重寫handleCodeResult方法
override func handleCodeResult(arrayResult: [LBXScanResult]) {
if let result = arrayResult.first {
let msg = result.strScanned
print("掃描結(jié)果:" + msg!)
}
}
(5)自定義掃描動畫
//掃描動畫:在github下載項(xiàng)目亮航,復(fù)制CodeScan.bundle獲取圖片
var style = LBXScanViewStyle()
style.anmiationStyle = .NetGrid
style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net")
scanStyle = style
2. 具體實(shí)現(xiàn)
(1)界面設(shè)計
界面設(shè)計.png
(2)掃一掃對應(yīng)的視圖控制器ScanViewController.swift
//
// ScanViewController.swift
// JackUChat
//
// Created by 徐云 on 2019/2/25.
// Copyright ? 2019 Liy. All rights reserved.
//
import UIKit
import swiftScan
class ScanViewController: LBXScanViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//自定義視圖導(dǎo)航標(biāo)題
self.title = "二維碼/條碼"
navigationController?.navigationBar.barStyle = .blackTranslucent
navigationController?.navigationBar.tintColor = UIColor.white
//掃描動畫:在github下載項(xiàng)目兜叨,復(fù)制CodeScan.bundle獲取圖片
var style = LBXScanViewStyle()
style.anmiationStyle = .NetGrid
style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net")//引用bundle中的圖片
scanStyle = style
}
override func handleCodeResult(arrayResult: [LBXScanResult]) {
if let result = arrayResult.first {
let msg = result.strScanned
print("掃描結(jié)果:" + msg!)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
3. 效果展示
二維碼掃描界面.png
4. 返回上級界面時速度慢
(1)分析:組件作者可能是按O-C的思路來寫的swiftScan庫的,swift時代不需要銷毀(反初始化)的處理软棺。
控制臺信息.png
(2)解決方案:修改LBXScanViewController源碼瓦侮,注釋viewWillDisappear()函數(shù)中的代碼
解決方案.png