Повторение анимации на SwiftUI Image - PullRequest
1 голос
/ 10 октября 2019

Учитывая следующее struct:

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

    var foreverAnimation: Animation {
        Animation.linear(duration: 2.0)
            .repeatForever()
    }

    var body: some View {
        Button(action: {
            self.peopleViewModel.load()
        }, label: {
            Image(systemName: "arrow.2.circlepath")
                .rotationEffect(Angle(degrees: self.isAnimating ? self.angle : 0.0))
                .onAppear {
                    withAnimation(self.foreverAnimation) {
                        self.angle += 10.0
                    }
                }
        })
    }
}

Я надеялся, что Image будет вращаться по часовой стрелке и повторяться до тех пор, пока self.isAnimating не станет false, хотя анимируется только один раз.

1 Ответ

2 голосов
/ 10 октября 2019

Я думаю, это то, что вы ищете:

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: {
            self.peopleViewModel.load()
        }, label: {
            Image(systemName: "arrow.2.circlepath")
                .rotationEffect(Angle(degrees: self.isAnimating ? 360.0 : 0.0))
                .animation(self.foreverAnimation)
                .onAppear {
                    self.isAnimating = true
            }
        })
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...