1

I'm trying to get an activity indicator to rotate during download. For some reason, when I try to get the animation to repeat forever, it throws an error way at the bottom of the VStack code.

This works:

struct AddView: View {

    @State var showActivitySpinner: Bool = false
    @State var urlText: String = ""

    var body: some View {
        VStack{
            Image(systemName: "arrow.2.circlepath.circle")
                .resizable()
                .frame(width: 80, height: 80)
                .opacity(showActivitySpinner ? 1 : 0)
                .rotationEffect(.degrees(showActivitySpinner ? 360 : 0))
                .animation(.easeIn(duration: 1.0))

            Spacer()
                .frame(maxHeight: 100)

            TextField("placeholder text", text: $urlText)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .font(Font.system(size: 16, design: .default))
                .offset(y: -50)

            Button(
                action:{self.myFunction()},
                label: {
                    HStack{
                        Image(systemName: "plus.circle.fill")
                        Text("Add")
                            .font(.title)
                    }
                }
            )
        }
        .padding()
        .offset(y: -100)

}

But then when I add .repeatForever(), xcode has a problem with the VStack offset.

"Extraneous argument label 'y:' in call" What is going on?

BenJ
  • 187
  • 1
  • 14

2 Answers2

2

You must write Animation before .easeIn

Correct your modifier to this:

.animation(Animation.easeIn(duration: 1.0).repeatForever(autoreverses: true))

As you probably already know, SwiftUI (at the moment) doesn't point to the correct line of error. Thats why it points to the offset.

1

I don’t think this is related to the .repeatsForever() code.

From the documentation, there are two methods for changing VStack offset:

func offset(CGSize) -> View

func offset(x: CGFloat, y: CGFloat) -> View

Neither of these have only a y label. XCode thinks you mean the first one and assumes you have incorrectly added a y argument when it expects a CGSize. Try using the offset(x: CGFloat, y: CGFloat) method (with x = 0).

I reckon there was just a delay in processing your code before the error appeared.

Chris
  • 4,009
  • 3
  • 21
  • 52
  • Very strange. I build and run the first version (without .repeatForever) with no errors. Then I add .repeatForever and I get the above error. Then if I make the fix you suggest, I get an error on line 25 next to Spacer() - "Type of expression is ambiguous without more context." I'm clueless. – BenJ Nov 20 '19 at 22:29
  • @BenJ Try moving `.repeatsForever() outside the parentheses to the right (i.e. calling it on the animation rather than consecutively after the .easeIn - worth a shot! – Chris Nov 20 '19 at 22:35
  • That produces "Protocol type 'Any' cannot conform to 'View' because only concrete types can conform to protocols" on line 17, as well as "Value of type 'some View' has no member 'repeatForever'" on line 23. – BenJ Nov 20 '19 at 22:38
  • @BenJ Check this answer for a guide about using animation in SwiftUI - https://stackoverflow.com/a/58624788/8289095 – Chris Nov 21 '19 at 06:09