我做了以下內容PulsatingView:
struct PulsatingView: View {
@State private var opacity = 0.7
@State private var scale: CGFloat = 0.5
var body: some View {
VStack {
ZStack {
Circle()
.fill(Color.black.opacity(opacity))
.animation(.easeInOut(duration: 2).delay(1.5).repeatForever(autoreverses: false))
.frame(width: 7, height: 7)
.scaleEffect(scale)
.animation(.easeIn(duration: 2.5).delay(1).repeatForever(autoreverses: false))
Circle()
.fill(Color.black)
.frame(width: 7, height: 7)
}
}
.onAppear {
opacity = 0
scale = 5
}
}
}
它產生以下結果:(點擊查看影片;它是一個 .gif)

但是,當我將它添加到視圖中時,VStack例如在 a中,會發生這種情況:(單擊以查看影片;它是 .gif)

我不知道為什么會發生這種情況或如何解決它。我已經嘗試過添加修飾符,例如.fixedSize(horizontal: true, vertical: true)或.frame(width: 50, height: 50),但這無濟于事。
有誰知道發生了什么?
uj5u.com熱心網友回復:
嘗試使用帶有連接值的影片,例如
Circle()
.fill(Color.black.opacity(opacity))
.animation(.easeInOut(duration: 2).delay(1.5).repeatForever(autoreverses: false), value: opacity) // << here !!
.frame(width: 7, height: 7)
.scaleEffect(scale)
.animation(.easeIn(duration: 2.5).delay(1).repeatForever(autoreverses: false), value: scale) // << here !!
重要提示:如果它在NavigationView影片狀態下啟動,則應推遲激活。https://stackoverflow.com/a/66643096/12299030提供了原因和調查細節。
uj5u.com熱心網友回復:
這個問題與您的代碼無關,因為我們應該使用 NavigationView 來修復它,DispatchQueue.main.async我也想重構您的代碼,希望對您有所幫助,您也可以盡可能避免使用硬編碼值,例如您可以在外部應用框架看法:
struct ContentView: View {
var body: some View {
CircleView(lineWidth: 0.5, color: .red, scale: 5.0)
.frame(width: 7, height: 7)
}
}
struct CircleView: View {
let lineWidth: CGFloat
let color: Color
let scale: CGFloat
@State private var startAnimation: Bool = Bool()
var body: some View {
Circle()
.strokeBorder(style: .init(lineWidth: startAnimation ? .zero : lineWidth)).opacity(startAnimation ? .zero : 1.0)
.scaleEffect(startAnimation ? scale : 1.0)
.overlay( Circle() )
.foregroundColor(color)
.onAppear() { DispatchQueue.main.async { startAnimation.toggle() } }
.animation(.easeIn(duration: 2.5).delay(1).repeatForever(autoreverses: false), value: startAnimation)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324371.html
上一篇:如何修改CSS文本影片
