@@ -36,72 +36,73 @@ public class SPAlertIconHeartView: UIView {
36
36
super. draw ( rect)
37
37
HeartDraw . drawHeart ( frame: rect, resizing: . aspectFit, fillColor: self . tintColor)
38
38
}
39
- }
40
-
41
- class HeartDraw : NSObject {
42
39
43
- @objc dynamic public class func drawHeart( frame targetFrame: CGRect = CGRect ( x: 0 , y: 0 , width: 510 , height: 470 ) , resizing: ResizingBehavior = . aspectFit, fillColor: UIColor = UIColor ( red: 0.000 , green: 0.000 , blue: 0.000 , alpha: 1.000 ) ) {
44
-
45
- let context = UIGraphicsGetCurrentContext ( ) !
40
+ class HeartDraw : NSObject {
46
41
47
- context. saveGState ( )
48
- let resizedFrame : CGRect = resizing. apply ( rect: CGRect ( x: 0 , y: 0 , width: 510 , height: 470 ) , target: targetFrame)
49
- context. translateBy ( x: resizedFrame. minX, y: resizedFrame. minY)
50
- context. scaleBy ( x: resizedFrame. width / 510 , y: resizedFrame. height / 470 )
51
-
52
- let bezierPath = UIBezierPath ( )
53
- bezierPath. move ( to: CGPoint ( x: 255 , y: 469.6 ) )
54
- bezierPath. addLine ( to: CGPoint ( x: 219.3 , y: 433.9 ) )
55
- bezierPath. addCurve ( to: CGPoint ( x: 0 , y: 140.65 ) , controlPoint1: CGPoint ( x: 86.7 , y: 316.6 ) , controlPoint2: CGPoint ( x: 0 , y: 237.55 ) )
56
- bezierPath. addCurve ( to: CGPoint ( x: 140.25 , y: 0.4 ) , controlPoint1: CGPoint ( x: 0 , y: 61.6 ) , controlPoint2: CGPoint ( x: 61.2 , y: 0.4 ) )
57
- bezierPath. addCurve ( to: CGPoint ( x: 255 , y: 53.95 ) , controlPoint1: CGPoint ( x: 183.6 , y: 0.4 ) , controlPoint2: CGPoint ( x: 226.95 , y: 20.8 ) )
58
- bezierPath. addCurve ( to: CGPoint ( x: 369.75 , y: 0.4 ) , controlPoint1: CGPoint ( x: 283.05 , y: 20.8 ) , controlPoint2: CGPoint ( x: 326.4 , y: 0.4 ) )
59
- bezierPath. addCurve ( to: CGPoint ( x: 510 , y: 140.65 ) , controlPoint1: CGPoint ( x: 448.8 , y: 0.4 ) , controlPoint2: CGPoint ( x: 510 , y: 61.6 ) )
60
- bezierPath. addCurve ( to: CGPoint ( x: 290.7 , y: 433.9 ) , controlPoint1: CGPoint ( x: 510 , y: 237.55 ) , controlPoint2: CGPoint ( x: 423.3 , y: 316.6 ) )
61
- bezierPath. addLine ( to: CGPoint ( x: 255 , y: 469.6 ) )
62
- bezierPath. close ( )
63
- fillColor. setFill ( )
64
- bezierPath. fill ( )
65
-
66
- context. restoreGState ( )
67
- }
68
-
69
- @objc ( HeartStyleKitResizingBehavior)
70
- public enum ResizingBehavior : Int {
71
- case aspectFit
72
- case aspectFill
73
- case stretch
74
- case center
42
+ @objc dynamic public class func drawHeart( frame targetFrame: CGRect = CGRect ( x: 0 , y: 0 , width: 510 , height: 470 ) , resizing: ResizingBehavior = . aspectFit, fillColor: UIColor = UIColor ( red: 0.000 , green: 0.000 , blue: 0.000 , alpha: 1.000 ) ) {
43
+
44
+ let context = UIGraphicsGetCurrentContext ( ) !
45
+
46
+ context. saveGState ( )
47
+ let resizedFrame : CGRect = resizing. apply ( rect: CGRect ( x: 0 , y: 0 , width: 510 , height: 470 ) , target: targetFrame)
48
+ context. translateBy ( x: resizedFrame. minX, y: resizedFrame. minY)
49
+ context. scaleBy ( x: resizedFrame. width / 510 , y: resizedFrame. height / 470 )
50
+
51
+ let bezierPath = UIBezierPath ( )
52
+ bezierPath. move ( to: CGPoint ( x: 255 , y: 469.6 ) )
53
+ bezierPath. addLine ( to: CGPoint ( x: 219.3 , y: 433.9 ) )
54
+ bezierPath. addCurve ( to: CGPoint ( x: 0 , y: 140.65 ) , controlPoint1: CGPoint ( x: 86.7 , y: 316.6 ) , controlPoint2: CGPoint ( x: 0 , y: 237.55 ) )
55
+ bezierPath. addCurve ( to: CGPoint ( x: 140.25 , y: 0.4 ) , controlPoint1: CGPoint ( x: 0 , y: 61.6 ) , controlPoint2: CGPoint ( x: 61.2 , y: 0.4 ) )
56
+ bezierPath. addCurve ( to: CGPoint ( x: 255 , y: 53.95 ) , controlPoint1: CGPoint ( x: 183.6 , y: 0.4 ) , controlPoint2: CGPoint ( x: 226.95 , y: 20.8 ) )
57
+ bezierPath. addCurve ( to: CGPoint ( x: 369.75 , y: 0.4 ) , controlPoint1: CGPoint ( x: 283.05 , y: 20.8 ) , controlPoint2: CGPoint ( x: 326.4 , y: 0.4 ) )
58
+ bezierPath. addCurve ( to: CGPoint ( x: 510 , y: 140.65 ) , controlPoint1: CGPoint ( x: 448.8 , y: 0.4 ) , controlPoint2: CGPoint ( x: 510 , y: 61.6 ) )
59
+ bezierPath. addCurve ( to: CGPoint ( x: 290.7 , y: 433.9 ) , controlPoint1: CGPoint ( x: 510 , y: 237.55 ) , controlPoint2: CGPoint ( x: 423.3 , y: 316.6 ) )
60
+ bezierPath. addLine ( to: CGPoint ( x: 255 , y: 469.6 ) )
61
+ bezierPath. close ( )
62
+ fillColor. setFill ( )
63
+ bezierPath. fill ( )
64
+
65
+ context. restoreGState ( )
66
+ }
75
67
76
- public func apply( rect: CGRect , target: CGRect ) -> CGRect {
77
- if rect == target || target == CGRect . zero {
78
- return rect
79
- }
68
+ @objc ( HeartStyleKitResizingBehavior)
69
+ public enum ResizingBehavior : Int {
80
70
81
- var scales = CGSize . zero
82
- scales. width = abs ( target. width / rect. width)
83
- scales. height = abs ( target. height / rect. height)
71
+ case aspectFit
72
+ case aspectFill
73
+ case stretch
74
+ case center
84
75
85
- switch self {
86
- case . aspectFit:
87
- scales. width = min ( scales. width, scales. height)
88
- scales. height = scales. width
89
- case . aspectFill:
90
- scales. width = max ( scales. width, scales. height)
91
- scales. height = scales. width
92
- case . stretch:
93
- break
94
- case . center:
95
- scales. width = 1
96
- scales. height = 1
76
+ public func apply( rect: CGRect , target: CGRect ) -> CGRect {
77
+ if rect == target || target == CGRect . zero {
78
+ return rect
79
+ }
80
+
81
+ var scales = CGSize . zero
82
+ scales. width = abs ( target. width / rect. width)
83
+ scales. height = abs ( target. height / rect. height)
84
+
85
+ switch self {
86
+ case . aspectFit:
87
+ scales. width = min ( scales. width, scales. height)
88
+ scales. height = scales. width
89
+ case . aspectFill:
90
+ scales. width = max ( scales. width, scales. height)
91
+ scales. height = scales. width
92
+ case . stretch:
93
+ break
94
+ case . center:
95
+ scales. width = 1
96
+ scales. height = 1
97
+ }
98
+
99
+ var result = rect. standardized
100
+ result. size. width *= scales. width
101
+ result. size. height *= scales. height
102
+ result. origin. x = target. minX + ( target. width - result. width) / 2
103
+ result. origin. y = target. minY + ( target. height - result. height) / 2
104
+ return result
97
105
}
98
-
99
- var result = rect. standardized
100
- result. size. width *= scales. width
101
- result. size. height *= scales. height
102
- result. origin. x = target. minX + ( target. width - result. width) / 2
103
- result. origin. y = target. minY + ( target. height - result. height) / 2
104
- return result
105
106
}
106
107
}
107
108
}
0 commit comments