ํํ ๋ชจ๋ฌ / ํ ์ด๋ธ ๋ทฐ ๊ตฌํ ์ฝ๋
//
// OrderSheetController.swift
// MacAProject
//
// Created by ahnzihyeon on 7/4/24.
//
import UIKit
import SwiftUI
import SnapKit
//MakeCell ํด๋์ค: ํ
์ด๋ธ๋ทฐ์
ํด๋์ค
class MakeCell: UITableViewCell {
let contentLabel = UILabel()
//์
์ ์ด๊ธฐํ ๋ฉ์๋
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(contentLabel)
contentLabel.snp.makeConstraints {
$0.edges.equalToSuperview().inset(10)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//์
์ ๋ค์ด๊ฐ ๋ด์ฉ
func configure(){
contentLabel.text = "Sample Text"
}
}
//TableViewController ํด๋์ค
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//ํ
์ด๋ธ๋ทฐ ์ธ์คํด์ค ์์ฑ
private let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = 100 //์
ํ๋ํ๋์ ๋์ด
//cell์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ์ฌ์ฉ ์๋ณ์๋ฅผ ํ
์ด๋ธ๋ทฐ์ ๋ฑ๋ก
tableView.register(MakeCell.self, forCellReuseIdentifier: "cellID")
//ํ
์ด๋ธ๋ทฐ ์คํ ๋ ์ด์์ ์ค์ ๋ฉ์๋ ํธ์ถ
setupTableviewConstraints()
paymentButton()
}
//๋ชจ๋ฌ ์์ ์๋ ๊ฒฐ์ ํ๊ธฐ ๋ฒํผ
func paymentButton(){
let orderListButton = UIButton()
orderListButton.setTitle("๊ฒฐ์ ํ๊ธฐ", for: .normal)
orderListButton.setTitleColor(.white, for: .normal)
orderListButton.backgroundColor = #colorLiteral(red: 0.2734747827, green: 0.1341301203, blue: 0.003133529332, alpha: 1)
orderListButton.titleLabel?.font = .boldSystemFont(ofSize: 24)
orderListButton.layer.cornerRadius = 15
self.view.addSubview(orderListButton)
orderListButton.snp.makeConstraints {
$0.bottom.equalToSuperview().inset(30)
$0.height.equalTo(65)
$0.width.equalTo(350)
$0.centerX.equalToSuperview()
}
orderListButton.addTarget(self, action: #selector(paymentSuccessAlert), for: .touchDown)
}
@objc
func paymentSuccessAlert() {
let alert = UIAlertController(title: "๊ฒฐ์ ์ฑ๊ณต", message: "์ฃผ๋ฌธ์ด ์๋ฃ๋์์ต๋๋ค.", preferredStyle: .alert)
let closeAction = UIAlertAction(title: "๋ซ๊ธฐ", style: .default, handler: nil)
alert.addAction(closeAction)
//alert์ฐฝ ๋์ฐ๊ธฐ
self.present(alert, animated: true, completion: nil)
}
//ํ
์ด๋ธ๋ทฐ ์คํ ๋ ์ด์์ ์ค์
func setupTableviewConstraints(){
view.addSubview(tableView)
tableView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return cartArray.count //์์ง ๊ฐ์ ๋ฐ์ ์ค์ง ์์๊ธฐ ๋๋ฌธ์ ์ผ๋จ ์ฃผ์ -> ๋ฆฌํด๊ฐ์ ํ
์ด๋ธ๋ทฐ์ ์ ๋ฌ
return 7
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// ์
์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ํ์์ ๊บผ๋ด์ ์บ์คํ
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as? MakeCell else { //MakeCell์ ํ์
์บ์คํ
return UITableViewCell()
}
cell.configure() // ์
์ ๊ตฌ์ฑํ๋ ๋ฉ์๋ ํธ์ถ
cell.selectionStyle = .none // ์
์ ํ ์ ์์ ๋ณํ์ง ์๊ฒ ์ค์
return cell
}
}
//ViewController ์ญํ
class OrderSheetController: UIViewController {
//ํ์ ์๋ ์ฃผ๋ฌธํ๊ธฐ ๋ฒํผ
func paymentButton(){
let orderListButton = UIButton()
orderListButton.setTitle("์ฃผ๋ฌธ ๋ด์ญ", for: .normal)
orderListButton.setTitleColor(.white, for: .normal)
orderListButton.backgroundColor = #colorLiteral(red: 0.2734747827, green: 0.1341301203, blue: 0.003133529332, alpha: 1)
orderListButton.titleLabel?.font = .boldSystemFont(ofSize: 24)
orderListButton.layer.cornerRadius = 15
self.view.addSubview(orderListButton)
orderListButton.snp.makeConstraints {
$0.bottom.equalToSuperview().inset(30)
$0.height.equalTo(65)
$0.width.equalTo(350)
$0.centerX.equalToSuperview()
}
orderListButton.addTarget(self, action: #selector(ShowOderList), for: .touchDown)
}
@objc
func ShowOderList(){
let tvc = TableViewController()
if let orderSheet = tvc.sheetPresentationController {
orderSheet.detents = [.medium()]
orderSheet.preferredCornerRadius = 20
/// ์๋จ -- ๋ถ์ฌ ์ก๋ ๋ถ๋ถ ๋ณด์ด๊ฒ ํ ๊ฒ์ธ์ง!
orderSheet.prefersGrabberVisible = true
}
self.present(tvc, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
paymentButton()
}
}
struct PreView: PreviewProvider {
static var previews: some View {
OrderSheetController().toPreview()
}
}
#if DEBUG
extension UIViewController {
private struct Preview: UIViewControllerRepresentable {
let viewController: UIViewController
func makeUIViewController(context: Context) -> UIViewController {
return viewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
func toPreview() -> some View {
Preview(viewController: self)
}
}
#endif
ํ ์คํธ๋ฉ์์ง ์ถ๊ฐ
//
// OrderSheetController.swift
// MacAProject
//
// Created by ahnzihyeon on 7/4/24.
//
import UIKit
import SwiftUI
import SnapKit
//MakeCell ํด๋์ค: ํ
์ด๋ธ๋ทฐ์
ํด๋์ค
class MakeCell: UITableViewCell {
let contentLabel = UILabel()
//์
์ ์ด๊ธฐํ ๋ฉ์๋
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(contentLabel)
contentLabel.snp.makeConstraints {
$0.edges.equalToSuperview().inset(10)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//์
์ ๋ค์ด๊ฐ ๋ด์ฉ
func configure(){
contentLabel.text = "Sample Text"
}
}
//TableViewController ํด๋์ค
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//ํ
์ด๋ธ๋ทฐ ์ธ์คํด์ค ์์ฑ
private let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = 100 //์
ํ๋ํ๋์ ๋์ด
//cell์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ์ฌ์ฉ ์๋ณ์๋ฅผ ํ
์ด๋ธ๋ทฐ์ ๋ฑ๋ก
tableView.register(MakeCell.self, forCellReuseIdentifier: "cellID")
//ํ
์ด๋ธ๋ทฐ ์คํ ๋ ์ด์์ ์ค์ ๋ฉ์๋ ํธ์ถ
setupTableviewConstraints()
paymentButton()
}
//๋ชจ๋ฌ ์์ ์๋ ๊ฒฐ์ ํ๊ธฐ ๋ฒํผ
func paymentButton(){
let orderListButton = UIButton()
orderListButton.setTitle("๊ฒฐ์ ํ๊ธฐ", for: .normal)
orderListButton.setTitleColor(.white, for: .normal)
orderListButton.backgroundColor = #colorLiteral(red: 0.2734747827, green: 0.1341301203, blue: 0.003133529332, alpha: 1)
orderListButton.titleLabel?.font = .boldSystemFont(ofSize: 24)
orderListButton.layer.cornerRadius = 15
self.view.addSubview(orderListButton)
orderListButton.snp.makeConstraints {
$0.bottom.equalToSuperview().inset(30)
$0.height.equalTo(65)
$0.width.equalTo(350)
$0.centerX.equalToSuperview()
}
orderListButton.addTarget(self, action: #selector(paymentSuccessAlert), for: .touchDown)
}
@objc
func paymentSuccessAlert() {
let alert = UIAlertController(title: "๊ฒฐ์ ์ฑ๊ณต", message: "์ฃผ๋ฌธ์ด ์๋ฃ๋์์ต๋๋ค.", preferredStyle: .alert)
// let closeAction = UIAlertAction(title: "๋ซ๊ธฐ", style: .default, handler: nil)
let closeAction = UIAlertAction(title: "๋ซ๊ธฐ", style: .default) { (action) in
// ํ์ฌ ํ๋ฉด์ ๋ซ๋ ๋์์ ์ฌ๊ธฐ์ ์คํ
self.dismiss(animated: false, completion: nil)
}
alert.addAction(closeAction)
//alert์ฐฝ ๋์ฐ๊ธฐ
self.present(alert, animated: true, completion: nil)
}
//ํ
์ด๋ธ๋ทฐ ์คํ ๋ ์ด์์ ์ค์
func setupTableviewConstraints(){
view.addSubview(tableView)
tableView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return cartArray.count //์์ง ๊ฐ์ ๋ฐ์ ์ค์ง ์์๊ธฐ ๋๋ฌธ์ ์ผ๋จ ์ฃผ์ -> ๋ฆฌํด๊ฐ์ ํ
์ด๋ธ๋ทฐ์ ์ ๋ฌ
return 7
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// ์
์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ํ์์ ๊บผ๋ด์ ์บ์คํ
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as? MakeCell else { //MakeCell์ ํ์
์บ์คํ
return UITableViewCell()
}
cell.configure() // ์
์ ๊ตฌ์ฑํ๋ ๋ฉ์๋ ํธ์ถ
cell.selectionStyle = .none // ์
์ ํ ์ ์์ ๋ณํ์ง ์๊ฒ ์ค์
return cell
}
}
//ViewController ์ญํ
class OrderSheetController: UIViewController {
//ํ์ ์๋ ์ฃผ๋ฌธํ๊ธฐ ๋ฒํผ
func paymentButton(){
let orderListButton = UIButton()
orderListButton.setTitle("์ฃผ๋ฌธ ๋ด์ญ", for: .normal)
orderListButton.setTitleColor(.white, for: .normal)
orderListButton.backgroundColor = #colorLiteral(red: 0.2734747827, green: 0.1341301203, blue: 0.003133529332, alpha: 1)
orderListButton.titleLabel?.font = .boldSystemFont(ofSize: 24)
orderListButton.layer.cornerRadius = 15
self.view.addSubview(orderListButton)
orderListButton.snp.makeConstraints {
$0.bottom.equalToSuperview().inset(30)
$0.height.equalTo(65)
$0.width.equalTo(350)
$0.centerX.equalToSuperview()
}
orderListButton.addTarget(self, action: #selector(ShowOderList), for: .touchDown)
}
@objc
func ShowOderList(){
let tvc = TableViewController()
if let orderSheet = tvc.sheetPresentationController {
orderSheet.detents = [.medium()]
orderSheet.preferredCornerRadius = 20
/// ์๋จ -- ๋ถ์ฌ ์ก๋ ๋ถ๋ถ ๋ณด์ด๊ฒ ํ ๊ฒ์ธ์ง!
orderSheet.prefersGrabberVisible = true
}
self.present(tvc, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
paymentButton()
showToast()
}
func showToast() {
let toastLabel = UILabel()
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.7)
toastLabel.textColor = UIColor.white
toastLabel.font = UIFont.systemFont(ofSize: 17.0)
toastLabel.textAlignment = .center
toastLabel.text = "์ฅ๋ฐ๊ตฌ๋์ ๋ฉ๋ด๋ฅผ ์ถ๊ฐํ์ต๋๋ค"
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 7
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 0.9, delay: 0.6, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
toastLabel.snp.makeConstraints {
$0.center.equalToSuperview()
$0.width.equalTo(280)
$0.height.equalTo(50)
}
}
}