์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[์ฝ”๋“œ๋ฒ ์ด์ŠคUI] ํ‚ค์˜ค์Šคํฌ ์•ฑ ํ”„๋กœ์ ํŠธ 3์ผ์ฐจ - ํ•˜ํ”„๋ชจ๋‹ฌ/ํ…Œ์ด๋ธ”๋ทฐ/ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๊ตฌํ˜„ ์ฝ”๋“œ ์ค‘๊ฐ„ ๋ฐฑ์—…

๋ณธ๋ฌธ

ํ•˜ํ”„ ๋ชจ๋‹ฌ / ํ…Œ์ด๋ธ” ๋ทฐ ๊ตฌํ˜„ ์ฝ”๋“œ

//
//  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)
        }
    }
    
}

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ