Blame view

Twear/Basic/LoadingButton/LoaderShape.swift 6.08 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  //
  //  LoaderShape.swift
  //  LoaderButton
  //
  //  Created by 黄进文 on 2018/6/11.
  //  Copyright © 2018 Jovins. All rights reserved.
  //
  
  import UIKit
  
  enum LoaderShape {
      case ring
      case ringVertical
      case ringHorizontal
      case ringThirdFour
      case circle
      case circleSemi
      case line
      case stroke
      case rectangle
      case triangle
      case pacman
  
      /// Return CALayer of specific shape
      /// - Parameters:
      /// - size: size of draw shape area
      /// - color: stroke color of shape
      func layer(with size: CGSize, color: UIColor) -> CALayer {
  
          let layer: CAShapeLayer = LoaderLayer()
          var path: UIBezierPath = UIBezierPath()
          let lineWidth: CGFloat = 1
          switch self {
          case .ring:
               path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                           radius: (size.width * 0.5),
                           startAngle: CGFloat(-3 * Double.pi / 4),
                           endAngle: CGFloat(-Double.pi / 4),
                           clockwise: false)
              layer.fillColor = nil
              layer.strokeColor = color.cgColor
              layer.lineWidth = 2
          case .ringVertical:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: CGFloat(-3 * Double.pi / 4),
                          endAngle: CGFloat(-Double.pi / 4),
                          clockwise: true) /// clockwise: 顺时针
              path.move(to: CGPoint(x: size.width * 0.5 - size.width * 0.5 * cos(CGFloat(Double.pi / 4)),
                                    y: size.height * 0.5 + size.height * 0.5 * sin(CGFloat(Double.pi / 4))))
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: CGFloat(-5 * Double.pi / 4),
                          endAngle: CGFloat(-7 * Double.pi / 4), clockwise: false)
              layer.fillColor = nil
              layer.strokeColor = color.cgColor
              layer.lineWidth = lineWidth
          case .ringHorizontal:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: CGFloat(3 * Double.pi / 4),
                          endAngle: CGFloat(5 * Double.pi / 4),
                          clockwise: true)
              path.move(to: CGPoint(x: size.width * 0.5 + size.width * 0.5 * cos(CGFloat(Double.pi / 4)),
                                    y: size.height * 0.5 - size.height * 0.5 * sin(CGFloat(Double.pi / 4))))
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: CGFloat(-Double.pi / 4),
                          endAngle: CGFloat(Double.pi / 4),
                          clockwise: true)
              layer.fillColor = nil
              layer.strokeColor = color.cgColor
              layer.lineWidth = lineWidth
          case .ringThirdFour:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: CGFloat(-3 * Double.pi / 4),
                          endAngle: CGFloat(-Double.pi / 4),
                          clockwise: false)
              layer.fillColor = nil
              layer.strokeColor = color.cgColor
              layer.lineWidth = lineWidth
          case .circle:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: 0,
                          endAngle: CGFloat(2 * Double.pi), clockwise: false)
              layer.fillColor = color.cgColor
          case .circleSemi:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: CGFloat(-Double.pi / 6),
                          endAngle: CGFloat(-5 * Double.pi / 6),
                          clockwise: false)
              path.close()
              layer.fillColor = color.cgColor
          case .line:
              path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height),
                                  cornerRadius: size.width * 0.5)
              layer.fillColor = color.cgColor
          case .stroke:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width * 0.5,
                          startAngle: -(.pi * 0.5),
                          endAngle: .pi + .pi * 0.5,
                          clockwise: true)
              layer.fillColor = nil
              layer.strokeColor = color.cgColor
              layer.lineWidth = lineWidth
          case .rectangle:
              path.move(to: CGPoint(x: 0, y: 0))
              path.addLine(to: CGPoint(x: size.width, y: 0))
              path.addLine(to: CGPoint(x: size.width, y: size.height))
              path.addLine(to: CGPoint(x: 0, y: size.height))
              layer.fillColor = color.cgColor
          case .triangle:
              let offsetY = size.height / 4
              path.move(to: CGPoint(x: 0, y: size.height - offsetY))
              path.addLine(to: CGPoint(x: size.width * 0.5, y: size.height * 0.5 - offsetY))
              path.addLine(to: CGPoint(x: size.width, y: size.height - offsetY))
              path.close()
              layer.fillColor = color.cgColor
          case .pacman:
              path.addArc(withCenter: CGPoint(x: size.width * 0.5, y: size.height * 0.5),
                          radius: size.width / 4,
                          startAngle: 0,
                          endAngle: CGFloat(2 * Double.pi),
                          clockwise: true)
              layer.fillColor = nil
              layer.strokeColor = color.cgColor
              layer.lineWidth = size.width * 0.5
          }
          layer.backgroundColor = nil
          layer.path = path.cgPath
          layer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
          return layer
      }
  }