I have a class called CategoryCell which us UICollectionViewCell.
On the CellForItemAt function:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
drawfunction.draw_Footing(withView: cell.view)
the drawing will be in the cell.view.
draw_Footing functions is a function to draw some lines, and it is located in drawfunction class which it NSObject. in the same class, I have the function call animateShape which can animate a single line between two points (CGpoint).
func animateShape(view: UIView, p1: CGpoint, p2: CGPoint) {
let shapeLayer = CAShapeLayer()
shapeLayer.removeFromSuperlayer()
// create whatever path you want
shapeLayer.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
shapeLayer.strokeColor = color.cgColor
shapeLayer.lineWidth = linewidth//CGFloat(1.5)
shapeLayer.path = path.cgPath
// animate it
view.layer.addSublayer(shapeLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.duration = duration//0.5
shapeLayer.add(animation, forKey: "MyAnimation")
}
I have 4 points G1, G2, G3, G4. I need to animate a line between these 4 points. So, if I do:
animateShape(view, p1: G1, p2: G2)
animateShape(view, p1: G2, p2: G3)
animateShape(view, p1: G3, p2: G4)
All the line will be animated in the same time. I need to animate first the line between G1 and G2, and after completion, need to animate the line between G2 and G3 and not in the same time. I tried to include dispatchQueue, but I am not sure and I don't know how.
Any advise?
The things is, I do not see how the
pathwas actually created using your pointsp1andp2Anyways, I am assuming your end goal is to do a drawing line path animation in a
UICollectionViewCelland that is what I tried to achieve based on the given the description in your question.First the drawing class:
Then basic custom cell set up, nothing fancy, just added for completeness
The view controller set up where the most interesting parts are in the
willDisplay cellfunctionJust for completeness, my flow layout set up
This gives me an animated path in the collectionview cell
Hope this gives you some ideas to achieve your task
Update
Based on OP, Xin Lok's comment:
Based on that comment, One way I can think of achieving that is to I think the key is to reuse and persist with the shape layer and the path.
Here are some updates I made based on that conclusion
First I just made a simple struct so we can create lines easily
Then I create some random lines and grouped them in an array
Importantly note the line order in each array, because this is the order they will be drawn
In the drawing class, I made some changes to persist the
CAShapeLayerandpathA special mention to jrturton in the comments for suggesting
CGMutablePathand simplifying the path creation.Then some minor changes in the collectionview cell configuration
Now again, for convenience, remember the order in which they should be drawn:
The end result: