-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#13] HandyFab 생성 #16
Open
wjdalswl
wants to merge
1
commit into
main
Choose a base branch
from
feature/#13
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// FabViewController.swift | ||
// Handy-Storybook | ||
// | ||
// Created by 정민지 on 7/31/24. | ||
// | ||
|
||
import Handy | ||
|
||
final class FabViewController: BaseViewController { | ||
|
||
let primaryFab: HandyFab = { | ||
let checkBox = HandyFab() | ||
return checkBox | ||
}() | ||
|
||
let disabledPrimaryFab: HandyFab = { | ||
let checkBox = HandyFab() | ||
checkBox.isDisabled = true | ||
return checkBox | ||
}() | ||
|
||
let secondaryFab: HandyFab = { | ||
let checkBox = HandyFab() | ||
checkBox.type = .secondary | ||
checkBox.size = .small | ||
return checkBox | ||
}() | ||
|
||
let disabledSecondaryFab: HandyFab = { | ||
let checkBox = HandyFab() | ||
checkBox.type = .secondary | ||
checkBox.size = .small | ||
checkBox.isDisabled = true | ||
return checkBox | ||
}() | ||
|
||
let customIconFab: HandyFab = { | ||
let checkBox = HandyFab() | ||
checkBox.size = .small | ||
checkBox.iconImage = .add | ||
return checkBox | ||
}() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
} | ||
|
||
override func setViewHierarchies() { | ||
[ | ||
primaryFab, | ||
disabledPrimaryFab, | ||
secondaryFab, | ||
disabledSecondaryFab, | ||
customIconFab | ||
].forEach { | ||
view.addSubview($0) | ||
} | ||
} | ||
|
||
override func setViewLayouts() { | ||
primaryFab.snp.makeConstraints { | ||
$0.center.equalToSuperview() | ||
} | ||
disabledPrimaryFab.snp.makeConstraints { | ||
$0.top.equalTo(primaryFab.snp.bottom).offset(16) | ||
$0.centerX.equalToSuperview() | ||
} | ||
secondaryFab.snp.makeConstraints { | ||
$0.top.equalTo(disabledPrimaryFab.snp.bottom).offset(16) | ||
$0.centerX.equalToSuperview() | ||
} | ||
disabledSecondaryFab.snp.makeConstraints { | ||
$0.top.equalTo(secondaryFab.snp.bottom).offset(16) | ||
$0.centerX.equalToSuperview() | ||
} | ||
customIconFab.snp.makeConstraints { | ||
$0.top.equalTo(disabledSecondaryFab.snp.bottom).offset(16) | ||
$0.centerX.equalToSuperview() | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
// | ||
// HandyFab.swift | ||
// Handy | ||
// | ||
// Created by 정민지 on 7/31/24. | ||
// | ||
|
||
import UIKit | ||
|
||
public class HandyFab: UIButton { | ||
|
||
// MARK: - 외부에서 지정할 수 있는 속성 | ||
|
||
/** | ||
Fab 버튼을 비활성화 시킬 때 사용합니다. | ||
*/ | ||
@Invalidating(.layout, .display) public var isDisabled: Bool = false { | ||
didSet { | ||
setConfiguration() | ||
} | ||
} | ||
|
||
/** | ||
Fab 버튼의 Pressed / Hover 여부를 나타낼 때 사용합니다. | ||
*/ | ||
public override var isHighlighted: Bool { | ||
didSet { setConfiguration() } | ||
} | ||
|
||
/** | ||
Fab 버튼의 타입을 결정할 때 사용합니다. | ||
*/ | ||
@Invalidating(.layout, .display) public var type: FabType = .primary { | ||
didSet { | ||
setConfiguration() | ||
} | ||
} | ||
|
||
/** | ||
Fab 버튼의 크기를 결정할 때 사용합니다. | ||
*/ | ||
@Invalidating(.layout, .display) public var size: FabSize = .large { | ||
didSet { | ||
setConfiguration() | ||
} | ||
} | ||
|
||
/** | ||
Fab 버튼의 아이콘을 설정할 때 사용합니다. | ||
*/ | ||
public var iconImage: UIImage? { | ||
didSet { | ||
setConfiguration() | ||
} | ||
} | ||
|
||
// MARK: - 외부에서 접근할 수 있는 enum | ||
|
||
/** | ||
Fab 버튼의 type 종류입니다. | ||
각 type에 맞는 borderColor, backgroundColor를 computed property로 가지고 있습니다. | ||
*/ | ||
public enum FabType { | ||
case primary | ||
case secondary | ||
|
||
var backgroundColor: UIColor { | ||
switch self { | ||
case .primary: | ||
return HandySemantic.buttonFabPrimaryEnabled | ||
case .secondary: | ||
return HandySemantic.buttonFabSecondaryEnabled | ||
} | ||
} | ||
|
||
var highlightedColor: UIColor { | ||
switch self { | ||
case .primary: | ||
return HandySemantic.buttonFabPrimaryPressed | ||
case .secondary: | ||
return HandySemantic.buttonFabSecondaryPressed | ||
} | ||
} | ||
|
||
var disabledColor: UIColor { | ||
switch self { | ||
case .primary: | ||
return HandySemantic.buttonFabPrimaryDisabled | ||
case .secondary: | ||
return HandySemantic.buttonFabSecondaryDisabled | ||
} | ||
} | ||
|
||
var borderColor: UIColor { | ||
switch self { | ||
case .primary: | ||
return .clear | ||
case .secondary: | ||
return HandySemantic.lineBasicLight | ||
} | ||
} | ||
|
||
var shadowColor: UIColor { | ||
switch self { | ||
case .primary: | ||
return UIColor( | ||
red: 110/255, | ||
green: 118/255, | ||
blue: 135/255, | ||
alpha: 0.25 | ||
) | ||
case .secondary: | ||
return UIColor( | ||
red: 181/255, | ||
green: 185/255, | ||
blue: 196/255, | ||
alpha: 0.25 | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Fab 버튼의 size 종류입니다. | ||
각 size에 맞는 icon을 computed property로 가지고 있습니다. | ||
*/ | ||
public enum FabSize { | ||
case small | ||
case large | ||
|
||
fileprivate var fabSize: CGFloat { | ||
switch self { | ||
case .small: | ||
return 40 | ||
case .large: | ||
return 56 | ||
} | ||
} | ||
|
||
fileprivate var icon: String { | ||
switch self { | ||
case .small: | ||
return "arrow.up" | ||
case .large: | ||
return "square.and.arrow.up" | ||
} | ||
} | ||
} | ||
|
||
// MARK: - 메소드 | ||
|
||
public init() { | ||
super.init(frame: .zero) | ||
setConfiguration() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setConfiguration() { | ||
NSLayoutConstraint.activate([ | ||
widthAnchor.constraint(equalToConstant: size.fabSize), | ||
heightAnchor.constraint(equalToConstant: size.fabSize) | ||
]) | ||
self.layer.cornerRadius = size.fabSize / 2 | ||
self.layer.borderWidth = type == .secondary ? 1 : 0 | ||
|
||
var configuration = UIButton.Configuration.plain() | ||
|
||
let iconConfig = UIImage.SymbolConfiguration(pointSize: 24, weight: .regular) | ||
let iconImage = iconImage == nil ? UIImage( | ||
systemName: size.icon, | ||
withConfiguration: iconConfig | ||
): iconImage | ||
|
||
// TODO: - HandyIcon으로 아이콘 변경, pointSize 삭제, resize 적용(아래 코드로 교체) | ||
// var iconConfig = iconImage == nil ? UIImage(systemName: size.icon): iconImage | ||
// iconConfig = iconConfig?.resize(to: 24) | ||
|
||
|
||
var iconColor: UIColor = type == .secondary ? HandySemantic.iconBasicSecondary : HandySemantic.iconBasicWhite | ||
switch (isDisabled, isHighlighted) { | ||
case (true, _): | ||
self.backgroundColor = type.disabledColor | ||
self.layer.borderColor = type.borderColor.cgColor | ||
iconColor = type == .secondary ? HandySemantic.iconBasicDisabled : HandySemantic.iconBasicWhite | ||
removeShadow() | ||
|
||
case (false, true): | ||
self.backgroundColor = type.highlightedColor | ||
self.layer.borderColor = type.borderColor.cgColor | ||
applyShadow() | ||
|
||
case (false, false): | ||
self.backgroundColor = type.backgroundColor | ||
self.layer.borderColor = type.borderColor.cgColor | ||
applyShadow() | ||
} | ||
|
||
configuration.image = iconImage?.withTintColor(iconColor, renderingMode: .alwaysOriginal) | ||
|
||
self.isEnabled = !isDisabled | ||
self.configuration = configuration | ||
} | ||
|
||
private func applyShadow() { | ||
layer.shadowColor = type.shadowColor.cgColor | ||
layer.shadowOffset = CGSize(width: 0, height: 4) | ||
layer.shadowRadius = 8 | ||
layer.shadowOpacity = 1.0 | ||
} | ||
|
||
private func removeShadow() { | ||
layer.shadowOpacity = 0.0 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Invalidating
은 왜 사용하셨는지 궁금합니다!