|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2019 Ivan Vorobei ( [email protected]) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +import UIKit |
| 23 | + |
| 24 | +public class SPAlertIconLikeView: UIView { |
| 25 | + |
| 26 | + init() { |
| 27 | + super.init(frame: .zero) |
| 28 | + self.backgroundColor = .clear |
| 29 | + } |
| 30 | + |
| 31 | + required init?(coder aDecoder: NSCoder) { |
| 32 | + fatalError("init(coder:) has not been implemented") |
| 33 | + } |
| 34 | + |
| 35 | + public override func draw(_ rect: CGRect) { |
| 36 | + super.draw(rect) |
| 37 | + LikeDraw.draw(frame: rect, resizing: .aspectFit, fillColor: self.tintColor) |
| 38 | + } |
| 39 | + |
| 40 | + class LikeDraw: NSObject { |
| 41 | + |
| 42 | + @objc dynamic public class func draw(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 60, height: 56), resizing: ResizingBehavior = .aspectFit, fillColor: UIColor = UIColor(red: 0.349, green: 0.345, blue: 0.353, alpha: 1.000)) { |
| 43 | + let context = UIGraphicsGetCurrentContext()! |
| 44 | + context.saveGState() |
| 45 | + let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 62, height: 65), target: targetFrame) |
| 46 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 47 | + context.scaleBy(x: resizedFrame.width / 62, y: resizedFrame.height / 65) |
| 48 | + context.saveGState() |
| 49 | + context.beginTransparencyLayer(auxiliaryInfo: nil) |
| 50 | + let clipPath = UIBezierPath() |
| 51 | + clipPath.move(to: CGPoint(x: 49.72, y: 61.4)) |
| 52 | + clipPath.addLine(to: CGPoint(x: 49.9, y: 61.36)) |
| 53 | + clipPath.addCurve(to: CGPoint(x: 54.26, y: 56.74), controlPoint1: CGPoint(x: 52.08, y: 60.85), controlPoint2: CGPoint(x: 54.26, y: 59.43)) |
| 54 | + clipPath.addCurve(to: CGPoint(x: 53.68, y: 54.41), controlPoint1: CGPoint(x: 54.26, y: 55.74), controlPoint2: CGPoint(x: 54.02, y: 55.02)) |
| 55 | + clipPath.addLine(to: CGPoint(x: 53.59, y: 54.26)) |
| 56 | + clipPath.addCurve(to: CGPoint(x: 53.73, y: 53.47), controlPoint1: CGPoint(x: 53.37, y: 53.89), controlPoint2: CGPoint(x: 53.42, y: 53.61)) |
| 57 | + clipPath.addCurve(to: CGPoint(x: 56.97, y: 48.86), controlPoint1: CGPoint(x: 55.49, y: 52.69), controlPoint2: CGPoint(x: 56.97, y: 51.13)) |
| 58 | + clipPath.addCurve(to: CGPoint(x: 56.12, y: 45.83), controlPoint1: CGPoint(x: 56.97, y: 47.69), controlPoint2: CGPoint(x: 56.66, y: 46.62)) |
| 59 | + clipPath.addLine(to: CGPoint(x: 55.99, y: 45.65)) |
| 60 | + clipPath.addCurve(to: CGPoint(x: 56.25, y: 44.58), controlPoint1: CGPoint(x: 55.66, y: 45.23), controlPoint2: CGPoint(x: 55.74, y: 44.89)) |
| 61 | + clipPath.addCurve(to: CGPoint(x: 58.37, y: 40.39), controlPoint1: CGPoint(x: 57.53, y: 43.86), controlPoint2: CGPoint(x: 58.37, y: 42.24)) |
| 62 | + clipPath.addCurve(to: CGPoint(x: 57.17, y: 36.98), controlPoint1: CGPoint(x: 58.37, y: 39.11), controlPoint2: CGPoint(x: 57.95, y: 37.68)) |
| 63 | + clipPath.addCurve(to: CGPoint(x: 57.34, y: 35.92), controlPoint1: CGPoint(x: 56.75, y: 36.59), controlPoint2: CGPoint(x: 56.83, y: 36.31)) |
| 64 | + clipPath.addCurve(to: CGPoint(x: 58.73, y: 32.29), controlPoint1: CGPoint(x: 58.23, y: 35.22), controlPoint2: CGPoint(x: 58.73, y: 33.88)) |
| 65 | + clipPath.addCurve(to: CGPoint(x: 53.84, y: 27.34), controlPoint1: CGPoint(x: 58.73, y: 29.55), controlPoint2: CGPoint(x: 56.61, y: 27.34)) |
| 66 | + clipPath.addLine(to: CGPoint(x: 43.86, y: 27.34)) |
| 67 | + clipPath.addCurve(to: CGPoint(x: 39.64, y: 23.96), controlPoint1: CGPoint(x: 41.32, y: 27.34), controlPoint2: CGPoint(x: 39.64, y: 26.03)) |
| 68 | + clipPath.addCurve(to: CGPoint(x: 44.4, y: 8.16), controlPoint1: CGPoint(x: 39.64, y: 20.1), controlPoint2: CGPoint(x: 44.4, y: 13.17)) |
| 69 | + clipPath.addCurve(to: CGPoint(x: 40.48, y: 4), controlPoint1: CGPoint(x: 44.4, y: 5.57), controlPoint2: CGPoint(x: 42.69, y: 4)) |
| 70 | + clipPath.addCurve(to: CGPoint(x: 36.35, y: 7.52), controlPoint1: CGPoint(x: 38.47, y: 4), controlPoint2: CGPoint(x: 37.44, y: 5.4)) |
| 71 | + clipPath.addCurve(to: CGPoint(x: 22.26, y: 28.04), controlPoint1: CGPoint(x: 32.21, y: 15.74), controlPoint2: CGPoint(x: 26.56, y: 22.36)) |
| 72 | + clipPath.addCurve(to: CGPoint(x: 16.75, y: 43.69), controlPoint1: CGPoint(x: 18.62, y: 32.9), controlPoint2: CGPoint(x: 16.84, y: 36.98)) |
| 73 | + clipPath.addCurve(to: CGPoint(x: 38.11, y: 61.97), controlPoint1: CGPoint(x: 16.61, y: 54), controlPoint2: CGPoint(x: 24.97, y: 61.86)) |
| 74 | + clipPath.addLine(to: CGPoint(x: 41.99, y: 62)) |
| 75 | + clipPath.addCurve(to: CGPoint(x: 49.72, y: 61.4), controlPoint1: CGPoint(x: 45.51, y: 62.02), controlPoint2: CGPoint(x: 48.13, y: 61.77)) |
| 76 | + clipPath.addLine(to: CGPoint(x: 49.72, y: 61.4)) |
| 77 | + clipPath.close() |
| 78 | + clipPath.move(to: CGPoint(x: 3, y: 43.89)) |
| 79 | + clipPath.addCurve(to: CGPoint(x: 14.18, y: 28.37), controlPoint1: CGPoint(x: 3, y: 35.19), controlPoint2: CGPoint(x: 7.86, y: 28.37)) |
| 80 | + clipPath.addLine(to: CGPoint(x: 18.29, y: 28.37)) |
| 81 | + clipPath.addLine(to: CGPoint(x: 17.96, y: 28.79)) |
| 82 | + clipPath.addCurve(to: CGPoint(x: 12.95, y: 43.61), controlPoint1: CGPoint(x: 15.56, y: 31.9), controlPoint2: CGPoint(x: 13.06, y: 36.82)) |
| 83 | + clipPath.addCurve(to: CGPoint(x: 20.22, y: 59.29), controlPoint1: CGPoint(x: 12.84, y: 50.01), controlPoint2: CGPoint(x: 15.16, y: 55.6)) |
| 84 | + clipPath.addLine(to: CGPoint(x: 15.21, y: 59.29)) |
| 85 | + clipPath.addCurve(to: CGPoint(x: 3, y: 43.89), controlPoint1: CGPoint(x: 8.23, y: 59.29), controlPoint2: CGPoint(x: 3, y: 52.27)) |
| 86 | + clipPath.close() |
| 87 | + clipPath.addClip() |
| 88 | + let rectanglePath = UIBezierPath(rect: CGRect(x: -2.02, y: -1, width: 65.75, height: 68)) |
| 89 | + fillColor.setFill() |
| 90 | + rectanglePath.fill() |
| 91 | + context.endTransparencyLayer() |
| 92 | + context.restoreGState() |
| 93 | + context.restoreGState() |
| 94 | + } |
| 95 | + |
| 96 | + @objc(StyleKitNameResizingBehavior) |
| 97 | + public enum ResizingBehavior: Int { |
| 98 | + |
| 99 | + case aspectFit |
| 100 | + case aspectFill |
| 101 | + case stretch |
| 102 | + case center |
| 103 | + |
| 104 | + public func apply(rect: CGRect, target: CGRect) -> CGRect { |
| 105 | + if rect == target || target == CGRect.zero { |
| 106 | + return rect |
| 107 | + } |
| 108 | + |
| 109 | + var scales = CGSize.zero |
| 110 | + scales.width = abs(target.width / rect.width) |
| 111 | + scales.height = abs(target.height / rect.height) |
| 112 | + |
| 113 | + switch self { |
| 114 | + case .aspectFit: |
| 115 | + scales.width = min(scales.width, scales.height) |
| 116 | + scales.height = scales.width |
| 117 | + case .aspectFill: |
| 118 | + scales.width = max(scales.width, scales.height) |
| 119 | + scales.height = scales.width |
| 120 | + case .stretch: |
| 121 | + break |
| 122 | + case .center: |
| 123 | + scales.width = 1 |
| 124 | + scales.height = 1 |
| 125 | + } |
| 126 | + |
| 127 | + var result = rect.standardized |
| 128 | + result.size.width *= scales.width |
| 129 | + result.size.height *= scales.height |
| 130 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 131 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 132 | + return result |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments