wake-ios/wake/View/Blind/Card.swift
2025-09-01 19:42:32 +08:00

84 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
struct CustomLightSequenceAnimation: View {
// "123321123321"
private let baseSequence: [Int] = [1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1]
@State private var currentLight: Int = 1 //
@State private var sequenceIndex: Int = 0 //
//
@State private var currentOpacity: CGFloat = 1.0
@State private var nextOpacity: CGFloat = 0.0
//
private let screenWidth = UIScreen.main.bounds.width
private let squareSize: CGFloat
private let imageSize: CGFloat
init() {
self.squareSize = screenWidth * 1.8 //
self.imageSize = squareSize / 3 // 1/3
}
//
private var centerPosition: CGPoint {
CGPoint(x: screenWidth / 2, y: squareSize * 0.325)
}
var body: some View {
ZStack {
//
SVGImage(svgName: "BlindBg")
.frame(width: squareSize, height: squareSize)
.position(centerPosition)
//
SVGImage(svgName: "Light\(currentLight)")
.frame(width: imageSize, height: imageSize)
.position(centerPosition)
.opacity(currentOpacity)
//
SVGImage(svgName: "Light\(nextLight)")
.frame(width: imageSize, height: imageSize)
.position(centerPosition)
.opacity(nextOpacity)
}
.onAppear {
startLoopAnimation()
}
}
//
private var nextLight: Int {
let nextIdx = (sequenceIndex + 1) % baseSequence.count
return baseSequence[nextIdx]
}
//
private func startLoopAnimation() {
// 1.2
Timer.scheduledTimer(withTimeInterval: 1.2, repeats: true) { _ in
// 0.5
withAnimation(Animation.easeInOut(duration: 0.5)) {
currentOpacity = 0.0
nextOpacity = 1.0
}
//
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
currentLight = nextLight
sequenceIndex = (sequenceIndex + 1) % baseSequence.count
currentOpacity = 1.0
nextOpacity = 0.0
}
}
}
}
//
struct CustomLightSequenceAnimation_Previews: PreviewProvider {
static var previews: some View {
CustomLightSequenceAnimation()
}
}