UIBezierPath for drawing a border on a UIButton with animations such as a 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: exit
source share