Skip to content

Commit

Permalink
Merge pull request #3 from JoshHrach/DurationProperty
Browse files Browse the repository at this point in the history
Added property to change rotation period
  • Loading branch information
JoshHrach authored Jun 2, 2020
2 parents 48570b5 + 82e1f8f commit 9656adc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
14 changes: 12 additions & 2 deletions Sources/ActivityIndicatorView/ActivityIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import SwiftUI
A view that shows that a task is in progress.
*/
public struct ActivityIndicatorView: View {
/// Background color behind the indicator. This appears in a circle behind the activity view.
private var backgroundColor: Color
/// The color of the activity indicator. This is in a gradient that fades to `.white`.
private var foregroundColor: Color

/// Total number of dashes visible in the indicator.
private var totalDashes = 12

/// Time to complete a single revolution. In seconds.
private(set) var period: Double

/// Radial offset. State property used to animate the view.
@State private var radialOffset: Double = 0

private var dashViews: some View {
Expand All @@ -29,20 +36,23 @@ public struct ActivityIndicatorView: View {
- parameter backgroundColor: The background color behind the indicator. This will appear in a circle behind the view. If none is set, defaults to `.clear`.
- parameter foregroundColor: The color of the activity indicator. If none is set, defaults to `.gray`.
- parameter period: How long it takes for the view to complete one complete rotation. In seconds. Defaults to `1`.
*/
public init(backgroundColor: Color = .clear, foregroundColor: Color = .gray) {
public init(backgroundColor: Color = .clear, foregroundColor: Color = .gray, period: Double = 1) {
self.backgroundColor = backgroundColor
self.foregroundColor = foregroundColor
self.period = period
}


public var body: some View {
ZStack {
Circle()
.fill(backgroundColor)

AngularGradient(gradient: .init(colors: [.white, foregroundColor]), center: .center)
.rotationEffect(.degrees(radialOffset))
.animation(Animation.linear(duration: 1).repeatForever(autoreverses: false))
.animation(Animation.linear(duration: period).repeatForever(autoreverses: false))
.mask(self.dashViews)
}
.scaledToFit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import XCTest
@testable import ActivityIndicatorView

final class ActivityIndicatorViewTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(ActivityIndicatorView().text, "Hello, World!")
func testDurationSetting() {
let duration: Double = 2
let view = ActivityIndicatorView(period: duration)

XCTAssert(view.period == duration)
}

static var allTests = [
("testExample", testExample),
]
}

0 comments on commit 9656adc

Please sign in to comment.