Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] #23 - StarRating 커스텀 컴포넌트 구현 #25

Merged
merged 5 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion HealthFoodMe/HealthFoodMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
A9E593C72874266200B0F8B5 /* SearchRecent.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9E593C62874266200B0F8B5 /* SearchRecent.swift */; };
A9E593CF2876C26900B0F8B5 /* Color.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A9E593CE2876C26900B0F8B5 /* Color.xcassets */; };
A9E593D1287818E900B0F8B5 /* SearchTVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9E593D0287818E900B0F8B5 /* SearchTVC.swift */; };
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 @@ -137,7 +138,7 @@
69028BAB2874681700373984 /* MenuTabVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuTabVC.swift; sourceTree = "<group>"; };
69028BAF2874A60E00373984 /* HeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeaderView.swift; sourceTree = "<group>"; };
690AB5482875C1B200DE72D4 /* MenuDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuDataModel.swift; sourceTree = "<group>"; };
695758B6287D7B6700E36789 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Config.swift; path = ../../../../../../../../Downloads/Config.swift; sourceTree = "<group>"; };
695758B6287D7B6700E36789 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
695758B8287D7C0B00E36789 /* MenuTab.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = MenuTab.storyboard; sourceTree = "<group>"; };
A938BB8D287D221200B8DE07 /* Search.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Search.storyboard; sourceTree = "<group>"; };
A938BB8F287D222800B8DE07 /* MainDetail.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = MainDetail.storyboard; sourceTree = "<group>"; };
Expand All @@ -147,6 +148,7 @@
A9E593CE2876C26900B0F8B5 /* Color.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Color.xcassets; sourceTree = "<group>"; };
A9E593D0287818E900B0F8B5 /* SearchTVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchTVC.swift; 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 @@ -391,6 +393,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 @@ -889,6 +899,7 @@
EBF66A9F287227F500DE0ED1 /* Common */ = {
isa = PBXGroup;
children = (
EB033E82287B5B6E00C48FB9 /* UI Components */,
EBF66AA0287227F500DE0ED1 /* Base */,
);
path = Common;
Expand Down Expand Up @@ -1193,6 +1204,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 */,
69028BAC2874681700373984 /* MenuTabVC.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
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Group 267.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group 267@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group 267@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Group 268.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group 268@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group 268@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Group 269.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group 269@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group 269@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// StarRatingView.swift
// HealthFoodMe
//
// Created by Junho Lee on 2022/07/11.
//

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 = 16

// 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()
imageView.snp.makeConstraints { make in
make.width.height.equalTo(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.0001

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
}
}
}
}
}