How to STOP Animation().repeatForever in SwiftUI

前端 未结 1 1725
滥情空心
滥情空心 2021-01-19 19:19

In SwiftUI I am trying to start and STOP an animation with a button. I can\'t find a method within SwiftUI that lets me stop an automation. In Swift 4 I used to say \"star

相关标签:
1条回答
  • 2021-01-19 19:49

    The following should work, moved animation into overlay-only and added conditional to .default (tested with Xcode 11.2 / iOS 13.2)

    Button("Start", action: { self.start.toggle() })
        .font(.largeTitle)
        .padding()
        .foregroundColor(.white)
        .background(RoundedRectangle(cornerRadius: 10).fill(Color.green))
        .overlay(
            RoundedRectangle(cornerRadius: 10)
                .stroke(Color.green, lineWidth: 4)
                .scaleEffect(start ? 2 : 0.9)
                .opacity(start ? 0 : 1)
                .animation(start ? Animation.easeOut(duration: 0.6)
                    .delay(1) // Add 1 second between animations
                    .repeatForever(autoreverses: false) : .default)
        )
    
    0 讨论(0)
提交回复
热议问题