๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UIKit

[iOS/Swift] ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ, ๊ทธ๋ฆผ์ž๊ฐ€ ์ ์šฉ๋œ ํŒ์—… ๋ทฐ ๊ตฌํ˜„ํ•˜๊ธฐ

by Danna 2021. 4. 28.
728x90
728x90

๐Ÿ’ก [iOS/Swift] ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ, ๊ทธ๋ฆผ์ž๊ฐ€ ์ ์šฉ๋œ ํŒ์—… ๋ทฐ ๊ตฌํ˜„ํ•˜๊ธฐ

Zerobin ์•ฑ์—์„œ ์™ผ์ชฝ ํ™”๋ฉด์€ ๋ฆฌ๋ทฐ์ž‘์„ฑ์‹œ ์ƒˆ์‹น์Šคํƒฌํ”„๋ฅผ ๋ˆ„๋ฅด๋Š” ํ™”๋ฉด์ด๊ณ , ์˜ค๋ฅธ์ชฝ ํ™”๋ฉด์€ ์ƒˆ์‹น ์Šคํƒฌํ”„์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ™”๋ฉด์ž…๋‹ˆ๋‹ค.

 

์•ฑ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ์™ผ์ชฝํ™”๋ฉด์˜ โ“˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒ์—… ๋ทฐ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋„๋ก ๊ฐœ๋ฐœํ•ด์•ผ ํ–ˆ๊ณ , ์ด ๊ณผ์ •์—์„œ ์ƒ๊ฒผ๋˜ ๋ฌธ์ œ๋“ค์„ ๋‹ค๋ค„๋ณผ๊ฒŒ์š”! ์„ค๋ช…์„ ์œ„ํ•ด์„œ ์™ผ์ชฝํ™”๋ฉด์€ MainVC,  โ“˜ ๋ฒ„ํŠผ์€ infoButton, ์˜ค๋ฅธ์ชฝ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚œ ํŒ์—…ํ™”๋ฉด์„ PopupVC ๋ผ๊ณ  ์ง€์นญํ• ๊ฒŒ์š”.

 

๋จผ์ €, MainVC ์—์„œ PopupVC ๋ฅผ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ณ  ํŒ์—…๋ทฐ์—์„œ View ์˜ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ทธ๋ฆผ์ž๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‚˜๋ˆ ์„œ ์ž‘์„ฑํ• ๊ฒŒ์š”. 

 

โœ”๏ธŽ ๋ฉ”์ธํ™”๋ฉด์—์„œ ํŒ์—… ๋ทฐ๋ฅผ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•

1. ๋จผ์ € ๋‘ ํ™”๋ฉด์„ ์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

 

MainVC ๋Š” ํŽธํ•œ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜์‹œ๋ฉด ๋˜์ง€๋งŒ, ํŒ์—… ๋ทฐ๋ฅผ ๋„์šฐ๊ธฐ ์œ„ํ•œ ๋ฒ„ํŠผ ํ•˜๋‚˜๋Š” ์žˆ์–ด์•ผ๊ฒ ์ฃ !?

PopupVC ๋Š” ๊ฐ€์šด๋ฐ์— infoView ๋ฅผ ๋‘๊ณ , ๋’ค์˜ ๋ฐฐ๊ฒฝ์€ ์–ด๋‘์šด ์ƒ‰์œผ๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

  • MainVC = Review Stamp View Controller
  • PopupVC = Stamp Info View Controller

 

2. MainVC ์—์„œ infoButton ์ด ํด๋ฆญ๋˜์—ˆ์„ ๋•Œ PopupVC ๋ฅผ overFullScreen ์œผ๋กœ ๋„์›Œ์ค๋‹ˆ๋‹ค.

 

modalPresentationStyle ์„ overFullScreen ์œผ๋กœ ์„ค์ •ํ•˜๊ณ , present ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ทฐ๋ฅผ ๋„์›Œ์ค€๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

.overFullScreen ์œผ๋กœ ์„ค์ •ํ•œ ์ด์œ ๋Š” MainVC ํ™”๋ฉด์„ ์œ ์ง€ํ•˜๊ณ  ๊ทธ ์œ„์— PopupVC ๋ฅผ ๋„์›Œ์ฃผ๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค.

.fullScreen ์œผ๋กœ ์„ค์ •ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด, MainVC ๋Š” ๋ทฐ๊ณ„์ธต์—์„œ ์‚ฌ๋ผ์ง€๊ณ , PopupVC ๋งŒ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค..

 

๐Ÿ‘€ ์šฉ์–ด์ •๋ฆฌ!
modalPresentationStyle :: View Controller ๊ฐ€ modal ํ˜•์‹์œผ๋กœ ๋„์›Œ์งˆ ๋•Œ์˜ ์Šคํƒ€์ผ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์ด๋‹ค.
modalPresentationStyle ์€ iOS13 ์—์„œ ๊ธฐ๋ณธ์ ์ธ ํ™”๋ฉด ์ „ํ™˜ ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๊ณ , ๊ธฐ์กด view ๊ฐ€ view ๊ณ„์ธต์—์„œ ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ , ์ƒˆ๋กœ์šด view ๊ฐ€ ๋– ์žˆ๋Š” ํ˜•์‹์ด๋‹ค.
modalPresentationStyle ์†์„ฑ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…๊ฐ’์€ 11๊ฐ€์ง€๋กœ ๋‹ค์Œ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋‹ค.
class MainVC: UIViewController {
    // ...
    // MARK: - Action

    @IBAction func infoButtonTapped(_ sender: Any) {
        let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
        let popupVC = storyBoard.instantiateViewController(withIdentifier: "PopupVC")
        popupVC.modalPresentationStyle = .overFullScreen
        present(popupVC, animated: false, completion: nil)
    }
}

 

โœ”๏ธŽ ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ, ๊ทธ๋ฆผ์ž๊ฐ€ ์ ์šฉ๋œ ๋ทฐ ๊ตฌํ˜„ํ•˜๊ธฐ

* ์ผ๋ฐ˜์ ์œผ๋กœ View์— ๊ทธ๋ฆผ์ž๋ฅผ ์ ์šฉํ•  ๋•Œ์—๋Š” ๋ณ„๋„์˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ, View์˜ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ๋งŒ๋“ ๊ฒฝ์šฐ ๊ทธ๋ฆผ์ž๋ฅผ ์ ์šฉํ•˜๋ฉด ๊ทธ๋ฆผ์ž๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

1. ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ ๊ทธ๋ฆผ์ž๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ containerView ๋ฅผ ๋งŒ๋“ ๋‹ค.

  • ๊ทธ๋ฆผ์ž๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” shadowColor, shadowOffset, shadowRadius, shadowOpacity ๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค!
  • shadowColor :: ๊ทธ๋ฆผ์ž ์ƒ‰์ƒ, shadowOffset :: ๊ทธ๋ฆผ์ž์˜ ์œ„์น˜
  • shadowRadius :: ๊ทธ๋ฆผ์ž ๊ฒฝ๊ณ„์˜ ์„ ๋ช…๋„, shadowOpacity :: ๊ทธ๋ฆผ์ž ํˆฌ๋ช…๋„

 

2. infoView (๊ธฐ์กด๋ทฐ) ์˜ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋‘ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  • UIView.layer.cornerRadius ์†์„ฑ์„ ์›ํ•˜๋Š” radius ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • UIView.clipsToBounds ์†์„ฑ์„ true ๋กœ ์„ค์ •ํ•ด ๋ ˆ์ด์–ด ๋ฐ”๊นฅ ๋ถ€๋ถ„์„ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. 

 

3. containerView ์— infoView ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

  • 1๋ฒˆ ๊ณผ์ •์—์„œ containerView ๋ฅผ ๋งŒ๋“  ํ›„์— ์ถ”๊ฐ€ํ•ด๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค!

 

4. containerView ์˜ AutoLayout ์„ ์ง€์ •ํ•œ๋‹ค.

  • infoView ์— AutoyLayout ์„ ์ง€์ •ํ–ˆ๋”๋ผ๋„, containerView ํ•˜์œ„ ๋ทฐ๋กœ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— AutoLayout ์„ค์ •์„ ๋‹ค์‹œ ํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ €๋Š” ์ด๋ถ€๋ถ„์—์„œ AutoLayout ์„ค์ •์„ ๋†“์ณ์„œ ํ—ค๋งธ์—ˆ์–ด์š”! ๐Ÿ˜ข

 

์ถ”๊ฐ€์ ์œผ๋กœ, PopupVC ๋ฅผ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ„ํŠผ์ด๋‚˜ Tap Gesture ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค~ 

class PopupVC: UIViewController {

    @IBOutlet weak var infoView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // shadow ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ containerView
        let containerView = UIView()
        containerView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
        containerView.layer.shadowOffset = CGSize(width: 1, height: 4)
        containerView.layer.shadowRadius = 10
        containerView.layer.shadowOpacity = 1
        containerView.addSubview(infoView)
        
        // infoView ๋ชจ์„œ๋ฆฌ ๋‘ฅ๊ธ€๊ฒŒ ๋งŒ๋“ค๊ธฐ
        infoView.layer.cornerRadius = 25
        infoView.clipsToBounds = true
        view.addSubview(containerView)
        
        // containerView ์— ๋Œ€ํ•ด Auto Layout ์„ค์ •
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -10).isActive = true
        containerView.widthAnchor.constraint(equalToConstant: 315).isActive = true
        containerView.heightAnchor.constraint(equalToConstant: 367).isActive = true
    }
    
    @IBAction func closeButtonTapped(_ sender: Any) {
        self.dismiss(animated: false, completion: nil)
    }
}

 


์ฐธ๊ณ  ๋ฌธ์„œ

 

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

728x90
728x90