I created one scanner application, and in that I created a scanning animation using CABasicAnimation. Now the animation is happening from top to bottom in that frame. the code to achieved is given below.
position. The code for the way I created the animation is added below.
let animation = CABasicAnimation(keyPath: "position.y")
let gradienLayer = CAGradientLayer()
func createScanningIndicator(for view:UIView, ratio:CGFloat = 0.6){
let minimumSide = min(view.bounds.height, view.bounds.width)
let squareSide = minimumSide * ratio
gradienLayer.colors = [UIColor.green.withAlphaComponent(0).cgColor, UIColor.green.cgColor]
gradienLayer.opacity = 0.5
let xOffset = ((view.bounds.width/2) - (squareSide/2)) + 10
let yOffset = ((view.bounds.height/2) - (squareSide/2)) + 10
gradienLayer.frame = CGRect(x: xOffset, y: yOffset, width: squareSide-20, height: 15)
view.layer.insertSublayer(gradienLayer, at: 0)
let initialYPosition = gradienLayer.position.y
let finalYPosition = (initialYPosition + squareSide) - 40
animation.fromValue = initialYPosition as NSNumber
animation.toValue = finalYPosition as NSNumber
animation.duration = 2
animation.repeatCount = .infinity
animation.isRemovedOnCompletion = false
gradienLayer.add(animation, forKey: nil)
}
I need to update this code to work that animation in a loop, now the animation works from Botton infinitely, Currently the animation is happening from top to bottom tear means first it will animate from top to bottom and it will go back to the top and start to animate to bottom and repeat. But I want to happen that animation like, first from top to bottom , after that from bottom to top and repeat, is it possible to do ? if it's possible, how to do it?