Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- extension FloatingPoint {
- var degreesToRadians: Self { self * .pi / 180 }
- var radiansToDegrees: Self { self * 180 / .pi }
- }
- @IBDesignable class ArcView: UIView {
- @IBInspectable var arcRadius: CGFloat = 40 {
- didSet {
- drawShap()
- }
- }
- @IBInspectable var fillColor:UIColor = .white {
- didSet {
- drawShap()
- }
- }
- @IBInspectable var strokeColor:UIColor = UIColor.red {
- didSet {
- drawShap()
- }
- }
- @IBInspectable var lineWidth:CGFloat = 2 {
- didSet {
- drawShap()
- }
- }
- @IBInspectable var cornerRadius:CGFloat = 0 {
- didSet {
- drawShap()
- }
- }
- override class var layerClass: AnyClass {
- CAShapeLayer.self
- }
- private var shapeLayer: CAShapeLayer {
- self.layer as! CAShapeLayer
- }
- private func drawShap() {
- shapeLayer.path = getBezierPath()
- shapeLayer.fillColor = fillColor.resolvedColor(with: traitCollection).cgColor
- shapeLayer.strokeColor = strokeColor.resolvedColor(with: traitCollection).cgColor
- shapeLayer.lineWidth = lineWidth
- shapeLayer.shadowColor = UIColor.black.cgColor
- shapeLayer.shadowOpacity = 0.15
- shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
- shapeLayer.masksToBounds = false
- }
- private func getBezierPath() -> CGPath {
- let radius: CGFloat = arcRadius
- let s_radious: CGFloat = 8
- let alpha = asin(s_radious/radius).radiansToDegrees
- let path = UIBezierPath()
- let halfWidth = self.frame.width / 2
- path.move(to: CGPoint(x: cornerRadius, y: 0))
- path.addLine(to: CGPoint(x: (halfWidth - radius - s_radious), y: 0))
- path.addArc(withCenter: .init(x: halfWidth-(radius+s_radious), y: s_radious),
- radius: s_radious,
- startAngle: CGFloat(-90).degreesToRadians,
- endAngle: CGFloat(0).degreesToRadians,
- clockwise: true)
- path.addArc(withCenter: CGPoint(x: halfWidth, y: 0),
- radius: radius + s_radious/6,
- startAngle: CGFloat(180 - alpha).degreesToRadians,
- endAngle: CGFloat(0+alpha).degreesToRadians,
- clockwise: false)
- path.addArc(withCenter: .init(x: halfWidth+radius+s_radious, y: s_radious),
- radius: s_radious,
- startAngle: CGFloat(180).degreesToRadians,
- endAngle: CGFloat(270).degreesToRadians,
- clockwise: true)
- print("\(halfWidth-(radius+s_radious))")
- print("\(halfWidth), \(radius), \(s_radious)")
- path.addLine(to: CGPoint(x: self.frame.width - cornerRadius, y: 0))
- path.addArc(withCenter: CGPoint(x: frame.width - cornerRadius, y: frame.height - cornerRadius), radius: cornerRadius, startAngle: CGFloat(-90).degreesToRadians, endAngle: CGFloat(90).degreesToRadians, clockwise: true)
- path.addLine(to: CGPoint(x: cornerRadius, y: self.frame.height))
- path.addArc(withCenter: CGPoint(x: cornerRadius, y: frame.height/2), radius: cornerRadius, startAngle: CGFloat(90).degreesToRadians, endAngle: CGFloat(-90).degreesToRadians, clockwise: true)
- path.close()
- return path.cgPath
- }
- override func layoutSubviews() {
- super.layoutSubviews()
- drawShap()
- }
- override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
- super.traitCollectionDidChange(previousTraitCollection)
- if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
- drawShap()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement