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

[#19] HandySwitch 생성 #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions Handy/Handy-Storybook/Atom/HansySwitchViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// HansySwitchViewController.swift
// Handy-Storybook
//
// Created by 이조은 on 9/18/24.
//

import Handy
import UIKit

final class HansySwitchViewController: BaseViewController {

let switch1: HandySwitch = {
let uiSwitch = HandySwitch()
uiSwitch.size = .large
return uiSwitch
}()

let switch2: HandySwitch = {
let uiSwitch = HandySwitch()
uiSwitch.size = .medium
uiSwitch.isOn = true
return uiSwitch
}()

let switch3: HandySwitch = {
let uiSwitch = HandySwitch()
uiSwitch.size = .small
uiSwitch.isDisabled = true
return uiSwitch
}()

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .white
}

override func setViewHierarchies() {
self.view.addSubview(switch1)
self.view.addSubview(switch2)
self.view.addSubview(switch3)
}

override func setViewLayouts() {
switch1.snp.makeConstraints {
$0.top.equalToSuperview().inset(100)
$0.leading.equalToSuperview().inset(20)
}
switch2.snp.makeConstraints {
$0.top.equalTo(switch1.snp.bottom).offset(36) // -4
$0.leading.equalToSuperview().inset(13) // -7
}
switch3.snp.makeConstraints {
$0.top.equalTo(switch2.snp.bottom).offset(28) // -12
$0.leading.equalToSuperview().inset(8) // -12
}
}
}
2 changes: 1 addition & 1 deletion Handy/Handy-Storybook/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: UIScreen.main.bounds)
window?.windowScene = windowScene
window?.rootViewController = HandyBoxButtonViewController()
window?.rootViewController = HansySwitchViewController()
window?.makeKeyAndVisible()
}

Expand Down
8 changes: 8 additions & 0 deletions Handy/Handy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
025776562C4EB7BB00272EC6 /* Pretendard-SemiBold.otf in Resources */ = {isa = PBXBuildFile; fileRef = 025776532C4EB7BB00272EC6 /* Pretendard-SemiBold.otf */; };
025776592C4EB8BC00272EC6 /* Pretendard-Light.otf in Resources */ = {isa = PBXBuildFile; fileRef = 025776582C4EB8BC00272EC6 /* Pretendard-Light.otf */; };
0257765D2C4EB9EF00272EC6 /* BaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0257765C2C4EB9EF00272EC6 /* BaseViewController.swift */; };
02697A242C99D7230027A362 /* HandySwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02697A232C99D7230027A362 /* HandySwitch.swift */; };
02697A262C99DDA30027A362 /* HansySwitchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02697A252C99DDA30027A362 /* HansySwitchViewController.swift */; };
029C44682C468F8300331F61 /* Pretendard-Light.otf in Resources */ = {isa = PBXBuildFile; fileRef = 029C44652C468F8300331F61 /* Pretendard-Light.otf */; };
029C44692C468F8300331F61 /* Pretendard-Regular.otf in Resources */ = {isa = PBXBuildFile; fileRef = 029C44662C468F8300331F61 /* Pretendard-Regular.otf */; };
029C446A2C468F8300331F61 /* Pretendard-SemiBold.otf in Resources */ = {isa = PBXBuildFile; fileRef = 029C44672C468F8300331F61 /* Pretendard-SemiBold.otf */; };
Expand Down Expand Up @@ -75,6 +77,8 @@
025776532C4EB7BB00272EC6 /* Pretendard-SemiBold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Pretendard-SemiBold.otf"; path = "Handy-Storybook/Font/Pretendard-SemiBold.otf"; sourceTree = SOURCE_ROOT; };
025776582C4EB8BC00272EC6 /* Pretendard-Light.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Pretendard-Light.otf"; sourceTree = "<group>"; };
0257765C2C4EB9EF00272EC6 /* BaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseViewController.swift; sourceTree = "<group>"; };
02697A232C99D7230027A362 /* HandySwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HandySwitch.swift; sourceTree = "<group>"; };
02697A252C99DDA30027A362 /* HansySwitchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HansySwitchViewController.swift; sourceTree = "<group>"; };
029C44652C468F8300331F61 /* Pretendard-Light.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Pretendard-Light.otf"; sourceTree = "<group>"; };
029C44662C468F8300331F61 /* Pretendard-Regular.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Pretendard-Regular.otf"; sourceTree = "<group>"; };
029C44672C468F8300331F61 /* Pretendard-SemiBold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Pretendard-SemiBold.otf"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -135,6 +139,7 @@
children = (
025776382C4EA98C00272EC6 /* LabelViewController.swift */,
02ED764B2C57BD09001569F1 /* HandyBoxButtonViewController.swift */,
02697A252C99DDA30027A362 /* HansySwitchViewController.swift */,
);
path = Atom;
sourceTree = "<group>";
Expand Down Expand Up @@ -188,6 +193,7 @@
children = (
02ED762F2C52849A001569F1 /* HandyButton */,
029E47FC2C49FD1A00D2F3B7 /* HandyLabel.swift */,
02697A232C99D7230027A362 /* HandySwitch.swift */,
);
path = Atom;
sourceTree = "<group>";
Expand Down Expand Up @@ -405,6 +411,7 @@
0257765D2C4EB9EF00272EC6 /* BaseViewController.swift in Sources */,
02ED764C2C57BD09001569F1 /* HandyBoxButtonViewController.swift in Sources */,
025776352C4EA98C00272EC6 /* AppDelegate.swift in Sources */,
02697A262C99DDA30027A362 /* HansySwitchViewController.swift in Sources */,
025776372C4EA98C00272EC6 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -415,6 +422,7 @@
files = (
02ED76332C5284E6001569F1 /* HandyBoxButton.swift in Sources */,
E5D02AFD2C46C5A70056CE7B /* HandySematic.swift in Sources */,
02697A242C99D7230027A362 /* HandySwitch.swift in Sources */,
E5D02B002C480A180056CE7B /* HandyPrimitive.swift in Sources */,
E5669A3F2C443E7300DABC21 /* HandyBasicColor.swift in Sources */,
02ED76312C5284BB001569F1 /* HandyButtonProtocol.swift in Sources */,
Expand Down
85 changes: 85 additions & 0 deletions Handy/Handy/Source/Atom/HandySwitch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// HandySwitch.swift
// Handy
//
// Created by 이조은 on 9/18/24.
//

import UIKit

public class HandySwitch: UISwitch {

// MARK: - 외부에서 지정할 수 있는 속성
@Invalidating(.layout, .display) public var isDisabled: Bool = false

@Invalidating(.display) public var size: SwitchSize = .medium {
didSet {
updateSwitchSize() // size가 변경될 때 크기 변경
}
}

public override var isOn: Bool {
didSet { setNeedsDisplay() }
}

// MARK: - 외부에서 접근할 수 있는 enum
public enum SwitchSize {
case large
case medium
case small

fileprivate var ratio: CGFloat {
switch self {
case .large:
return 1.0
case .medium:
return 0.7
case .small:
return 0.5
}
}

fileprivate var switchWidth: CGFloat {
return 51.0 * self.ratio
}

fileprivate var switchHeight: CGFloat {
return 31.0 * self.ratio
}
}

// MARK: - 내부에서 사용되는 변수
private var fgColor: UIColor?
private var bgColor: UIColor?

// MARK: - 메소드

public init() {
super.init(frame: .zero)
setupView()
}

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

private func setupView() {
setSwitchColor()
updateSwitchSize()
}

private func setSwitchColor() {
self.subviews.first?.subviews.first?.backgroundColor = HandySemantic.switchUnselected
self.onTintColor = HandySemantic.switchSelected
}

private func updateSwitchSize() {
self.transform = CGAffineTransform(scaleX: size.ratio, y: size.ratio)
}

public override func layoutSubviews() {
super.layoutSubviews()
isEnabled = !isDisabled
setSwitchColor()
}
}
15 changes: 15 additions & 0 deletions Handy/Handy/Source/Foundation/HandySematic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,19 @@ public enum HandySemantic {
public static var paginationBasicUnSelected: UIColor {
return .gray500
}

// MARK: - Switch

public static var switchUnselected: UIColor {
return .gray300
}

public static var switchSelected: UIColor {
return .violet500
}

public static var switchDisabled: UIColor {
return .gray200
}

}