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 [#9] 커스텀 버튼 구현 #10

Merged
merged 11 commits into from
Jan 4, 2024
24 changes: 24 additions & 0 deletions HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
0B0035402B43D64D00DA140C /* HMHNavigationBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B00353F2B43D64D00DA140C /* HMHNavigationBar.swift */; };
0B2C2D3B2B443BE90023CCFA /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2C2D3A2B443BE90023CCFA /* Image.swift */; };
0B2C2D3F2B4559E10023CCFA /* OnboardingButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2C2D3E2B4559E10023CCFA /* OnboardingButton.swift */; };
0B2C2D412B4572240023CCFA /* HMHSelectButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2C2D402B4572240023CCFA /* HMHSelectButton.swift */; };
0B50F9CB2B369813000C5046 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B50F9CA2B369813000C5046 /* AppDelegate.swift */; };
0B50F9CD2B369813000C5046 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B50F9CC2B369813000C5046 /* SceneDelegate.swift */; };
0B50F9CF2B369813000C5046 /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B50F9CE2B369813000C5046 /* HomeViewController.swift */; };
Expand Down Expand Up @@ -60,6 +62,8 @@
/* Begin PBXFileReference section */
0B00353F2B43D64D00DA140C /* HMHNavigationBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HMHNavigationBar.swift; sourceTree = "<group>"; };
0B2C2D3A2B443BE90023CCFA /* Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
0B2C2D3E2B4559E10023CCFA /* OnboardingButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingButton.swift; sourceTree = "<group>"; };
0B2C2D402B4572240023CCFA /* HMHSelectButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HMHSelectButton.swift; sourceTree = "<group>"; };
0B50F9C72B369813000C5046 /* HMH_iOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HMH_iOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
0B50F9CA2B369813000C5046 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
0B50F9CC2B369813000C5046 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -121,6 +125,22 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0B2C2D3C2B4559AE0023CCFA /* Onboarding */ = {
isa = PBXGroup;
children = (
0B2C2D3D2B4559C90023CCFA /* Views */,
);
path = Onboarding;
sourceTree = "<group>";
};
0B2C2D3D2B4559C90023CCFA /* Views */ = {
isa = PBXGroup;
children = (
0B2C2D3E2B4559E10023CCFA /* OnboardingButton.swift */,
);
path = Views;
sourceTree = "<group>";
};
0B50F9BE2B369813000C5046 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -184,6 +204,7 @@
0B67CCE72B369BE300582D54 /* Presentation */ = {
isa = PBXGroup;
children = (
0B2C2D3C2B4559AE0023CCFA /* Onboarding */,
0B8A89A72B369DE600688BA6 /* Home */,
0B8A89A52B369DD500688BA6 /* Common */,
);
Expand Down Expand Up @@ -315,6 +336,7 @@
isa = PBXGroup;
children = (
0B00353F2B43D64D00DA140C /* HMHNavigationBar.swift */,
0B2C2D402B4572240023CCFA /* HMHSelectButton.swift */,
);
path = UIComponets;
sourceTree = "<group>";
Expand Down Expand Up @@ -464,6 +486,7 @@
buildActionMask = 2147483647;
files = (
0B50F9CF2B369813000C5046 /* HomeViewController.swift in Sources */,
0B2C2D412B4572240023CCFA /* HMHSelectButton.swift in Sources */,
0B8A89BC2B369F2D00688BA6 /* UIView+.swift in Sources */,
0B8A89AF2B369E4300688BA6 /* HomeModel.swift in Sources */,
36A3D9C02B409CBD007EA272 /* Font.swift in Sources */,
Expand All @@ -472,6 +495,7 @@
0B50F9CB2B369813000C5046 /* AppDelegate.swift in Sources */,
0B8A89C42B369FA000688BA6 /* F.swift in Sources */,
0B8A89B72B369F1100688BA6 /* C.swift in Sources */,
0B2C2D3F2B4559E10023CCFA /* OnboardingButton.swift in Sources */,
0B2C2D3B2B443BE90023CCFA /* Image.swift in Sources */,
0B8A89AD2B369E3B00688BA6 /* HomeCell.swift in Sources */,
0B8A89C62B369FA800688BA6 /* H.swift in Sources */,
Expand Down
126 changes: 126 additions & 0 deletions HMH_iOS/HMH_iOS/Presentation/Common/UIComponets/HMHSelectButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// HMHSelectButton.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/3/24.
//

import UIKit

import SnapKit
import Then

final class HMHSelectButton: UIButton {
@frozen
enum HMHSelectButtonType {
case solitary
case multiple
case disabled
}

private var selectButtonType: HMHSelectButtonType = .disabled
private var isChecked: Bool = false

private let buttonContentLabel = UILabel().then {
$0.font = UIFont.iosText5Medium16
}

init(buttonType: HMHSelectButtonType, buttonText: String) {
super.init(frame: .zero)
self.selectButtonType = buttonType
buttonContentLabel.text = buttonText

configureButton()
addTarget()
setUI()
}

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

private func setUI() {
setHierarchy()
setConstraints()
}

private func setHierarchy() {
self.addSubview(buttonContentLabel)
}

private func setConstraints() {
self.snp.makeConstraints {
$0.height.equalTo(62.adjusted)
}

buttonContentLabel.snp.makeConstraints {
$0.center.equalToSuperview()
}
}

private func addTarget() {
self.addTarget(self, action: #selector(onboardingButtonTapped), for: .touchUpInside)
}

private func configureButton() {
self.makeCornerRound(radius: 6.adjustedHeight)
self.layer.cornerCurve = .continuous

switch selectButtonType {
case .solitary,.multiple:
self.do {
$0.isEnabled = true
$0.backgroundColor = .gray6
}

buttonContentLabel.do {
$0.textColor = .whiteText
}
case .disabled:
self.do {
$0.isEnabled = false
$0.backgroundColor = .gray7
}

buttonContentLabel.do {
$0.textColor = .gray4
}
}
}

private func updateStyle() {
if isChecked {
backgroundColor = .bluePurpleActive
makeBorder(width: 1.5, color: .bluePurpleLine)
} else {
backgroundColor = .gray6
layer.borderColor = UIColor.clear.cgColor
}
}

private func setChecked(_ checked: Bool) {
isChecked = checked
updateStyle()
}

@objc private func onboardingButtonTapped() {
switch selectButtonType {
case .solitary:
guard !isChecked else { return }

superview?.subviews.forEach { subview in
if let button = subview as? HMHSelectButton, button != self {
button.setChecked(false)
}
}

setChecked(true)

case .multiple:
setChecked(!isChecked)

case .disabled:
return
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// OnboardingButton.swift
// HMH_iOS
//
// Created by Seonwoo Kim on 1/3/24.
//

import UIKit

import SnapKit
import Then

final class OnboardingButton: UIButton {
@frozen
enum OnboardingButtonType {
case enable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5. 소소하지만 .. case naming 혹시 enabled는 어떠신가요? disabled와 맞추는 게 좋아보여서요!

case disabled
}

private var type: OnboardingButtonType = .disabled

private let buttonTitleLabel = UILabel().then {
$0.textColor = .whiteText
$0.font = .iosText4Semibold16
}

init(buttonStatus type: OnboardingButtonType, buttonText: String) {
super.init(frame: .zero)
self.type = type
buttonTitleLabel.text = buttonText

configureButton()
setUI()
}

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

private func setUI() {
setHierarchy()
setConstraints()
}

private func setHierarchy() {
self.addSubview(buttonTitleLabel)
}

private func setConstraints() {
self.snp.makeConstraints {
$0.height.equalTo(52.adjusted)
}

buttonTitleLabel.snp.makeConstraints {
$0.center.equalToSuperview()
}
}

private func configureButton() {
self.makeCornerRound(radius: 6.adjustedHeight)
self.layer.cornerCurve = .continuous

switch type {
case .enable:
self.do {
$0.isEnabled = true
$0.backgroundColor = .bluePurpleButton
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5. 여기도 위랑 똑같이 self.isEnabled = true 이런 식으로 작성 가능 할 것 같아요!

case .disabled:
self.do {
$0.isEnabled = false
$0.backgroundColor = .gray5
}
}
}
}