|
| 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 SPAlertIconFlagView: 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 | + ShuffleDraw.draw(frame: rect, resizing: .aspectFit, fillColor: self.tintColor) |
| 38 | + } |
| 39 | + |
| 40 | + class ShuffleDraw: 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: 51, height: 55), target: targetFrame) |
| 46 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 47 | + context.scaleBy(x: resizedFrame.width / 51, y: resizedFrame.height / 55) |
| 48 | + let bezierPath = UIBezierPath() |
| 49 | + bezierPath.move(to: CGPoint(x: 3.94, y: 53)) |
| 50 | + bezierPath.addCurve(to: CGPoint(x: 2, y: 51.09), controlPoint1: CGPoint(x: 2.89, y: 53), controlPoint2: CGPoint(x: 2, y: 52.13)) |
| 51 | + bezierPath.addLine(to: CGPoint(x: 2, y: 8.73)) |
| 52 | + bezierPath.addCurve(to: CGPoint(x: 4.91, y: 4.3), controlPoint1: CGPoint(x: 2, y: 6.41), controlPoint2: CGPoint(x: 3.15, y: 5.09)) |
| 53 | + bezierPath.addLine(to: CGPoint(x: 5.25, y: 4.15)) |
| 54 | + bezierPath.addCurve(to: CGPoint(x: 12.32, y: 3), controlPoint1: CGPoint(x: 7.03, y: 3.38), controlPoint2: CGPoint(x: 8.59, y: 3)) |
| 55 | + bezierPath.addCurve(to: CGPoint(x: 36.63, y: 7.61), controlPoint1: CGPoint(x: 21.78, y: 3), controlPoint2: CGPoint(x: 27.66, y: 7.61)) |
| 56 | + bezierPath.addCurve(to: CGPoint(x: 44.61, y: 6.54), controlPoint1: CGPoint(x: 41.02, y: 7.61), controlPoint2: CGPoint(x: 43.36, y: 6.54)) |
| 57 | + bezierPath.addCurve(to: CGPoint(x: 47.26, y: 8.71), controlPoint1: CGPoint(x: 46.09, y: 6.54), controlPoint2: CGPoint(x: 47.26, y: 7.33)) |
| 58 | + bezierPath.addLine(to: CGPoint(x: 47.26, y: 34.57)) |
| 59 | + bezierPath.addCurve(to: CGPoint(x: 44.35, y: 39.03), controlPoint1: CGPoint(x: 47.26, y: 36.92), controlPoint2: CGPoint(x: 46.11, y: 38.24)) |
| 60 | + bezierPath.addLine(to: CGPoint(x: 44.01, y: 39.19)) |
| 61 | + bezierPath.addCurve(to: CGPoint(x: 36.96, y: 40.33), controlPoint1: CGPoint(x: 42.23, y: 39.95), controlPoint2: CGPoint(x: 40.7, y: 40.33)) |
| 62 | + bezierPath.addCurve(to: CGPoint(x: 12.65, y: 35.72), controlPoint1: CGPoint(x: 27.99, y: 40.33), controlPoint2: CGPoint(x: 22.08, y: 35.72)) |
| 63 | + bezierPath.addCurve(to: CGPoint(x: 5.87, y: 36.92), controlPoint1: CGPoint(x: 8.91, y: 35.72), controlPoint2: CGPoint(x: 6.51, y: 36.64)) |
| 64 | + bezierPath.addLine(to: CGPoint(x: 5.87, y: 51.09)) |
| 65 | + bezierPath.addCurve(to: CGPoint(x: 3.94, y: 53), controlPoint1: CGPoint(x: 5.87, y: 52.16), controlPoint2: CGPoint(x: 5.01, y: 53)) |
| 66 | + bezierPath.close() |
| 67 | + fillColor.setFill() |
| 68 | + bezierPath.fill() |
| 69 | + context.restoreGState() |
| 70 | + } |
| 71 | + |
| 72 | + @objc(StyleKitNameResizingBehavior) |
| 73 | + public enum ResizingBehavior: Int { |
| 74 | + |
| 75 | + case aspectFit |
| 76 | + case aspectFill |
| 77 | + case stretch |
| 78 | + case center |
| 79 | + |
| 80 | + public func apply(rect: CGRect, target: CGRect) -> CGRect { |
| 81 | + if rect == target || target == CGRect.zero { |
| 82 | + return rect |
| 83 | + } |
| 84 | + |
| 85 | + var scales = CGSize.zero |
| 86 | + scales.width = abs(target.width / rect.width) |
| 87 | + scales.height = abs(target.height / rect.height) |
| 88 | + |
| 89 | + switch self { |
| 90 | + case .aspectFit: |
| 91 | + scales.width = min(scales.width, scales.height) |
| 92 | + scales.height = scales.width |
| 93 | + case .aspectFill: |
| 94 | + scales.width = max(scales.width, scales.height) |
| 95 | + scales.height = scales.width |
| 96 | + case .stretch: |
| 97 | + break |
| 98 | + case .center: |
| 99 | + scales.width = 1 |
| 100 | + scales.height = 1 |
| 101 | + } |
| 102 | + |
| 103 | + var result = rect.standardized |
| 104 | + result.size.width *= scales.width |
| 105 | + result.size.height *= scales.height |
| 106 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 107 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 108 | + return result |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments