Remove sparkle

This commit is contained in:
Thomas Ricouard 2024-10-29 18:03:44 +01:00
parent 1d11f78abc
commit e5755f1d83

View file

@ -8,14 +8,11 @@ extension ButtonStyle where Self == StatusActionButtonStyle {
} }
} }
/// A toggle button that slightly scales and sends particle on activation, /// A toggle button that slightly scales and changes its foreground color to `tintColor` when toggled on
/// changing its foreground color to `tintColor` when toggled on
struct StatusActionButtonStyle: ButtonStyle { struct StatusActionButtonStyle: ButtonStyle {
var isOn: Bool var isOn: Bool
var tintColor: Color? var tintColor: Color?
@State var sparklesCounter: Float = 0
func makeBody(configuration: Configuration) -> some View { func makeBody(configuration: Configuration) -> some View {
configuration.label configuration.label
.foregroundColor(isOn ? tintColor : Color(UIColor.secondaryLabel)) .foregroundColor(isOn ? tintColor : Color(UIColor.secondaryLabel))
@ -23,18 +20,6 @@ struct StatusActionButtonStyle: ButtonStyle {
.brightness(brightness(configuration: configuration)) .brightness(brightness(configuration: configuration))
.animation(configuration.isPressed ? nil : .default, value: isOn) .animation(configuration.isPressed ? nil : .default, value: isOn)
.scaleEffect(configuration.isPressed && !isOn ? 0.8 : 1.0) .scaleEffect(configuration.isPressed && !isOn ? 0.8 : 1.0)
.background {
if let tint = tintColor {
SparklesView(counter: sparklesCounter, tint: tint, size: 5, velocity: 30)
}
}
.onChange(of: configuration.isPressed) { _, newValue in
guard tintColor != nil, !newValue, !isOn else { return }
withAnimation(.spring(response: 1, dampingFraction: 1)) {
sparklesCounter += 1
}
}
} }
func brightness(configuration: Configuration) -> Double { func brightness(configuration: Configuration) -> Double {
@ -44,84 +29,4 @@ struct StatusActionButtonStyle: ButtonStyle {
default: 0 default: 0
} }
} }
@MainActor
struct SparklesView: View, @preconcurrency Animatable {
var counter: Float
var tint: Color
var size: CGFloat
var velocity: CGFloat
var animatableData: Float {
get { counter }
set { counter = newValue }
}
struct Cell: Identifiable {
var id: Int
var angle: CGFloat
var velocity: CGFloat
var scale: CGFloat
var alpha: CGFloat
}
@State var cells: [Cell] = []
var progress: CGFloat {
CGFloat(counter - counter.rounded(.down))
}
public var body: some View {
if progress > 0.0 {
ZStack(alignment: .center) {
ForEach(cells) { cell in
Circle()
.frame(width: size, height: size)
.foregroundColor(tint)
.opacity(cell.alpha - (progress * cell.alpha) / 3)
.scaleEffect(cell.scale - progress * cell.scale)
.offset(
x: cos(cell.angle) * progress * cell.velocity * velocity,
y: sin(cell.angle) * progress * cell.velocity * velocity
)
}
}
.onAppear {
cells = Self.generateCells()
}
.onChange(of: counter) { oldValue, newValue in
if floor(oldValue) != floor(newValue) {
cells = Self.generateCells()
}
}
}
}
static func generateCells() -> [Cell] {
let cellCount = 16
let velocityRange = 0.6...1.0
let scaleRange = 0.5...1.0
let alphaRange = 0.6...1.0
let spacing = 2 * .pi / (1.618 + Double(arc4random() % 200) / 10000)
let initialSpacing = deg2rad(Double(arc4random() % 360))
return (0..<cellCount).map { index in
Cell(
id: index,
angle: initialSpacing + spacing * Double(index),
velocity: random(within: velocityRange),
scale: random(within: scaleRange),
alpha: random(within: alphaRange)
)
}
}
static func random(within range: ClosedRange<Double>) -> Double {
Double(arc4random()) / 0xFFFF_FFFF * (range.upperBound - range.lowerBound) + range.lowerBound
}
static func deg2rad(_ number: Double) -> Double {
number * .pi / 180
}
}
} }