spacenation / swiftui-sliders

:rocket: SwiftUI Sliders with custom styles
MIT License
793 stars 84 forks source link

Request (possibly just for advice): A star slider with half-star values #27

Closed NCrusher74 closed 2 years ago

NCrusher74 commented 3 years ago

There are tutorials available for how to make a star rating UI component, but I'm really hoping to do something a little more complicated, and I think it's going to require handling the rating as a slider (with an invisible knob).

My idea is that the "bar" will be a row of five stars and then sliding along it results in this:

0.0 = "star" x 5 0.5 = ("star.leadinghalf.fill" x 1) + ("star" x 4) 1.0 = ("star.fill" x 1) + ("star" x 4) 1.5 = ("star.fill" x 1) + ("star.leadinghalf.fill" x 1) + ("star" x 3) .. and so forth up to ("star.fill" x 5)

I've tried altering your sliders to do something like this, and clearly I did it wrong because any attempt to "slide" along the row resulted in the row of stars changing position on the screen.

Is this something that should be handled as a slider, or should I instead perhaps handle it so that each star is a button that starts at "star (empty)" and changes to "star.leadinghalf.fill" the first time it's tapped, and then to "star.fill" if it's tapped again, before cycling back to being empty?

ay42 commented 2 years ago

@NCrusher74 probably too late for an answer but you can do this with masking views

Thumb = EmptyView() Slider background = Row of grey stars Slider track = Row of yellow stars Slider track mask = Rectangle

I would go for a custom RatingView instead.