Skip to content

Commit

Permalink
[Feat] Health-Food-Me#23 - StarRatingView spacing 및 rating 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
L-j-h-c committed Jul 10, 2022
1 parent fa6a0e9 commit a4daccf
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
12 changes: 12 additions & 0 deletions HealthFoodMe/HealthFoodMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
3BCC441E28745FD800661A12 /* SocialLoginVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BCC441D28745FD800661A12 /* SocialLoginVC.swift */; };
3BCC442028745FE500661A12 /* SocialLogin.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3BCC441F28745FE500661A12 /* SocialLogin.storyboard */; };
A9E593CF2876C26900B0F8B5 /* Color.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A9E593CE2876C26900B0F8B5 /* Color.xcassets */; };
EB033E84287B5BE300C48FB9 /* StarRatingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB033E83287B5BE300C48FB9 /* StarRatingView.swift */; };
EB6A44C7287366DF00749582 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = EB6A44C6287366DF00749582 /* .swiftlint.yml */; };
EB6A44D128737CC400749582 /* MainDetailUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB6A44CB28737CC400749582 /* MainDetailUseCase.swift */; };
EB6A44D228737CC400749582 /* MainDetailRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB6A44CC28737CC400749582 /* MainDetailRepository.swift */; };
Expand Down Expand Up @@ -121,6 +122,7 @@
3BCC441F28745FE500661A12 /* SocialLogin.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = SocialLogin.storyboard; sourceTree = "<group>"; };
A9E593CE2876C26900B0F8B5 /* Color.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Color.xcassets; sourceTree = "<group>"; };
E71673C053B2E49E39F1AADB /* Pods-HealthFoodMe.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HealthFoodMe.release.xcconfig"; path = "Target Support Files/Pods-HealthFoodMe/Pods-HealthFoodMe.release.xcconfig"; sourceTree = "<group>"; };
EB033E83287B5BE300C48FB9 /* StarRatingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StarRatingView.swift; sourceTree = "<group>"; };
EB085ED628716E1900361837 /* HealthFoodMe.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HealthFoodMe.app; sourceTree = BUILT_PRODUCTS_DIR; };
EB085EE728716E1B00361837 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
EB6A44C6287366DF00749582 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -269,6 +271,14 @@
path = Pods;
sourceTree = "<group>";
};
EB033E82287B5B6E00C48FB9 /* UI Components */ = {
isa = PBXGroup;
children = (
EB033E83287B5BE300C48FB9 /* StarRatingView.swift */,
);
path = "UI Components";
sourceTree = "<group>";
};
EB085ECD28716E1900361837 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -765,6 +775,7 @@
EBF66A9F287227F500DE0ED1 /* Common */ = {
isa = PBXGroup;
children = (
EB033E82287B5B6E00C48FB9 /* UI Components */,
EBF66AA0287227F500DE0ED1 /* Base */,
);
path = Common;
Expand Down Expand Up @@ -1061,6 +1072,7 @@
EBF66ADE287227F500DE0ED1 /* HomeRouter.swift in Sources */,
EBF66ACF287227F500DE0ED1 /* Result+.swift in Sources */,
EBF66AD7287227F500DE0ED1 /* Presentable.swift in Sources */,
EB033E84287B5BE300C48FB9 /* StarRatingView.swift in Sources */,
EB6A451128740CFF00749582 /* MainInfoTVC.swift in Sources */,
EB6A44F22873BED500749582 /* MainMapViewModel.swift in Sources */,
EBF66AE0287227F500DE0ED1 /* EventLogger.swift in Sources */,
Expand Down
10 changes: 9 additions & 1 deletion HealthFoodMe/HealthFoodMe/Global/Literals/ImageLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import UIKit

struct ImageLiterals {

struct Common {
static let starIcon = UIImage(named: "icn_star")
static let starIcon_25 = UIImage(named: "icn_star_25")
static let starIcon_50 = UIImage(named: "icn_star_50")
static let starIcon_75 = UIImage(named: "icn_star_75")
static let starIcon_filled = UIImage(named: "icn_star_filled")
}

struct MainDetail {
static let scrapIcon = UIImage(named: "icn_scrap")
static let scrapIcon_filled = UIImage(named: "icn_scrap_filled")
Expand All @@ -21,7 +29,7 @@ struct ImageLiterals {
static let showdownIcon = UIImage(named: "icn_showdown")
static let showupIcon = UIImage(named: "icn_showup")
static let timeIcon = UIImage(named: "icn_time")

static let tempSalady = UIImage(named: "tempSalady")
static let tempMuseum = UIImage(named: "tempMuseum")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,106 @@
// Created by Junho Lee on 2022/07/11.
//

import Foundation
import UIKit

import SnapKit

final class StarRatingView: UIView {

// MARK: - Properties

var spacing: CGFloat = 2 {
didSet {
starStackView.spacing = self.spacing
}
}

var rate: CGFloat = 0 {
didSet {
paintStars()
}
}

private var starImageViews: [UIImageView] = []

private var starScale: CGFloat = 50

// MARK: - UI Components

private lazy var starStackView: UIStackView = {
let st = UIStackView()
st.axis = .horizontal
st.spacing = self.spacing
st.distribution = .fillProportionally
return st
}()

// MARK: View Life Cycle

convenience init(starScale: CGFloat) {
self.init()

self.starScale = starScale
setUI()
setLayout()
}

private override init(frame: CGRect) {
super.init(frame: frame)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

// MARK: - Methods

extension StarRatingView {

private func setUI() {
self.backgroundColor = .white
}

private func setLayout() {
self.addSubviews(starStackView)

setStarStackView()
starStackView.snp.makeConstraints { make in
make.center.equalToSuperview()
}
}

private func setStarStackView() {
for starNumber in 0...4 {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.starScale, height: self.starScale))
imageView.contentMode = .scaleToFill
imageView.image = ImageLiterals.Common.starIcon
imageView.tag = starNumber
starImageViews.append(imageView)
starStackView.addArrangedSubviews(imageView)
}
}

func paintStars() {
starImageViews.forEach { star in
let starTag = CGFloat(star.tag) - 0.001

if starTag <= rate - 1 {
star.image = ImageLiterals.Common.starIcon_filled
} else {
if starTag < rate - 0.9 {
star.image = ImageLiterals.Common.starIcon_filled
} else if starTag <= rate - 0.6 {
star.image = ImageLiterals.Common.starIcon_75
} else if starTag < rate - 0.4 {
star.image = ImageLiterals.Common.starIcon_50
} else if starTag <= rate - 0.1 {
star.image = ImageLiterals.Common.starIcon_25
} else {
star.image = ImageLiterals.Common.starIcon
}
}
}
}
}

0 comments on commit a4daccf

Please sign in to comment.