【问题标题】:How add dependency animation between multiple view in SwiftUI?如何在 SwiftUI 中的多个视图之间添加依赖动画?
【发布时间】:2020-07-21 18:02:45
【问题描述】:

我想用 SwiftUI 实现点进度动画。

我认为我们需要在每个点动画与另一个点动画之间添加依赖关系,有了这种依赖关系,我们可以实现下面的动画!

参考

我尝试使用以下代码,但运气不好,我知道这不是实现此类动画的正确方法。

如果有人有解决方案,请随时在您的代码中回答。

感谢您的帮助,不胜感激!

struct AnimatedDot: View {
    @State var rightAnimates = false
    
    var body: some View {
        
        ZStack {
            
            VStack(alignment: .leading) {
            HStack(alignment: .center, spacing: 0){
            Circle()
                .fill(Color.blue)
                .scaleEffect( rightAnimates ? 1: 0)
                .animation(Animation.spring().repeatForever(autoreverses: false)
                    .speed(0.7)
                    .delay(0.25))
                .offset(x: 20)
                .onAppear() {
                    self.rightAnimates.toggle()
                    }
            Circle()
            .fill(Color.blue)
            .scaleEffect( rightAnimates ? 1: 0)
            .animation(Animation.spring().repeatForever(autoreverses: false)
                .speed(0.7)
                .delay(0.5))
                .offset(x: 20).onAppear() {
                    //self.rightAnimates.toggle()
                }
            Circle()
            .fill(Color.blue)
            .scaleEffect( rightAnimates ? 1: 0.5)
            .animation(Animation.spring().repeatForever(autoreverses: false)
                .speed(0.7)
                .delay(0.75))
            .offset(x: 20)
            }.frame(height:20)
            }.frame(width: 120, height: 40)
        }
    }
}

【问题讨论】:

    标签: ios swift animation swiftui swiftui-animation


    【解决方案1】:

    尝试使用您想要的自定义延迟持续时间

    struct AnimatedDot: View {
            
             @State var rightAnimates = false
               
               var body: some View {
                   
                   ZStack {
                       
                       VStack(alignment: .leading) {
                       HStack(alignment: .center, spacing: 0){
                       Circle()
                           .fill(Color.blue)
                           .scaleEffect( rightAnimates ? 1: 0)
                        .animation(Animation.linear(duration: 0.5).repeatForever()
                               .delay(0.25))
                           .offset(x: 20)
                           .onAppear() {
                               self.rightAnimates.toggle()
                               }
                       Circle()
                       .fill(Color.blue)
                       .scaleEffect( rightAnimates ? 1: 0)
                       .animation(Animation.linear(duration: 0.5).repeatForever()
                           .delay(0.5))
                           .offset(x: 20).onAppear() {
                               //self.rightAnimates.toggle()
                           }
                       Circle()
                       .fill(Color.blue)
                       .scaleEffect( rightAnimates ? 1: 0)
                       .animation(Animation.linear(duration: 0.5).repeatForever()
                           .delay(0.75))
                       .offset(x: 20)
                       }.frame(height:20)
                       }.frame(width: 120, height: 40)
                   }
               }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2021-04-04
      • 2016-03-03
      相关资源
      最近更新 更多