Draw line animated

前端 未结 4 1817
终归单人心
终归单人心 2020-12-12 17:58

I want to have a line in the center of the screen and animate it like a snake

This is step by step animation I want to make

How can I do this?

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 18:08

    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

        let path = UIBezierPath()
    

    i have given x, y values according to my need, you can change it according to yours

    path.move(to: CGPoint(x: 134, y: 209))//
        path.addLine(to: CGPoint(x: (131 + 93), y: 209))// for drawing line
        path.addQuadCurve(to: CGPoint(x: (131 + 97), y: 212) , controlPoint: CGPoint(x: (131 + 93), y: 209))// for drawing a curve
        path.addLine(to: CGPoint(x: (131 + 97) , y: (209 + 33 )))
        path.addQuadCurve(to: CGPoint(x: (131 + 93), y: (209 + 37)), controlPoint:  CGPoint(x: (131 + 97) , y: (209 + 33 )))
        path.addLine(to: CGPoint(x: 135, y: (209 + 37)))
        path.addQuadCurve(to: CGPoint(x: 131, y: 209 + 33), controlPoint: CGPoint(x: 135, y: 209 + 37))
        path.addLine(to: CGPoint(x: 131, y: 213))
        path.addQuadCurve(to: CGPoint(x: 134, y: 209), controlPoint:CGPoint(x: 131, y: 213) )
         let shapelayer = CAShapeLayer()//create shape layer object
        shapelayer.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
        shapelayer.strokeColor = #colorLiteral(red: 0.06274510175, green: 0, blue: 0.1921568662, alpha: 1).cgColor
        shapelayer.lineWidth = 2
        shapelayer.path = path.cgPath
        view.layer.addSublayer(shapelayer)
        let animation = CABasicAnimation(keyPath: "strokeEnd")// create animation and add it to shape layer
        animation.fromValue = 0
        animation.duration = 3
        shapelayer.add(animation, forKey: "MyAnimation")
        self.shapelayer = shapelayer
    

    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

提交回复
热议问题