UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

HWS+ Animating Button Style no longer compiles with Xcode 12.1

Forums > SwiftUI

I've been going throuh the HWS+ tutorial Animating Button Style and with Xcode 12.1 with iOS 14.1 this block no longer compiles and errors with: "Incorrect argument labels in call (have 'animationSpeed:_:label:', expected 'action:label:animationSpeed:')"

struct ContentView: View {
    var body: some View {
        AnimatedButton(animationSpeed: 5) {
            print("Pressed")
        } label: {
            Image(systemName: "star")
        }
    }
}

However I found that this formatting compiles and works as expected.

struct ContentView: View {
    var body: some View {
        AnimatedButton(action: {
            print("Pressed")
        }, label:{
            Image(systemName: "star")
        }, animationSpeed: 3)
    }
}

My question is why oh why Xcode?! Was there a swift change with closures that makes you have to do such things? Althought the solution makes sense somewhate this is kind of baffling... 🤷

3      

Not sure what your "AnimatedButton" struct looks like but this is mine from the HWS+ tutorial

import SwiftUI

protocol AnimatingButtonStyle: ButtonStyle {
    init(animation: Double)
}

struct AnimatedButton<ButtonStyle: AnimatingButtonStyle, Content: View>: View {
    let buttonStyle: ButtonStyle.Type // THE ORDER IS IMPORTANT FOR THE CALL
    var animationSpeed = 5.0
    let action: () -> Void
    let label: () -> Content

    @State private var animation = 0.0

    var body: some View {
        Button(action: action, label: label)
            .buttonStyle(buttonStyle.init(animation: animation))
            .onAppear {
                withAnimation(Animation.easeOut(duration: self.animationSpeed).repeatForever(autoreverses: false)) {
                    self.animation = 1
                }
        }
    }
}

Then you make the configuration eg

struct PulsingButtonStyle: AnimatingButtonStyle {
    let animation: Double

    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding()
            .background(Color.blue)
            .clipShape(Circle())
            .foregroundColor(.white)
            .padding(4)
            .overlay(
                Circle()
                    .stroke(Color.blue, lineWidth: 2)
                    .scaleEffect(CGFloat(1 + animation))
                    .opacity(1 - animation)
                )
    }
}

then when you use in your view

AnimatedButton(buttonStyle: PulsingButtonStyle.self, animationSpeed: 1, action: {
    print("Pressed")
}) {
    Image(systemName: "star")
}

This works fine on Xcode 12.1 fine, so think maybe your code not the same

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.