Sachin Sachin - 28 days ago 19
Swift Question

How to add left to right animation to textlayer in swift

I am trying to create a video editor in swift. Currently, I need to add a textlayer in which I need to navigate text from right to left. Code I used for it is given below

let textLayer = CATextLayer()
textLayer.backgroundColor = UIColor.clearColor().CGColor
textLayer.foregroundColor = UIColor.whiteColor().CGColor
textLayer.string = "T E S T"
textLayer.font = UIFont(name: "Helvetica", size: 28)
textLayer.shadowOpacity = 0.5
textLayer.alignmentMode = kCAAlignmentCenter
textLayer.frame = CGRectMake(0, 50, showInBounds.width,showInBounds.height/6)
textLayer.shouldRasterize = true

let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRectMake(0, 0, showInBounds.width, showInBounds.height);
videoLayer.frame = CGRectMake(0, 0, showInBounds.width, showInBounds.height);
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(textLayer)

mainComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)
return (mixComposition, mainComposition)


Now, Text is appearing but navigation is not working. Please help. Thanks in advance

Answer

I have solved the issue using the following animation method

let animation = CABasicAnimation(keyPath: "transform.translation.x")