备注
:使用第三方库[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协议
运行报错
:Cannot Record。
分析
:在模拟器中没有摄像头不能录制。
(4)重写handleCodeResult方法
override func handleCodeResult(arrayResult: [LBXScanResult]) {
if let result = arrayResult.first {
let msg = result.strScanned
print("扫描结果:" + msg!)
}
}
(5)自定义扫描动画
//扫描动画:在github下载项目,复制CodeScan.bundle获取图片
var style = LBXScanViewStyle()
style.anmiationStyle = .NetGrid
style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net")
scanStyle = style
2. 具体实现
(1)界面设计
(2)扫一扫对应的视图控制器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.
//自定义视图导航标题
self.title = "二维码/条码"
navigationController?.navigationBar.barStyle = .blackTranslucent
navigationController?.navigationBar.tintColor = UIColor.white
//扫描动画:在github下载项目,复制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("扫描结果:" + 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. 效果展示
4. 返回上级界面时速度慢
(1)分析:组件作者可能是按O-C的思路来写的swiftScan库的,swift时代不需要销毁(反初始化)的处理。
(2)解决方案:修改LBXScanViewController源码,注释viewWillDisappear()函数中的代码