|
| 1 | +// |
| 2 | +// Copyright (c) 2023 Related Code - https://relatedcode.com |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | + |
| 12 | +import UIKit |
| 13 | + |
| 14 | +// MARK: - Ball Vertical Bounce |
| 15 | +extension ProgressHUD { |
| 16 | + |
| 17 | + func animationBallVerticalBounce(_ view: UIView, _ color: UIColor) { |
| 18 | + let line = CAShapeLayer() |
| 19 | + line.strokeColor = color.cgColor |
| 20 | + line.lineWidth = view.frame.height / 15 |
| 21 | + line.lineCap = .round |
| 22 | + line.fillColor = UIColor.clear.cgColor |
| 23 | + view.layer.addSublayer(line) |
| 24 | + let speed = 0.07 |
| 25 | + |
| 26 | + let animationDownCurve = CAKeyframeAnimation(keyPath: "path") |
| 27 | + animationDownCurve.timingFunction = CAMediaTimingFunction(name: .easeOut) |
| 28 | + animationDownCurve.duration = 2.1 * speed |
| 29 | + animationDownCurve.values = [initialCurvePath(in: view).cgPath, downCurvePath(in: view).cgPath] |
| 30 | + animationDownCurve.autoreverses = true |
| 31 | + animationDownCurve.beginTime = 2.9 * speed |
| 32 | + |
| 33 | + let animationTopCurve = CAKeyframeAnimation(keyPath: "path") |
| 34 | + animationTopCurve.timingFunction = CAMediaTimingFunction(name: .easeOut) |
| 35 | + animationTopCurve.duration = 0.4 * speed |
| 36 | + animationTopCurve.values = [initialCurvePath(in: view).cgPath, topCurvePath(in: view).cgPath] |
| 37 | + animationTopCurve.autoreverses = true |
| 38 | + animationTopCurve.beginTime = 7.1 * speed |
| 39 | + |
| 40 | + let animationGroup = CAAnimationGroup() |
| 41 | + animationGroup.animations = [animationDownCurve, animationTopCurve] |
| 42 | + animationGroup.duration = 10 * speed |
| 43 | + animationGroup.repeatCount = .infinity |
| 44 | + |
| 45 | + line.add(animationGroup, forKey: "pathAnimation") |
| 46 | + line.path = initialCurvePath(in: view).cgPath |
| 47 | + |
| 48 | + createBallAnimation(in: view, with: color, speed: speed) |
| 49 | + } |
| 50 | + |
| 51 | + private func initialCurvePath(in view: UIView) -> UIBezierPath { |
| 52 | + let width = view.frame.size.width |
| 53 | + let height = view.frame.size.height + view.frame.size.height / 3 |
| 54 | + let path = UIBezierPath() |
| 55 | + path.move(to: CGPoint(x: 0, y: height / 2)) |
| 56 | + path.addQuadCurve(to: CGPoint(x: width, y: height / 2), controlPoint: CGPoint(x: width / 2, y: height / 2)) |
| 57 | + return path |
| 58 | + } |
| 59 | + |
| 60 | + private func downCurvePath(in view: UIView) -> UIBezierPath { |
| 61 | + let width = view.frame.size.width |
| 62 | + let height = view.frame.size.height + view.frame.size.height / 3 |
| 63 | + let path = UIBezierPath() |
| 64 | + path.move(to: CGPoint(x: 0, y: height / 2)) |
| 65 | + path.addQuadCurve(to: CGPoint(x: width, y: height / 2), controlPoint: CGPoint(x: width / 2, y: height / 1.3)) |
| 66 | + return path |
| 67 | + } |
| 68 | + |
| 69 | + private func topCurvePath(in view: UIView) -> UIBezierPath { |
| 70 | + let width = view.frame.size.width |
| 71 | + let height = view.frame.size.height + view.frame.size.height / 3 |
| 72 | + let path = UIBezierPath() |
| 73 | + path.move(to: CGPoint(x: 0, y: height / 2)) |
| 74 | + path.addQuadCurve(to: CGPoint(x: width, y: height / 2), controlPoint: CGPoint(x: width / 2, y: height / 2.3)) |
| 75 | + return path |
| 76 | + } |
| 77 | + |
| 78 | + private func createBallAnimation(in view: UIView, with color: UIColor, speed: Double) { |
| 79 | + let width = view.frame.size.width |
| 80 | + let height = view.frame.size.height |
| 81 | + let size = width / 4 |
| 82 | + let yPosition = height - height / 3 |
| 83 | + |
| 84 | + let circle = drawCircleWith(CGSize(width: size, height: size), color) |
| 85 | + circle.frame = CGRect(x: width / 2 - size / 2, y: height / 20, width: size, height: size) |
| 86 | + |
| 87 | + let animation = CABasicAnimation(keyPath: "transform.translation.y") |
| 88 | + animation.fromValue = 0 |
| 89 | + animation.toValue = yPosition - size / 2 |
| 90 | + animation.duration = 5.0 * speed |
| 91 | + animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) |
| 92 | + animation.autoreverses = true |
| 93 | + animation.repeatCount = .infinity |
| 94 | + |
| 95 | + circle.add(animation, forKey: "animation") |
| 96 | + view.layer.addSublayer(circle) |
| 97 | + } |
| 98 | + |
| 99 | + private func drawCircleWith(_ size: CGSize, _ color: UIColor) -> CALayer { |
| 100 | + let path = UIBezierPath() |
| 101 | + let radius = size.width / 4 |
| 102 | + let center = CGPoint(x: size.width / 2, y: size.height / 2) |
| 103 | + path.addArc(withCenter: center, radius: radius, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true) |
| 104 | + |
| 105 | + let layer = CAShapeLayer() |
| 106 | + layer.fillColor = nil |
| 107 | + layer.strokeColor = color.cgColor |
| 108 | + layer.lineWidth = size.width / 2 |
| 109 | + layer.backgroundColor = nil |
| 110 | + layer.path = path.cgPath |
| 111 | + |
| 112 | + return layer |
| 113 | + } |
| 114 | +} |
0 commit comments