|
| 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 SPAlertIconEjectView: 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 | + EjectDraw.draw(frame: rect, resizing: .aspectFit, fillColor: self.tintColor) |
| 38 | + } |
| 39 | + |
| 40 | + class EjectDraw: 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: 60, height: 60), target: targetFrame) |
| 46 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 47 | + context.scaleBy(x: resizedFrame.width / 60, y: resizedFrame.height / 60) |
| 48 | + let bezierPath = UIBezierPath() |
| 49 | + bezierPath.move(to: CGPoint(x: 52.91, y: 34.64)) |
| 50 | + bezierPath.addCurve(to: CGPoint(x: 57.54, y: 30.39), controlPoint1: CGPoint(x: 55.73, y: 34.64), controlPoint2: CGPoint(x: 57.54, y: 32.88)) |
| 51 | + bezierPath.addCurve(to: CGPoint(x: 56.01, y: 26.96), controlPoint1: CGPoint(x: 57.54, y: 29.26), controlPoint2: CGPoint(x: 56.98, y: 27.98)) |
| 52 | + bezierPath.addLine(to: CGPoint(x: 34.16, y: 4.58)) |
| 53 | + bezierPath.addCurve(to: CGPoint(x: 29.33, y: 2), controlPoint1: CGPoint(x: 32.35, y: 2.88), controlPoint2: CGPoint(x: 31.06, y: 2)) |
| 54 | + bezierPath.addCurve(to: CGPoint(x: 24.53, y: 4.58), controlPoint1: CGPoint(x: 27.63, y: 2), controlPoint2: CGPoint(x: 26.28, y: 2.91)) |
| 55 | + bezierPath.addLine(to: CGPoint(x: 2.85, y: 26.76)) |
| 56 | + bezierPath.addCurve(to: CGPoint(x: 1.12, y: 30.39), controlPoint1: CGPoint(x: 1.76, y: 27.81), controlPoint2: CGPoint(x: 1.09, y: 29.19)) |
| 57 | + bezierPath.addCurve(to: CGPoint(x: 5.75, y: 34.64), controlPoint1: CGPoint(x: 1.12, y: 32.88), controlPoint2: CGPoint(x: 2.93, y: 34.64)) |
| 58 | + bezierPath.addLine(to: CGPoint(x: 52.91, y: 34.64)) |
| 59 | + bezierPath.close() |
| 60 | + bezierPath.move(to: CGPoint(x: 51.86, y: 40.73)) |
| 61 | + bezierPath.addCurve(to: CGPoint(x: 57.69, y: 46.41), controlPoint1: CGPoint(x: 55.49, y: 40.73), controlPoint2: CGPoint(x: 57.69, y: 42.87)) |
| 62 | + bezierPath.addLine(to: CGPoint(x: 57.69, y: 52.42)) |
| 63 | + bezierPath.addCurve(to: CGPoint(x: 51.86, y: 58.13), controlPoint1: CGPoint(x: 57.69, y: 56.11), controlPoint2: CGPoint(x: 55.49, y: 58.13)) |
| 64 | + bezierPath.addLine(to: CGPoint(x: 6.86, y: 58.13)) |
| 65 | + bezierPath.addCurve(to: CGPoint(x: 1, y: 52.42), controlPoint1: CGPoint(x: 3.23, y: 58.13), controlPoint2: CGPoint(x: 1, y: 56.08)) |
| 66 | + bezierPath.addLine(to: CGPoint(x: 1, y: 46.41)) |
| 67 | + bezierPath.addCurve(to: CGPoint(x: 6.86, y: 40.73), controlPoint1: CGPoint(x: 1, y: 42.87), controlPoint2: CGPoint(x: 3.23, y: 40.73)) |
| 68 | + bezierPath.addLine(to: CGPoint(x: 51.86, y: 40.73)) |
| 69 | + bezierPath.close() |
| 70 | + fillColor.setFill() |
| 71 | + bezierPath.fill() |
| 72 | + context.restoreGState() |
| 73 | + } |
| 74 | + |
| 75 | + @objc(StyleKitNameResizingBehavior) |
| 76 | + public enum ResizingBehavior: Int { |
| 77 | + |
| 78 | + case aspectFit |
| 79 | + case aspectFill |
| 80 | + case stretch |
| 81 | + case center |
| 82 | + |
| 83 | + public func apply(rect: CGRect, target: CGRect) -> CGRect { |
| 84 | + if rect == target || target == CGRect.zero { |
| 85 | + return rect |
| 86 | + } |
| 87 | + |
| 88 | + var scales = CGSize.zero |
| 89 | + scales.width = abs(target.width / rect.width) |
| 90 | + scales.height = abs(target.height / rect.height) |
| 91 | + |
| 92 | + switch self { |
| 93 | + case .aspectFit: |
| 94 | + scales.width = min(scales.width, scales.height) |
| 95 | + scales.height = scales.width |
| 96 | + case .aspectFill: |
| 97 | + scales.width = max(scales.width, scales.height) |
| 98 | + scales.height = scales.width |
| 99 | + case .stretch: |
| 100 | + break |
| 101 | + case .center: |
| 102 | + scales.width = 1 |
| 103 | + scales.height = 1 |
| 104 | + } |
| 105 | + |
| 106 | + var result = rect.standardized |
| 107 | + result.size.width *= scales.width |
| 108 | + result.size.height *= scales.height |
| 109 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 110 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 111 | + return result |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments