画一条水平线并移动它

时间:2017-06-15 14:49:36

标签: swift animation calayer

我在swift中制作条形码扫描仪,我想在条形码区域添加一条带有水平红线的动画,我希望这条线上下移动......

我用calayer尝试了几个代码......我可以画画,但我不知道如何移动它(重复一次)

你可以帮帮我吗?

drawLine(onLayer: view.layer, fromPoint: CGPoint(x:100, y:100), toPoint: CGPoint(x:400, y:100))

func drawLine(onLayer layer: CALayer, fromPoint start: CGPoint, toPoint end: CGPoint) {

        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.move(to: start)
        linePath.addLine(to: end)
        line.path = linePath.cgPath
        line.fillColor = nil
        line.opacity = 1.0
        line.strokeColor = UIColor.red.cgColor
        layer.addSublayer(line)
    }

1 个答案:

答案 0 :(得分:1)

您可以使用绘制基本UIView绘制线条,或者像您一样绘制任何其他方式。然后移动它,您可以使用UIView.animation

只是一个用于移动UIView的简单代码(不确定是否可以使用此移动任何其他东西);

UIView.animate(withDuration: 0.2, delay: 0, options: [.autoreverse, .repeat], animations: { 
     self.view.transform = CGAffineTransform(translationX: newX, y: newY)
}, completion: nil)
相关问题