Skip to content

Commit

Permalink
Merge pull request #194 from Link-MIND/feat/#193
Browse files Browse the repository at this point in the history
[Feat] #193 - Sheet UISheetPresentationController로 로직 변경
  • Loading branch information
mini-min authored Sep 3, 2024
2 parents df7fcad + 00d962a commit cda0574
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 226 deletions.
12 changes: 6 additions & 6 deletions TOASTER-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
83474A6D2BED0750009B9C48 /* PatchEditLinkTitleResponseDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8364220B2BE7BFB2005C4085 /* PatchEditLinkTitleResponseDTO.swift */; };
8364220C2BE7BFB2005C4085 /* PatchEditLinkTitleResponseDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8364220B2BE7BFB2005C4085 /* PatchEditLinkTitleResponseDTO.swift */; };
8388E98C2BC8FAB200858C5C /* PatchEditLinkTitleRequestDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8388E98B2BC8FAB200858C5C /* PatchEditLinkTitleRequestDTO.swift */; };
8388E98E2BC8FC6700858C5C /* DetailEditLinkBottomSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8388E98D2BC8FC6700858C5C /* DetailEditLinkBottomSheetView.swift */; };
8388E98E2BC8FC6700858C5C /* EditLinkBottomSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8388E98D2BC8FC6700858C5C /* EditLinkBottomSheetView.swift */; };
83CFC3372B564F1100A2EB2B /* WeeklyLinkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83CFC3362B564F1100A2EB2B /* WeeklyLinkModel.swift */; };
83CFC3392B568BE700A2EB2B /* RecommendSiteModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83CFC3382B568BE700A2EB2B /* RecommendSiteModel.swift */; };
83CFC33B2B57324700A2EB2B /* SaveLinkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83CFC33A2B57324700A2EB2B /* SaveLinkModel.swift */; };
Expand Down Expand Up @@ -487,7 +487,7 @@
8315CD902B5521F70061F377 /* SelectClipModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectClipModel.swift; sourceTree = "<group>"; };
8364220B2BE7BFB2005C4085 /* PatchEditLinkTitleResponseDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PatchEditLinkTitleResponseDTO.swift; sourceTree = "<group>"; };
8388E98B2BC8FAB200858C5C /* PatchEditLinkTitleRequestDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PatchEditLinkTitleRequestDTO.swift; sourceTree = "<group>"; };
8388E98D2BC8FC6700858C5C /* DetailEditLinkBottomSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailEditLinkBottomSheetView.swift; sourceTree = "<group>"; };
8388E98D2BC8FC6700858C5C /* EditLinkBottomSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditLinkBottomSheetView.swift; sourceTree = "<group>"; };
83CFC3362B564F1100A2EB2B /* WeeklyLinkModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeeklyLinkModel.swift; sourceTree = "<group>"; };
83CFC3382B568BE700A2EB2B /* RecommendSiteModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecommendSiteModel.swift; sourceTree = "<group>"; };
83CFC33A2B57324700A2EB2B /* SaveLinkModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveLinkModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -679,7 +679,7 @@
39BE4BBD2B4ABB7C002B471D /* DetailClipSegmentedControlView.swift */,
39BE4BBF2B4ABB9E002B471D /* DetailClipEmptyView.swift */,
39BE4BC12B4ABBB0002B471D /* DeleteLinkBottomSheetView.swift */,
8388E98D2BC8FC6700858C5C /* DetailEditLinkBottomSheetView.swift */,
8388E98D2BC8FC6700858C5C /* EditLinkBottomSheetView.swift */,
);
path = Component;
sourceTree = "<group>";
Expand Down Expand Up @@ -1948,7 +1948,7 @@
6BE6DA9C2B54752B008B06FA /* GetTimerMainpageResponseDTO.swift in Sources */,
6B6AE6AA2B3FF6EA000E2366 /* UIStackView+.swift in Sources */,
8305178A2B4D1E09009FFB60 /* UserClipCollectionViewCell.swift in Sources */,
8388E98E2BC8FC6700858C5C /* DetailEditLinkBottomSheetView.swift in Sources */,
8388E98E2BC8FC6700858C5C /* EditLinkBottomSheetView.swift in Sources */,
3913B0AE2BCEC9C00031A3EB /* UpdateAlertManager.swift in Sources */,
3F2FA17D2B4928B700EDBF95 /* LoginUseCase.swift in Sources */,
6BE6DA9A2B54747B008B06FA /* TimerAPIService.swift in Sources */,
Expand Down Expand Up @@ -2226,7 +2226,7 @@
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -2261,7 +2261,7 @@
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ import Then

final class ToasterBottomSheetViewController: UIViewController {

// MARK: - Properties

private var bottomHeight: CGFloat = 100
private var keyboardHeight: CGFloat = 100

// MARK: - UI Properties
private var insertView = UIView()
private let dimmedBackView = UIView()
private let bottomSheetView = UIView()

private var insertView: UIView

private let titleLabel = UILabel()
private let closeButton = UIButton()
Expand All @@ -29,15 +23,10 @@ final class ToasterBottomSheetViewController: UIViewController {

init(bottomType: BottomType,
bottomTitle: String,
height: CGFloat,
insertView: UIView) {
super.init(nibName: nil, bundle: nil)

self.bottomSheetView.backgroundColor = bottomType.color
self.titleLabel.textAlignment = bottomType.alignment
self.titleLabel.text = bottomTitle
self.bottomHeight = height
self.insertView = insertView
super.init(nibName: nil, bundle: nil)
setupInitialStyle(bottomType: bottomType, bottomTitle: bottomTitle)
}

required init?(coder: NSCoder) {
Expand All @@ -46,106 +35,58 @@ final class ToasterBottomSheetViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

setupStyle()
setupHierarchy()
setupLayout()
setupDismissAction()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

showBottomSheet()
}
}

// MARK: - Networks

@MainActor
extension ToasterBottomSheetViewController {
/// 바텀 시트 표출
func showBottomSheet() {
Task {
self.updateBottomSheetLayout()
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.dimmedBackView.backgroundColor = .black900.withAlphaComponent(0.5)
self.view.layoutIfNeeded()
})
}
}

/// 바텀 시트 내리기
func hideBottomSheet() {
Task {
self.bottomSheetView.snp.remakeConstraints {
$0.bottom.leading.trailing.equalToSuperview()
}
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.dimmedBackView.backgroundColor = .clear
self.view.layoutIfNeeded()
self.view.endEditing(true)
}, completion: { _ in
if self.presentingViewController != nil {
self.dismiss(animated: false, completion: nil)
}
})
func setupSheetPresentation(bottomHeight: CGFloat) {
if let sheet = self.sheetPresentationController {
sheet.detents = [.custom(resolver: { _ in bottomHeight - 40 })]
sheet.preferredCornerRadius = 20
}
}

/// 바텀 시트 올라와있는 상태에서 높이 변화 시키기
func changeHeightBottomSheet(height: CGFloat) {
Task {
self.bottomSheetView.snp.remakeConstraints {
$0.bottom.leading.trailing.equalToSuperview()
$0.top.equalToSuperview().inset(self.view.frame.height - self.keyboardHeight - height)
func setupSheetHeightChanges(bottomHeight: CGFloat) {
if let sheet = self.sheetPresentationController {
sheet.animateChanges {
sheet.detents = [.custom(resolver: { _ in bottomHeight - 40 })]
}
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.dimmedBackView.backgroundColor = .black900.withAlphaComponent(0.5)
self.view.layoutIfNeeded()
})
}
}
}

// MARK: - Private Extensions

private extension ToasterBottomSheetViewController {
func setupInitialStyle(bottomType: BottomType, bottomTitle: String) {
self.view.backgroundColor = bottomType.color
self.titleLabel.textAlignment = bottomType.alignment
self.titleLabel.text = bottomTitle
}

func setupStyle() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

bottomSheetView.do {
$0.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
$0.makeRounded(radius: 20)
}

titleLabel.do {
$0.font = .suitBold(size: 18)
$0.textColor = .toasterBlack
}

closeButton.do {
$0.setImage(.icClose24, for: .normal)
$0.addAction(
UIAction { [weak self] _ in self?.dismiss(animated: true) },
for: .touchUpInside
)
}
}

func setupHierarchy() {
view.addSubviews(dimmedBackView, bottomSheetView)
bottomSheetView.addSubviews(titleLabel, closeButton, insertView)
view.addSubviews(titleLabel, closeButton, insertView)
}

func setupLayout() {
dimmedBackView.snp.makeConstraints {
$0.edges.equalToSuperview()
}

bottomSheetView.snp.makeConstraints {
$0.leading.trailing.bottom.equalToSuperview()
}

insertView.snp.makeConstraints {
$0.leading.trailing.bottom.equalToSuperview()
$0.top.equalToSuperview().inset(64)
Expand All @@ -159,47 +100,4 @@ private extension ToasterBottomSheetViewController {
$0.top.trailing.equalToSuperview().inset(20)
}
}

func setupDismissAction() {
// x 버튼 누를 때, 바텀시트를 내리는 Action Target
closeButton.addTarget(self, action: #selector(hideBottomSheetAction), for: .touchUpInside)

// 흐린 부분 탭할 때, 바텀시트를 내리는 TapGesture
let dimmedTap = UITapGestureRecognizer(target: self, action: #selector(hideBottomSheetAction))
dimmedBackView.addGestureRecognizer(dimmedTap)
dimmedBackView.isUserInteractionEnabled = true

// 아래로 스와이프 했을 때, 바텀시트를 내리는 swipeGesture
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(hideBottomSheetAction))
swipeGesture.direction = .down
view.addGestureRecognizer(swipeGesture)
}

func updateBottomSheetLayout() {
bottomSheetView.snp.remakeConstraints {
$0.bottom.leading.trailing.equalToSuperview()
$0.top.equalToSuperview().inset(self.view.frame.height - self.keyboardHeight - self.bottomHeight)
}

self.view.layoutIfNeeded()
}

@objc
func hideBottomSheetAction() {
hideBottomSheet()
}

@objc
func keyboardWillShow(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
keyboardHeight = keyboardSize.height
updateBottomSheetLayout()
}
}

@objc
func keyboardWillHide(_ notification: Notification) {
keyboardHeight = 100
updateBottomSheetLayout()
}
}
Loading

0 comments on commit cda0574

Please sign in to comment.