gpt4 book ai didi

ios - 带扫描指示器的条形码阅读器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:01 26 4
gpt4 key购买 nike

我想使用带有扫描指示器的条形码阅读器。谁能告诉我应该使用哪个条形码 sdk。目前我正在使用 zbar sdk。在 zbar 中我们没有扫描指示器。请看我要实现的屏幕截图

enter image description here

最佳答案

我做了一个增强版的 @rgkobashi 答案,试图创建正方形并在正方形内为指示线设置动画。

扫描指示器和动画代码如下:

 func createScanningIndicator() {

let height: CGFloat = 15
let opacity: Float = 0.4
let topColor = UIColor.green.withAlphaComponent(0)
let bottomColor = UIColor.green

let layer = CAGradientLayer()
layer.colors = [topColor.cgColor, bottomColor.cgColor]
layer.opacity = opacity

let squareWidth = view.frame.width * 0.6
let xOffset = view.frame.width * 0.2
let yOffset = view.frame.midY - (squareWidth / 2)
layer.frame = CGRect(x: xOffset, y: yOffset, width: squareWidth, height: height)

self.view.layer.insertSublayer(layer, at: 0)

let initialYPosition = layer.position.y
let finalYPosition = initialYPosition + squareWidth - height
let duration: CFTimeInterval = 2

let animation = CABasicAnimation(keyPath: "position.y")
animation.fromValue = initialYPosition as NSNumber
animation.toValue = finalYPosition as NSNumber
animation.duration = duration
animation.repeatCount = .infinity
animation.isRemovedOnCompletion = false

layer.add(animation, forKey: nil)
}

扫描正方形如下图:

func createScanningFrame() {

let lineLength: CGFloat = 15
let squareWidth = view.frame.width * 0.6
let topLeftPosX = view.frame.width * 0.2
let topLeftPosY = view.frame.midY - (squareWidth / 2)
let btmLeftPosY = view.frame.midY + (squareWidth / 2)
let btmRightPosX = view.frame.midX + (squareWidth / 2)
let topRightPosX = view.frame.width * 0.8

let path = UIBezierPath()

//top left
path.move(to: CGPoint(x: topLeftPosX, y: topLeftPosY + lineLength))
path.addLine(to: CGPoint(x: topLeftPosX, y: topLeftPosY))
path.addLine(to: CGPoint(x: topLeftPosX + lineLength, y: topLeftPosY))

//bottom left
path.move(to: CGPoint(x: topLeftPosX, y: btmLeftPosY - lineLength))
path.addLine(to: CGPoint(x: topLeftPosX, y: btmLeftPosY))
path.addLine(to: CGPoint(x: topLeftPosX + lineLength, y: btmLeftPosY))

//bottom right
path.move(to: CGPoint(x: btmRightPosX - lineLength, y: btmLeftPosY))
path.addLine(to: CGPoint(x: btmRightPosX, y: btmLeftPosY))
path.addLine(to: CGPoint(x: btmRightPosX, y: btmLeftPosY - lineLength))

//top right
path.move(to: CGPoint(x: topRightPosX, y: topLeftPosY + lineLength))
path.addLine(to: CGPoint(x: topRightPosX, y: topLeftPosY))
path.addLine(to: CGPoint(x: topRightPosX - lineLength, y: topLeftPosY))

let shape = CAShapeLayer()
shape.path = path.cgPath
shape.strokeColor = UIColor.white.cgColor
shape.lineWidth = 3
shape.fillColor = UIColor.clear.cgColor

self.view.layer.insertSublayer(shape, at: 0)
}

结果如下图: enter image description here

关于ios - 带扫描指示器的条形码阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28939085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com