๐ŸŽ iOS/UI

[iOS/UI] ์ด๋ฏธ์ง€ ์คŒ์œผ๋กœ ํ™•๋Œ€/์ถ•์†Œํ•˜๊ธฐ(feat. ์Šคํฌ๋กค๋ทฐ)

Fomagran ๐Ÿ’ป 2021. 11. 3. 17:43
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜ ์•Œ์•„๋ณผ ๊ฒƒ์€ ์ด๋ฏธ์ง€๋ฅผ ํ™•๋Œ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Storyboard

 

๋ ˆ์ด์•„์›ƒ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์Šคํฌ๋กค๋ทฐ์— ์ด๋ฏธ์ง€๋ทฐ๊ฐ€ ํฌํ•จ๋˜๊ฒŒ ํ•ด์ฃผ์„ธ์š”.

 


ViewController

 

๋จผ์ € ์ด๋ฏธ์ง€๋ทฐ์™€ ์Šคํฌ๋กค๋ทฐ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ์„ธ์š”.

 

  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var scrollView: UIScrollView!

 

์ดˆ๊ธฐ ์„ธํŒ…์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ฃผ์„ธ์š”.

 

1. ์ด๋ฏธ์ง€๋ทฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์›ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

2. ์Šคํฌ๋กค๋ทฐ์˜ ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ํ˜„์žฌ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”. (์•„์ง ์˜ค๋ฅ˜๊ฐ€ ๋‚ ๊ฑฐ์—์š”.)

 

3. ์Šคํฌ๋กค๋ทฐ์˜ ์ฒ˜์Œ ์คŒ์Šค์ผ€์ผ์„ ์ •ํ•ด์ค๋‹ˆ๋‹ค. (๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด 1.0์ž…๋‹ˆ๋‹ค.)

 

4. ์Šคํฌ๋กค๋ทฐ์˜ ์ตœ์†Œ ์คŒ์Šค์ผ€์ผ์„ ์ •ํ•ด์ค๋‹ˆ๋‹ค. (์คŒ์„ ํ•ด์„œ ์ถ•์†Œํ•  ๋•Œ ์ด ์ดํ•˜๋กœ๋Š” ์ค„์–ด๋“ค์ง€ ์•Š๊ฒŒ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

 

5. ์Šคํฌ๋กค๋ทฐ์˜ ์ตœ๋Œ€ ์คŒ์Šค์ผ€์ผ์„ ์ •ํ•ด์ค๋‹ˆ๋‹ค. (์คŒ์„ ํ•ด์„œ ํ™•๋Œ€ํ•  ๋•Œ ์ด ์ด์ƒ์€ ํ™•๋Œ€๋Œ€์ง€ ์•Š๊ฒŒ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

func configure() {
        imageView.image = UIImage(named: "์Šคํฐ์ง€๋ฐฅ.png") //1
        scrollView.delegate = self //2
        scrollView.zoomScale = 1.0 //3
        scrollView.minimumZoomScale = 1.0 //4
        scrollView.maximumZoomScale = 2.0 //5
    }

 

์œ„์—์„œ ์Šคํฌ๋กค๋ทฐ ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ํ˜„์žฌ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์„ค์ •ํ–ˆ์œผ๋‹ˆ ํ™•์žฅํ•ด์„œ ํ•ด๋‹น ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ฑ„ํƒํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  UIScrollViewDelegate์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ viewForZooming์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ์ด๋ฏธ์ง€๋ทฐ๋กœ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

//MARK:- UIScrollViewDelegate

extension ViewController:UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
         return self.imageView
     }
}

 

(viewForZooming์€ ์Šคํฌ๋กค๋ทฐ์—์„œ ํ•€์น˜ ์คŒ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์–ด๋–ค ๋ทฐ๋ฅผ ํ™•๋Œ€/์ถ•์†Œ๊ฐ€ ํ• ์ง€ ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

 


์‹คํ–‰ ํ™”๋ฉด

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ•€์น˜ ์คŒ์œผ๋กœ ํ™•๋Œ€/์ถ•์†Œ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

(์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ์—์„œ ํ•€์น˜ ์คŒ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Optionํ‚ค๋ฅผ ๋ˆ„๋ฅด๊ณ  ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.)

 

 

 

 

ํ•˜์ง€๋งŒ ๋ฌธ์ œ์ ์ด ํ•œ๊ฐ€์ง€ ์žˆ์—ˆ๋Š”๋ฐ minimumZoomScale๊ณผ maximumZoomScale์„ ์ •ํ•ด๋†“์•„๋„

 

๊ทธ ์ดํ•˜,์ด์ƒ์œผ๋กœ ์ถ•์†Œ/ํ™•๋Œ€๊ฐ€ ๋˜๊ณ  ํ•œ๋ฒˆ ํƒญ์„ ํ•˜๋ฉด ์ตœ์†Œ,์ตœ๋Œ€ ์คŒ์Šค์ผ€์ผ๋กœ ๋Œ์•„๊ฐ”์Šต๋‹ˆ๋‹ค.

 

์ €๋Š” ์•„์˜ˆ ๊ทธ ์ดํ•˜,์ด์ƒ์œผ๋กœ ์ถ•์†Œ/ํ™•๋Œ€๊ฐ€ ์•ˆ๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ scrollViewDidScroll ๋ฉ”์„œ๋“œ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.zoomScale <= 1.0 {
            scrollView.zoomScale = 1.0
        }
        
        if scrollView.zoomScale >= 2.0 {
            scrollView.zoomScale = 2.0
        }
    }

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๋ฌด๋ฆฌ ํ™•๋Œ€ํ•ด๋„ ์คŒ์Šค์ผ€์ผ์ด 2.0 ์ด์ƒ ํ™•๋Œ€๋˜์ง€ ์•Š๊ณ  ์•„๋ฌด๋ฆฌ ์ถ•์†Œํ•ด๋„ 1.0 ์ดํ•˜๋กœ ์ถ•์†Œ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


Source Code

 

728x90
๋ฐ˜์‘ํ˜•