Guest User

Untitled

a guest
Jun 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import UIKit
  2.  
  3. class CircleView: UIView {
  4.  
  5. var viewLoaded:Bool = false
  6.  
  7. var lineWidth:CGFloat = 10
  8. var animationDuration:Double = 5
  9.  
  10. let circlePathLayer = CAShapeLayer()
  11.  
  12. private func animate() -> Void {
  13. let animation = CABasicAnimation(keyPath: "strokeEnd")
  14. animation.fromValue = 0
  15. animation.toValue = 1
  16. animation.duration = self.animationDuration
  17. animation.autoreverses = false
  18. animation.repeatCount = 1.0
  19. self.circlePathLayer.add(animation, forKey: "line")
  20. }
  21.  
  22. private func configure() {
  23. let frame = circlePathFrame()
  24. self.circlePathLayer.frame = frame
  25. self.circlePathLayer.lineWidth = self.lineWidth
  26. self.circlePathLayer.fillColor = UIColor.lightGray.cgColor
  27. self.circlePathLayer.strokeColor = UIColor.blue.cgColor
  28. //line shape (rounded corners)
  29. self.circlePathLayer.lineCap = kCALineCapRound
  30. self.circlePathLayer.path = UIBezierPath(ovalIn: frame).cgPath
  31. layer.addSublayer(circlePathLayer)
  32. backgroundColor = .green
  33. }
  34.  
  35. private func circlePathFrame() -> CGRect {
  36. let circleRadius = self.radius()
  37. return CGRect(x:self.lineWidth/4, y: self.lineWidth/4, width: 2 * circleRadius , height: 2 * circleRadius)
  38. }
  39.  
  40. private func radius() -> CGFloat {
  41. if self.frame.size.width > self.frame.size.height {
  42. return self.frame.size.height/2 - self.lineWidth/2
  43. }
  44. else {
  45. return self.frame.size.width/2 - self.lineWidth/2
  46. }
  47. }
  48.  
  49. //MARK: - Layout subviews
  50.  
  51. override public func layoutSubviews() {
  52. super.layoutSubviews()
  53.  
  54. if let _ = self.superview,
  55. !self.viewLoaded {
  56.  
  57. self.viewLoaded = true
  58. self.configure()
  59. self.animate()
  60. }
  61. }
  62.  
  63. //MARK: - Reload circle
  64.  
  65. func reloadCircle() -> Void {
  66.  
  67. self.viewLoaded = false
  68. self.setNeedsLayout()
  69. self.layoutIfNeeded()
  70. }
  71. }
Add Comment
Please, Sign In to add comment