That's all. Hope it can help someone, as i also achieved this after a few hours effort because didn't find the exact solution. check this link as well:
My need was something similar - wanted to let the motion of a shape trace a line in the scene, but was having great difficulty synchronizing animation in CAShapeLayer with animation in SKScene.
So ended up using a different approach:
import SpriteKit
import PlaygroundSupport
import AVFoundation
let start = CGPoint(x: 100, y: 50)
let end = CGPoint(x: 200, y: 50)
let control = CGPoint(x: 150, y: 100);
var motion: SKAction = SKAction();
let radius: CGFloat = 20;
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let skview = SKView(frame: bounds)
PlaygroundPage.current.liveView = skview
PlaygroundPage.current.needsIndefiniteExecution = true
class MyScene: SKScene,AVSpeechSynthesizerDelegate {
var motionComplete = false
var redline: SKShapeNode = SKShapeNode();
var greenball: SKShapeNode = SKShapeNode();
override func sceneDidLoad() {
greenball = SKShapeNode(circleOfRadius: radius);
greenball.position = start;
greenball.fillColor = .green;
let motionpath = CGMutablePath();
motionpath.move(to: start)
motionpath.addQuadCurve(to: end, control: control)
motion = SKAction.follow(motionpath, asOffset: false, orientToPath: true,duration: 2);
let linepath = CGMutablePath();
linepath.move(to: start);
redline = SKShapeNode(path: linepath);
redline.strokeColor = .red;
redline.lineWidth = 5;
greenball.run(motion) {
self.motionComplete = true;
};
self.addChild(greenball);
self.addChild(redline);
}
override func update(_ currentTime: TimeInterval) {
if (motionComplete == false) {
let cgpath = self.redline.path as! CGMutablePath;
cgpath.addLine(to: greenball.position);
self.redline.path = cgpath;
}
}
}
let scene = MyScene(size: CGSize(width: 400, height: 200));
scene.scaleMode = SKSceneScaleMode.aspectFill
scene.size = skview.bounds.size
skview.presentScene(scene)
You can animate the end of the stroke of a path on a CAShapeLayer, e.g.,
weak var shapeLayer: CAShapeLayer?
@IBAction func didTapButton(_ sender: Any) {
// remove old shape layer if any
self.shapeLayer?.removeFromSuperlayer()
// create whatever path you want
let path = UIBezierPath()
path.move(to: CGPoint(x: 10, y: 50))
path.addLine(to: CGPoint(x: 200, y: 50))
path.addLine(to: CGPoint(x: 200, y: 240))
// create shape layer for that path
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
shapeLayer.strokeColor = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1).cgColor
shapeLayer.lineWidth = 4
shapeLayer.path = path.cgPath
// animate it
view.layer.addSublayer(shapeLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.duration = 2
shapeLayer.add(animation, forKey: "MyAnimation")
// save shape layer
self.shapeLayer = shapeLayer
}
That yields:
Clearly, you can change the UIBezierPath to whatever suits your interests. For example, you could have spaces in the path. Or you don't even need to have rectilinear paths:
With this, you can achieve any amount of red line movement.
The other method is to actually draw a line. I won't go into detail about that, but would recommend this article.
One last thing... On stack overflow we appreciate people who show they properly researched a topic before just asking a question. Using google, I was able to quickly come up with many tutorials on how to do what you are looking for. Try to do research yourself, because often a tutorial will have far more detail that is important than a stack overflow answer.
This technique can also be used, its added as border you can mold it your way. First create a path and then draw lines on it accordingly
i have given x, y values according to my need, you can change it according to yours
That's all. Hope it can help someone, as i also achieved this after a few hours effort because didn't find the exact solution. check this link as well:
https://github.com/Shahijabeen/BorderAnimation
My need was something similar - wanted to let the motion of a shape trace a line in the scene, but was having great difficulty synchronizing animation in CAShapeLayer with animation in SKScene.
So ended up using a different approach:
Result:
You can animate the end of the stroke of a
path
on aCAShapeLayer
, e.g.,That yields:
Clearly, you can change the
UIBezierPath
to whatever suits your interests. For example, you could have spaces in the path. Or you don't even need to have rectilinear paths:You can also combine animation of both the start and end of the stroke in a
CAAnimationGroup
:Yielding:
CoreAnimation gives you a lot of control over how the stroked path is rendered.
One method is using a UIView with the background color. Try something like this.
With this, you can achieve any amount of red line movement.
The other method is to actually draw a line. I won't go into detail about that, but would recommend this article.
One last thing... On stack overflow we appreciate people who show they properly researched a topic before just asking a question. Using google, I was able to quickly come up with many tutorials on how to do what you are looking for. Try to do research yourself, because often a tutorial will have far more detail that is important than a stack overflow answer.