Files
ActivityIndicatorView/Source/Indicators/OpacityDotsIndicatorView.swift
2020-10-08 12:15:27 +07:00

57 lines
1.5 KiB
Swift

//
// OpacityDotsIndicatorView.swift
// ActivityIndicatorView
//
// Created by Daniil Manin on 10/7/20.
// Copyright © 2020 Exyte. All rights reserved.
//
import SwiftUI
struct OpacityDotsIndicatorView: View {
private let count: Int = 3
private let inset: Int = 4
var body: some View {
GeometryReader { geometry in
ForEach(0..<self.count) { index in
OpacityDotsIndicatorItemView(index: index, count: self.count, inset: self.inset, size: geometry.size)
}.frame(width: geometry.size.width, height: geometry.size.height)
}
}
}
struct OpacityDotsIndicatorItemView: View {
let index: Int
let count: Int
let inset: Int
let size: CGSize
@State private var scale: CGFloat = 0
@State private var opacity: Double = 0
var body: some View {
let itemSize = (size.width - CGFloat(inset) * CGFloat(count - 1)) / CGFloat(count)
let animation = Animation.easeOut
.repeatForever(autoreverses: true)
.delay(index % 2 == 0 ? 0.2 : 0)
return Circle()
.frame(width: itemSize, height: itemSize)
.scaleEffect(scale)
.opacity(opacity)
.onAppear {
self.scale = 1
self.opacity = 1
withAnimation(animation) {
self.scale = 0.9
self.opacity = 0.3
}
}
.offset(x: (itemSize + CGFloat(inset)) * CGFloat(index) - size.width / 2 + itemSize / 2)
}
}