简体   繁体   English

animation 在 SwiftUI 完成后隐藏和显示视图

[英]Hide and Show View after animation is finished in SwiftUI

How to hide and show an Image/View after an animation is done in SwiftUI.如何在 SwiftUI 中完成 animation 后隐藏和显示图像/视图。

Image(systemName: "arrow.2.circlepath.circle.fill")
    .rotationEffect(.degrees(spin ? 360 : 0))
    .animation(Animation.easeInOut(duration: 0.8).repeatCount(5))

Use one variable to control the rotation and a second one to control the alpha:使用一个变量来控制旋转,使用第二个变量来控制 alpha:

struct ContentView: View {
  @State var degrees: Double = 0
  @State var alpha: Double = 1
    var body: some View {
        Image(systemName: "arrow.2.circlepath.circle.fill")
        .rotationEffect(.degrees(degrees))
        .onAppear(perform: {
          withAnimation(Animation.easeInOut(duration: 0.8).repeatCount(5)) {
            self.degrees = 360
          }
          DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
            self.alpha = 0
          }
        })
        .opacity(alpha)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM