TrendShape.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import SwiftUI
  2. struct Triangle: Shape {
  3. /// Creates a triangle shape pointing to the right
  4. func path(in rect: CGRect) -> Path {
  5. var path = Path()
  6. // Draw the triangle pointing to the right
  7. path.move(to: CGPoint(x: rect.maxX - 10, y: rect.midY))
  8. path.addLine(to: CGPoint(x: rect.minX, y: rect.minY))
  9. path.addQuadCurve(
  10. to: CGPoint(x: rect.minX, y: rect.maxY),
  11. control: CGPoint(x: rect.midX - 15, y: rect.midY)
  12. )
  13. path.closeSubpath()
  14. return path
  15. }
  16. }
  17. /// A view that displays a circular trend indicator with a directional triangle
  18. struct TrendShape: View {
  19. /// Rotation angle in degrees for the trend direction
  20. let rotationDegrees: Double
  21. // Angular gradient for the outer circle, transitioning through various blues and purples
  22. private let angularGradient = AngularGradient(
  23. colors: [
  24. Color(red: 0.7215686275, green: 0.3411764706, blue: 1), // #B857FF
  25. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569), // #9F6CFA
  26. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765), // #7C8BF3
  27. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961), // #57AAEC
  28. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902), // #43BBE9
  29. Color(red: 0.7215686275, green: 0.3411764706, blue: 1) // #B857FF (repeated for seamless transition)
  30. ],
  31. center: .center,
  32. startAngle: .degrees(270),
  33. endAngle: .degrees(-90)
  34. )
  35. // Color for the direction indicator triangle
  36. private let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902) // #43BBE9
  37. var body: some View {
  38. ZStack {
  39. // Outer circle with gradient
  40. Circle()
  41. .stroke(angularGradient, lineWidth: 6)
  42. .frame(width: 90, height: 90)
  43. .background(Circle().fill(Color.black))
  44. // Triangle with the color of the last gradient color
  45. Triangle()
  46. .fill(triangleColor)
  47. .frame(width: 20, height: 20)
  48. .offset(x: 55)
  49. }
  50. .rotationEffect(.degrees(rotationDegrees))
  51. .shadow(color: Color.black.opacity(0.33), radius: 3)
  52. }
  53. }