Reputation: 1529
In my application implementing custom activity indicator, by default apple watch has activity indicator that i don't want to show how can i achive? Because default activity indicator coming exactly in center of the screen but custom activity indicator is coming little bit down. first one is Default and second custom image.
Is there a way to give custom frames or hide the default activity indicator?
!
Upvotes: 2
Views: 472
Reputation: 3561
SwiftUI
import SwiftUI
struct ActivityIndicatorView: View {
// MARK: - Value
// MARK: Public
@Binding var isAnimating: Bool
// MARK: Private
private let radius: CGFloat = 10.0
private let count = 6
private let interval: TimeInterval = 0.15
private let minimumOpacity: Double = 0.25
@State private var opacity: Double = 0
private let point = { (index: Int, count: Int, radius: CGFloat, frame: CGRect) -> CGPoint in
let angle = 2.0 * .pi / Double(count) * Double(index)
let circleX = radius * cos(CGFloat(angle))
let circleY = radius * sin(CGFloat(angle))
return CGPoint(x: circleX + frame.midX, y: circleY + frame.midY)
}
private let timer = Timer.publish(every: 6 * 0.15, on: .main, in: .common).autoconnect() // every = count * interval
// MARK: - View
var body: some View {
GeometryReader { geometry in
ForEach(0..<self.count) { index in
Circle()
.fill(Color.white)
.frame(width: 9.0, height: 9.0)
.animation(nil)
.opacity(self.opacity <= 0 ? 1.0 * Double(index == 0 ? 6 : index) / Double(self.count) : self.opacity)
.position(self.point(index, self.count, self.radius, geometry.frame(in: .local)))
.animation(
Animation.easeOut(duration: self.minimumOpacity < self.opacity ? 0.3 : 0.4)
.repeatCount(1, autoreverses: true)
.delay(TimeInterval(index) * self.interval)
)
}
.onReceive(self.timer) { output in
self.update()
}
}
.rotationEffect(.degrees(30))
.opacity(isAnimating == false ? 0 : 1.0)
.onAppear {
self.update()
}
}
// MARK: - Function
// MARK: Private
private func update() {
opacity = minimumOpacity < opacity ? minimumOpacity : 1.0
DispatchQueue.main.asyncAfter(deadline: .now() + 0.38) {
self.opacity = self.minimumOpacity
}
}
}
#if DEBUG
struct ActivityIndicatorView_Previews: PreviewProvider {
static var previews: some View {
let view = ActivityIndicatorView(isAnimating: .constant(true))
return Group {
view
.previewDevice("Apple Watch Series 5 - 44mm")
view
.previewDevice("Apple Watch Series 4 - 40mm")
view
.previewDevice("Apple Watch Series 3 - 42mm")
view
.previewDevice("Apple Watch Series 3 - 38mm")
}
}
}
#endif
Upvotes: 0
Reputation: 5939
Unfortunately not. No such API exists. It's all controlled by the OS.
Upvotes: 2