-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ce5968
commit 54585e5
Showing
3 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
SSSwiftUIAnimations/SSSwiftUIAnimations/LoadingAnimation/ProgressBar/SwiftUIView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// | ||
// SwiftUIView.swift | ||
// SSSwiftUIAnimations | ||
// | ||
// Created by Brijesh Barasiya on 28/02/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct SwiftUIView: View { | ||
@Binding private var percentage: Float | ||
@State private var sticks: [Stick] | ||
@State private var lastStickIndex: Int = 0 | ||
private let circleRadius: CGFloat | ||
private let stickWidth: CGFloat | ||
private let filledColor: Color | ||
private let unFilledColor: Color | ||
private let duration: Double | ||
private let progressColor: Color | ||
|
||
init( | ||
percentage: Binding<Float>, | ||
size: Float, | ||
stickWidth: Float, | ||
progressColor: Color, | ||
filledColor: Color, | ||
unFilledColor: Color, | ||
duration: Double | ||
) { | ||
let screenBounds = UIScreen.main.bounds | ||
let screenWidth = Float(screenBounds.width) | ||
let screenHeight = Float(screenBounds.height) | ||
|
||
// Adjust stick size, ensuring it doesn't exceed the screen size - 50 | ||
let adjustedSize: Float = min(max(size, 50), min(screenWidth, screenHeight) - 50) | ||
|
||
// Adjust stick width, ensuring a minimum value of 9% of size. | ||
let adjustedStickWidth = min(max((adjustedSize / 100) * 5, stickWidth), (adjustedSize / 100) * 20) | ||
|
||
let circumference = 2 * .pi * adjustedSize | ||
let spacing = circumference / adjustedStickWidth | ||
let totalStickCount: Int = Int((spacing * 25) / 100) | ||
self.sticks = Array(repeating: Stick(xAxis: 0, color: unFilledColor), count: totalStickCount) | ||
self.circleRadius = CGFloat(adjustedSize/2) | ||
self.stickWidth = CGFloat(adjustedStickWidth) | ||
self.progressColor = progressColor | ||
self.filledColor = filledColor | ||
self.unFilledColor = unFilledColor | ||
self.duration = duration / Double(totalStickCount) | ||
self._percentage = percentage | ||
} | ||
|
||
var body: some View { | ||
Circle() | ||
.frame(width: circleRadius * 2) | ||
.foregroundColor(Color.clear) | ||
.overlay { | ||
ForEach(0..<sticks.count, id: \.self) { index in | ||
Rectangle() | ||
.frame(width: stickWidth, height: (stickWidth * 3)) | ||
.foregroundColor(sticks[index].color) | ||
.offset(y: (circleRadius - stickWidth)) | ||
.rotationEffect( | ||
.degrees(Double((CGFloat(index) + sticks[index].xAxis) * 360) / Double(sticks.count)) | ||
) | ||
} | ||
} | ||
.onAppear { | ||
animateStickView(index: 0, isReversing: false) | ||
} | ||
} | ||
|
||
private func animateStickView(index: Int, isReversing: Bool) { | ||
if #available(iOS 17.0, *) { | ||
withAnimation(Animation.linear(duration: duration)) { | ||
updateStickViewProperties(index: index, isReversing: isReversing) | ||
} completion: { | ||
resertStickViewAnimation(index: index, isReversing: isReversing) | ||
} | ||
} else { | ||
withAnimation(Animation.linear(duration: duration)) { | ||
updateStickViewProperties(index: index, isReversing: isReversing) | ||
} | ||
DispatchQueue.main.asyncAfter(deadline: .now() + duration) { | ||
resertStickViewAnimation(index: index, isReversing: isReversing) | ||
} | ||
} | ||
} | ||
|
||
private func updateStickViewProperties(index: Int, isReversing: Bool) { | ||
sticks[index].xAxis = isReversing ? -0.6 : 0.6 | ||
sticks[index].color = getStickColor(forIndex: index, isReversing: isReversing) | ||
let validatedPercentage = min(max(0, percentage), 100) | ||
let sticksAccordingToPercentage = Double(sticks.count) * Double(validatedPercentage / 100) | ||
let numberOfSticksToChange = max(Int(sticksAccordingToPercentage), 0) | ||
for stickIndex in 0..<min(numberOfSticksToChange, index) { | ||
sticks[stickIndex].color = progressColor | ||
} | ||
let finalIndex = index >= numberOfSticksToChange ? numberOfSticksToChange : lastStickIndex | ||
lastStickIndex = finalIndex | ||
} | ||
|
||
private func resertStickViewAnimation(index: Int, isReversing: Bool) { | ||
withAnimation(Animation.linear(duration: duration)) { | ||
sticks[index].xAxis = 0 | ||
} | ||
let nextIndex = isReversing ? index - 1 : index + 1 | ||
if (index == (lastStickIndex) && isReversing) || (index == sticks.indices.last && !isReversing) { | ||
animateStickView(index: isReversing ? nextIndex + 1 : nextIndex - 1, isReversing: !isReversing) | ||
} else { | ||
animateStickView(index: nextIndex ,isReversing: isReversing) | ||
} | ||
} | ||
|
||
private func getStickColor(forIndex index: Int, isReversing: Bool) -> Color { | ||
return if (index == lastStickIndex) { | ||
filledColor | ||
} else if (index == sticks.indices.last) { | ||
unFilledColor | ||
} else { | ||
(isReversing ? unFilledColor : filledColor) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
SwiftUIView( | ||
percentage: .constant(76), | ||
size: 100, | ||
stickWidth: 3, | ||
progressColor: .green, | ||
filledColor: .blue, | ||
unFilledColor: .gray, | ||
duration: 5 | ||
) | ||
} |