Repeating animation on SwiftUI Image

前端 未结 2 1373
挽巷
挽巷 2021-02-19 21:21

Given the following struct:

struct PeopleList : View {
    @State var angle: Double = 0.0
    @State var isAnimating = true

    var foreverAnimatio         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 21:48

    UPDATE: There is a backspin involved while stopping the animation which is solved with this solution.

    I think its this what you are looking for:

    struct PeopleList : View {
        @State var angle: Double = 0.0
        @State var isAnimating = false
        
        var foreverAnimation: Animation {
            Animation.linear(duration: 2.0)
                .repeatForever(autoreverses: false)
        }
        
        var body: some View {
            Button(action: {}, label: {
                Image(systemName: "arrow.2.circlepath")
                    .rotationEffect(Angle(degrees: self.isAnimating ? 360.0 : 0.0))
                    .animation(self.foreverAnimation)
                    .onAppear {
                        self.isAnimating = true
                }
            })
        }
    }
    

提交回复
热议问题