Animate a bezier path drawn in drawRect() Swift

后端 未结 2 1610
刺人心
刺人心 2020-12-25 14:49

I have this shape that i draw in drawRect()

var rectanglePath = UIBezierPath()

override func drawRect(rect: CGRect) {
    rectanglePath = UIBez         


        
2条回答
  •  清酒与你
    2020-12-25 15:30

    UIBezierPath to draw border on UIButton with animation like loader (Swift 5)

    func animation(){
        CATransaction.begin()
    
        let layer : CAShapeLayer = CAShapeLayer()
        layer.strokeColor = UIColor.purple.cgColor
        layer.lineWidth = 3.0
        layer.fillColor = UIColor.clear.cgColor
    
        let path : UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.btn.frame.size.width + 2, height: self.btn.frame.size.height + 2), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 5.0, height: 0.0))
        layer.path = path.cgPath
    
        let animation : CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0.0
        animation.toValue = 1.0
    
        animation.duration = 7.0
    
        CATransaction.setCompletionBlock{ [weak self] in
            print("Animation completed")
        }
    
        layer.add(animation, forKey: "myStroke")
        CATransaction.commit()
        self.btn.layer.addSublayer(layer)
    }
    

    Note:Animation Output

提交回复
热议问题