AutorÃa | Ultima modificación | Ver Log |
//// FiveStarView.swift// twogetskills//// Created by Efrain Yanez Recanatini on 7/27/22.//import SwiftUIpublic struct FiveStarView: View {var rating: Decimalvar color: Colorvar backgroundColor: Colorpublic init(rating: Decimal,color: Color = .red,backgroundColor: Color = .gray) {self.rating = ratingself.color = colorself.backgroundColor = backgroundColor}public var body: some View {ZStack {BackgroundStars(backgroundColor)ForegroundStars(rating: rating, color: color)}}}struct RatingStar: View {var rating: CGFloatvar color: Colorvar index: Intvar maskRatio: CGFloat {let mask = rating - CGFloat(index)switch mask {case 1...: return 1case ..<0: return 0default: return mask}}init(rating: Decimal, color: Color, index: Int) {self.rating = CGFloat(Double(rating.description) ?? 0)self.color = colorself.index = index}var body: some View {GeometryReader { star inStarImage().foregroundColor(self.color).mask(Rectangle().size(width: star.size.width * self.maskRatio,height: star.size.height))}}}private struct StarImage: View {var body: some View {Image(systemName: "star.fill").resizable().aspectRatio(contentMode: .fill)}}private struct BackgroundStars: View {var color: Colorinit(_ color: Color) {self.color = color}var body: some View {HStack {ForEach(0..<5) { _ inStarImage()}}.foregroundColor(color)}}private struct ForegroundStars: View {var rating: Decimalvar color: Colorinit(rating: Decimal, color: Color) {self.rating = ratingself.color = color}var body: some View {HStack {ForEach(0..<5) { index inRatingStar(rating: self.rating,color: self.color,index: index)}}}}struct FiveStarView_Previews: PreviewProvider {static var previews: some View {VStack {FiveStarView(rating: 4.5).frame(minWidth: 1, idealWidth: 100, maxWidth: 150, minHeight: 1, idealHeight: 30, maxHeight: 50, alignment: .center)}}}