-
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.
Linear & Circular Loading Animations
- Loading branch information
1 parent
3c9f828
commit 1ce5968
Showing
9 changed files
with
708 additions
and
0 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
107 changes: 107 additions & 0 deletions
107
SSSwiftUIAnimations/SSSwiftUIAnimations/LoadingAnimation/Loading/CircularLoading.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,107 @@ | ||
// | ||
// CircularLoading.swift | ||
// SSSwiftUIAnimations | ||
// | ||
// Created by Brijesh Barasiya on 29/01/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct CircularLoading: View { | ||
@State private var sticks: [Stick] | ||
private let circleRadius: CGFloat | ||
private let stickWidth: CGFloat | ||
private let filledColor: Color | ||
private let unFilledColor: Color | ||
private let duration: Double | ||
|
||
init( | ||
size: Float, | ||
stickWidth: Float, | ||
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) * 9, 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.filledColor = filledColor | ||
self.unFilledColor = unFilledColor | ||
self.duration = duration / Double(totalStickCount) | ||
} | ||
|
||
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 * 2)) | ||
.foregroundColor(sticks[index].color) | ||
.offset(y: (circleRadius - stickWidth)) | ||
.rotationEffect( | ||
.degrees(Double((CGFloat(index) + sticks[index].xAxis) * 360) / Double(sticks.count)) | ||
) | ||
} | ||
} | ||
.onAppear { | ||
animateStickView(index: 0, color: filledColor) | ||
} | ||
} | ||
|
||
private func animateStickView(index: Int, color: Color) { | ||
if #available(iOS 17.0, *) { | ||
withAnimation(Animation.linear(duration: duration)) { | ||
updateStickViewProperties(index: index, color: color) | ||
} completion: { | ||
resertStickViewAnimation(index: index, color: color) | ||
} | ||
} else { | ||
withAnimation(Animation.linear(duration: duration)) { | ||
updateStickViewProperties(index: index, color: color) | ||
} | ||
DispatchQueue.main.asyncAfter(deadline: .now() + duration) { | ||
resertStickViewAnimation(index: index, color: color) | ||
} | ||
} | ||
} | ||
|
||
private func updateStickViewProperties(index: Int, color: Color) { | ||
sticks[index].xAxis = 0.6 | ||
sticks[index].color = color | ||
} | ||
|
||
private func resertStickViewAnimation(index: Int, color: Color) { | ||
withAnimation(Animation.linear(duration: duration * 12)) { | ||
sticks[index].xAxis = 0 | ||
} | ||
if (index == sticks.indices.last) { | ||
let newColor = switch color { | ||
case unFilledColor: filledColor | ||
case filledColor : unFilledColor | ||
default : filledColor | ||
} | ||
animateStickView(index: 0, color: newColor) | ||
} else { | ||
animateStickView(index: index + 1, color: color) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
LoadingView(loaderType: .circularLoading()) | ||
} |
119 changes: 119 additions & 0 deletions
119
SSSwiftUIAnimations/SSSwiftUIAnimations/LoadingAnimation/Loading/LinearLoading.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,119 @@ | ||
// | ||
// LinearLoading.swift | ||
// SSSwiftUIAnimations | ||
// | ||
// Created by Brijesh Barasiya on 29/01/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct LinearLoading: View { | ||
@State private var sticks: [Stick] | ||
private let spacing: CGFloat | ||
private let stickWidth: CGFloat | ||
private let stickHeight: CGFloat | ||
private let filledColor: Color | ||
private let unFilledColor: Color | ||
private let perStickDuration: Double | ||
private let allowStickHeightAnimation: Bool | ||
|
||
init( | ||
stickCount: Int, | ||
spacing: Float, | ||
stickWidth: Float, | ||
stickHeight: Float, | ||
filledColor: Color, | ||
unFilledColor: Color, | ||
duration: Double, | ||
allowStickHeightAnimation: Bool | ||
) { | ||
let screenBounds = UIScreen.main.bounds | ||
let screenWidth = Float(screenBounds.width) | ||
let screenHeight = Float(screenBounds.height) | ||
|
||
// Adjust stick width, ensuring a minimum value of 10 | ||
let adjustedStickWidth = max(stickWidth, 10) | ||
|
||
// Adjust spacing, ensuring a space is not lowerthan 0. | ||
let adjustedSpacing = max(spacing, 0) | ||
|
||
// Adjust stick height, ensuring it doesn't exceed the screen height - 50 | ||
let adjustedStickHeight = min(max(stickHeight, adjustedSpacing + 10), screenHeight - 50) | ||
|
||
// Calculate the total number of sticks, considering screen width and spacing | ||
let totalStickCount = min(Int(screenWidth / (adjustedStickWidth + adjustedSpacing)) - 1, max(stickCount, 3)) | ||
|
||
// Calculate the animation duration per stick | ||
self.perStickDuration = max(duration / Double(totalStickCount), 1 / Double(totalStickCount)) | ||
|
||
self.sticks = Array(repeating: Stick(xAxis: 0, color: unFilledColor), count: totalStickCount) | ||
self.spacing = CGFloat(adjustedSpacing) | ||
self.stickWidth = CGFloat(adjustedStickWidth) | ||
self.stickHeight = CGFloat(adjustedStickHeight) | ||
self.filledColor = filledColor | ||
self.unFilledColor = unFilledColor | ||
self.allowStickHeightAnimation = allowStickHeightAnimation | ||
} | ||
|
||
var body: some View { | ||
HStack(spacing: spacing) { | ||
ForEach(sticks.indices, id: \.self) { index in | ||
Rectangle() | ||
.foregroundColor(sticks[index].color) | ||
.frame(width: stickWidth, height: allowStickHeightAnimation ? abs(sticks[index].xAxis) + (stickHeight - spacing) : stickHeight) | ||
.offset(x: sticks[index].xAxis) | ||
} | ||
} | ||
.frame(height: stickHeight) | ||
.onAppear { | ||
sticks[0].color = filledColor | ||
animateStickView(index: 0, isReversing: false) | ||
} | ||
} | ||
|
||
private func animateStickView(index: Int, isReversing: Bool) { | ||
if #available(iOS 17.0, *) { | ||
withAnimation(Animation.linear(duration: perStickDuration)) { | ||
updateStickViewProperties(index: index, isReversing: isReversing) | ||
} completion: { | ||
resertStickViewAnimation(index: index, isReversing: isReversing) | ||
} | ||
} else { | ||
withAnimation(Animation.linear(duration: perStickDuration)) { | ||
updateStickViewProperties(index: index, isReversing: isReversing) | ||
} | ||
DispatchQueue.main.asyncAfter(deadline: .now() + perStickDuration) { | ||
resertStickViewAnimation(index: index, isReversing: isReversing) | ||
} | ||
} | ||
} | ||
|
||
private func updateStickViewProperties(index: Int, isReversing: Bool) { | ||
sticks[index].xAxis = isReversing ? (spacing * -1) : spacing | ||
sticks[index].color = getStickColor(forIndex: index, isReversing: isReversing) | ||
} | ||
|
||
private func getStickColor(forIndex index: Int, isReversing: Bool) -> Color { | ||
return switch (index) { | ||
case 0 : filledColor | ||
case sticks.indices.last : unFilledColor | ||
default: isReversing ? unFilledColor : filledColor | ||
} | ||
} | ||
|
||
private func resertStickViewAnimation(index: Int, isReversing: Bool) { | ||
withAnimation(Animation.linear(duration: perStickDuration)) { | ||
sticks[index].xAxis = 0 | ||
} | ||
let nextIndex = isReversing ? index - 1 : index + 1 | ||
if (index == 0 && isReversing) || (index == sticks.indices.last && !isReversing) { | ||
animateStickView(index: isReversing ? nextIndex + 1 : nextIndex - 1, isReversing: !isReversing) | ||
} else { | ||
animateStickView(index: nextIndex, isReversing: isReversing) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
LoadingView(loaderType: .linearLoading()) | ||
} |
Oops, something went wrong.