|
| 1 | +// |
| 2 | +// SpinerAnimationType.swift |
| 3 | +// TransitionButton |
| 4 | +// |
| 5 | +// Created by Rahul Mayani on 11/09/20. |
| 6 | +// Copyright © 2020 ITechnoDev. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import UIKit |
| 11 | + |
| 12 | +// swiftlint:disable:next class_delegate_protocol |
| 13 | +protocol TransitionButtonAnimationDelegate { |
| 14 | + |
| 15 | + func setupSpinnerAnimation(in layer: CAShapeLayer, frame: CGRect, color: UIColor, spinnerSize: UInt?) |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | +Enum of animation types used for spiner animation. |
| 20 | + |
| 21 | +- DefaultSpinner: LineSpinFadeLoader animation. |
| 22 | +- BallRotate: BallRotate animation. |
| 23 | +- BallPulse: BallPulse animation. |
| 24 | +- AudioEqualizer: AudioEqualizer animation. |
| 25 | +- BallClipRotate: BallClipRotate animation. |
| 26 | +- BallScale: BallScale animation. |
| 27 | +*/ |
| 28 | +public enum SpinerAnimationType: Int, CaseIterable { |
| 29 | + |
| 30 | + /** |
| 31 | + DefaultSpinner. |
| 32 | + |
| 33 | + - returns: Instance of DefaultSpinner. |
| 34 | + */ |
| 35 | + case defaultSpinner = 0 |
| 36 | + /** |
| 37 | + BallRotate. |
| 38 | + |
| 39 | + - returns: Instance of SpinerBallRotate. |
| 40 | + */ |
| 41 | + case ballRotate = 1 |
| 42 | + /** |
| 43 | + BallPulse. |
| 44 | + |
| 45 | + - returns: Instance of SpinerBallPulse. |
| 46 | + */ |
| 47 | + case ballPulse = 2 |
| 48 | + /** |
| 49 | + AudioEqualizer. |
| 50 | + |
| 51 | + - returns: Instance of SpinerAudioEqualizer. |
| 52 | + */ |
| 53 | + case audioEqualizer = 3 |
| 54 | + /** |
| 55 | + BallClipRotate. |
| 56 | + |
| 57 | + - returns: Instance of SpinerBallClipRotate. |
| 58 | + */ |
| 59 | + case ballClipRotate = 4 |
| 60 | + /** |
| 61 | + BallScale. |
| 62 | + |
| 63 | + - returns: Instance of SpinerBallScale. |
| 64 | + */ |
| 65 | + case ballScale = 5 |
| 66 | + |
| 67 | + |
| 68 | + // swiftlint:disable:next cyclomatic_complexity function_body_length |
| 69 | + func animation() -> TransitionButtonAnimationDelegate { |
| 70 | + switch self { |
| 71 | + case .defaultSpinner: |
| 72 | + return DefaultSpinner() |
| 73 | + case .ballRotate: |
| 74 | + return SpinerBallRotate() |
| 75 | + case .ballPulse: |
| 76 | + return SpinerBallPulse() |
| 77 | + case .audioEqualizer: |
| 78 | + return SpinerAudioEqualizer() |
| 79 | + case .ballClipRotate: |
| 80 | + return SpinerBallClipRotate() |
| 81 | + case .ballScale: |
| 82 | + return SpinerBallScale() |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +enum TransitionButtonAnimationShape { |
| 88 | + case circle |
| 89 | + case ringTwoHalfVertical |
| 90 | + case ringTwoHalfHorizontal |
| 91 | + case line |
| 92 | + |
| 93 | + // swiftlint:disable:next cyclomatic_complexity function_body_length |
| 94 | + func layerWith(size: CGSize, color: UIColor) -> CALayer { |
| 95 | + let layer: CAShapeLayer = CAShapeLayer() |
| 96 | + var path: UIBezierPath = UIBezierPath() |
| 97 | + let lineWidth: CGFloat = 2 |
| 98 | + |
| 99 | + switch self { |
| 100 | + case .circle: |
| 101 | + path.addArc(withCenter: CGPoint(x: size.width / 2, y: size.height / 2), |
| 102 | + radius: size.width / 2, |
| 103 | + startAngle: 0, |
| 104 | + endAngle: CGFloat(2 * Double.pi), |
| 105 | + clockwise: false) |
| 106 | + layer.fillColor = color.cgColor |
| 107 | + case .ringTwoHalfVertical: |
| 108 | + path.addArc(withCenter: CGPoint(x: size.width / 2, y: size.height / 2), |
| 109 | + radius: size.width / 2, |
| 110 | + startAngle: CGFloat(-3 * Double.pi / 4), |
| 111 | + endAngle: CGFloat(-Double.pi / 4), |
| 112 | + clockwise: true) |
| 113 | + path.move( |
| 114 | + to: CGPoint(x: size.width / 2 - size.width / 2 * cos(CGFloat(Double.pi / 4)), |
| 115 | + y: size.height / 2 + size.height / 2 * sin(CGFloat(Double.pi / 4))) |
| 116 | + ) |
| 117 | + path.addArc(withCenter: CGPoint(x: size.width / 2, y: size.height / 2), |
| 118 | + radius: size.width / 2, |
| 119 | + startAngle: CGFloat(-5 * Double.pi / 4), |
| 120 | + endAngle: CGFloat(-7 * Double.pi / 4), |
| 121 | + clockwise: false) |
| 122 | + layer.fillColor = nil |
| 123 | + layer.strokeColor = color.cgColor |
| 124 | + layer.lineWidth = lineWidth |
| 125 | + case .ringTwoHalfHorizontal: |
| 126 | + path.addArc(withCenter: CGPoint(x: size.width / 2, y: size.height / 2), |
| 127 | + radius: size.width / 2, |
| 128 | + startAngle: CGFloat(3 * Double.pi / 4), |
| 129 | + endAngle: CGFloat(5 * Double.pi / 4), |
| 130 | + clockwise: true) |
| 131 | + path.move( |
| 132 | + to: CGPoint(x: size.width / 2 + size.width / 2 * cos(CGFloat(Double.pi / 4)), |
| 133 | + y: size.height / 2 - size.height / 2 * sin(CGFloat(Double.pi / 4))) |
| 134 | + ) |
| 135 | + path.addArc(withCenter: CGPoint(x: size.width / 2, y: size.height / 2), |
| 136 | + radius: size.width / 2, |
| 137 | + startAngle: CGFloat(-Double.pi / 4), |
| 138 | + endAngle: CGFloat(Double.pi / 4), |
| 139 | + clockwise: true) |
| 140 | + layer.fillColor = nil |
| 141 | + layer.strokeColor = color.cgColor |
| 142 | + layer.lineWidth = lineWidth |
| 143 | + case .line: |
| 144 | + path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), |
| 145 | + cornerRadius: size.width / 2) |
| 146 | + layer.fillColor = color.cgColor |
| 147 | + } |
| 148 | + |
| 149 | + layer.backgroundColor = nil |
| 150 | + layer.path = path.cgPath |
| 151 | + layer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) |
| 152 | + |
| 153 | + return layer |
| 154 | + } |
| 155 | +} |
0 commit comments