내가 배우는 이야기

Swift - 01. (2) 이미지 뷰어와 확대 축소(전구에 불 키기, 크기 조절하기) 본문

로봇화/맥

Swift - 01. (2) 이미지 뷰어와 확대 축소(전구에 불 키기, 크기 조절하기)

깨끗한눈빛 2019. 4. 18. 09:39

전구에 스위치로 불 키기. 확대, 축소 크기 조절 버튼

 

 

import UIKit

class ViewController: UIViewController {

    var isZoom = false //타입 선언없음: Bool type. false: 1
    var imgOff: UIImage?
    var imgOn: UIImage?
    
    @IBOutlet var imgView: UIImageView!
    @IBOutlet var btnText: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        imgOff = UIImage(named: "lamp_off.png")
        imgOn = UIImage(named: "lamp_on.png")
        imgView.image = imgOn
    }

    @IBAction func switchOnOff(_ sender: UISwitch) {
        if sender.isOn {
            imgView.image = imgOn
        }
        else {
            imgView.image = imgOff
        }
    }
    @IBAction func btnZoom(_ sender: Any) {
        let scale: CGFloat = 2.0 //Core Graphics
        var newWeight: CGFloat, newHeight: CGFloat
        
        if (isZoom) {
            newWeight = imgView.frame.width / scale
            newHeight = imgView.frame.height / scale
            btnText.setTitle("Zoom in", for: .normal) //UIButton은 text가 아니라 setTitle
            
        }
        else {
            newWeight = imgView.frame.width * scale
            newHeight = imgView.frame.height * scale
            btnText.setTitle("Zoom out", for: .normal)
            
        }
        isZoom = !isZoom
        imgView.frame.size = CGSize(width: newWeight, height: newHeight)
        
    }
    
}

 

 

전구이미지 출처 및 공부서적: Do it! 스위프트로 앱 만들기

Comments