2017-06-15 97 views
0

我用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) 
    } 

回答

0

您可以繪製基本的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) 
+0

太棒了!謝謝 – Olivier

+0

如果你不能工作,請告知,然後我可以幫助你更多。如果確實有效,請不要忘記接受這個答案! :) – Faruk

+0

對我來說沒關係......感謝您的幫助! – Olivier