Advertisement
Guest User

Untitled

a guest
Apr 15th, 2021
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.87 KB | None | 0 0
  1. import UIKit
  2.  
  3. extension FloatingPoint {
  4.     var degreesToRadians: Self { self * .pi / 180 }
  5.     var radiansToDegrees: Self { self * 180 / .pi }
  6. }
  7.  
  8. @IBDesignable class ArcView: UIView {
  9.    
  10.     @IBInspectable var arcRadius: CGFloat = 40 {
  11.         didSet {
  12.             drawShap()
  13.         }
  14.     }
  15.    
  16.     @IBInspectable var fillColor:UIColor = .white {
  17.         didSet {
  18.             drawShap()
  19.         }
  20.     }
  21.    
  22.     @IBInspectable var strokeColor:UIColor = UIColor.red {
  23.         didSet {
  24.             drawShap()
  25.         }
  26.     }
  27.    
  28.     @IBInspectable var lineWidth:CGFloat = 2 {
  29.         didSet {
  30.             drawShap()
  31.         }
  32.     }
  33.    
  34.     @IBInspectable var cornerRadius:CGFloat = 0 {
  35.         didSet {
  36.             drawShap()
  37.         }
  38.     }
  39.    
  40.     override class var layerClass: AnyClass {
  41.         CAShapeLayer.self
  42.     }
  43.    
  44.     private var shapeLayer: CAShapeLayer {
  45.         self.layer as! CAShapeLayer
  46.     }
  47.    
  48.     private func drawShap() {
  49.         shapeLayer.path = getBezierPath()
  50.         shapeLayer.fillColor = fillColor.resolvedColor(with: traitCollection).cgColor
  51.         shapeLayer.strokeColor = strokeColor.resolvedColor(with: traitCollection).cgColor
  52.         shapeLayer.lineWidth = lineWidth
  53.        
  54.         shapeLayer.shadowColor = UIColor.black.cgColor
  55.         shapeLayer.shadowOpacity = 0.15
  56.         shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
  57.         shapeLayer.masksToBounds = false
  58.     }
  59.    
  60.     private func getBezierPath() -> CGPath {
  61.    
  62.         let radius: CGFloat = arcRadius
  63.         let s_radious: CGFloat = 8
  64.         let alpha = asin(s_radious/radius).radiansToDegrees
  65.         let path = UIBezierPath()
  66.         let halfWidth = self.frame.width / 2
  67.        
  68.         path.move(to: CGPoint(x: cornerRadius, y: 0))
  69.         path.addLine(to: CGPoint(x: (halfWidth - radius - s_radious), y: 0))
  70.        
  71.         path.addArc(withCenter: .init(x: halfWidth-(radius+s_radious), y: s_radious),
  72.                     radius: s_radious,
  73.                     startAngle: CGFloat(-90).degreesToRadians,
  74.                     endAngle: CGFloat(0).degreesToRadians,
  75.                     clockwise: true)
  76.        
  77.         path.addArc(withCenter: CGPoint(x: halfWidth, y: 0),
  78.                     radius: radius + s_radious/6,
  79.                     startAngle: CGFloat(180 - alpha).degreesToRadians,
  80.                     endAngle: CGFloat(0+alpha).degreesToRadians,
  81.                     clockwise: false)
  82.        
  83.         path.addArc(withCenter: .init(x: halfWidth+radius+s_radious, y: s_radious),
  84.                     radius: s_radious,
  85.                     startAngle: CGFloat(180).degreesToRadians,
  86.                     endAngle: CGFloat(270).degreesToRadians,
  87.                     clockwise: true)
  88.         print("\(halfWidth-(radius+s_radious))")
  89.         print("\(halfWidth), \(radius), \(s_radious)")
  90.        
  91.         path.addLine(to: CGPoint(x: self.frame.width - cornerRadius, y: 0))
  92.         path.addArc(withCenter: CGPoint(x: frame.width - cornerRadius, y: frame.height - cornerRadius), radius: cornerRadius, startAngle: CGFloat(-90).degreesToRadians, endAngle: CGFloat(90).degreesToRadians, clockwise: true)
  93.         path.addLine(to: CGPoint(x: cornerRadius, y: self.frame.height))
  94.         path.addArc(withCenter: CGPoint(x: cornerRadius, y: frame.height/2), radius: cornerRadius, startAngle: CGFloat(90).degreesToRadians, endAngle: CGFloat(-90).degreesToRadians, clockwise: true)
  95.         path.close()
  96.         return path.cgPath
  97.     }
  98.    
  99.     override func layoutSubviews() {
  100.         super.layoutSubviews()
  101.         drawShap()
  102.     }
  103.    
  104.     override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  105.         super.traitCollectionDidChange(previousTraitCollection)
  106.         if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
  107.             drawShap()
  108.         }
  109.     }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement