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

With Animation Not Working

Forums > SwiftUI

struct RatingView: View {

@Binding var rating: Int

let maximumRating = 5

var body: some View { HStack{ Spacer() ForEach(0..<self.maximumRating) { number in getStarRating(number: number) } Spacer() } }

private func getStarRating(number: Int) -> some View {

let isSelected = number > self.rating

return Image(systemName: "star.fill") .foregroundColor(isSelected ? Color(.secondarySystemFill) : Color.black) .onTapGesture { withAnimation{ self.rating = number } } } }

I want to add an basic animation to this rating view using withAnimation. However, the animination does not seem to be working. I would appreciate any help as to why this does not work. And if there are any solutions to this problem?

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.