이미지뷰( Image View ) : 앱에서 사진을 보여 줘야 할 때 사용하는 객체
viewDidLoad 함수 ?
: 내가 만든 뷰를 불러왔을 때 호출되는 함수로, 부모 클래스인 UIViewController 클래스에 선언되있다 .
뷰가 불려진 후 실행하고자 하는 기능이 필요할 때 이 viewDidLoad 함수내에 코드를 입력하면 된다 !
import UIKit
class ViewController: UIViewController {
var isZoom = false
var imgOn : UIImage?
var imgOff : UIImage?
@IBOutlet var ImgView: UIImageView!
@IBOutlet var btnResize: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgOn = UIImage(named: "lamp_on.png") // imgOn 에 이미지를 할당
imgOff = UIImage(named: "lamp_off.png")
ImgView.image = imgOn
}
@IBAction func btnResizeimage(_ sender: UIButton) {
// CGFloat : xcode에서 float을 재정의해 놓은 자료형으로, float 과 같다고 생각하면됌
// 확대버튼 클릭시 2배확대할것 이므로 2.0으로 설정
let scale : CGFloat = 2.0
var newWidth:CGFloat,newHeight:CGFloat
if(isZoom) { // true
// 이미지 프레임의 가로,세로크기에 scale 값을 나누어 가로 세로에 할당
newWidth = ImgView.frame.width/scale
newHeight = ImgView.frame.height/scale
// 버튼의 택스트를 "확대"로 변경
btnResize.setTitle("확대", for: .normal)
}
else { // false
// 이미지프레임의 가로 세로크기에 scale값을 곱하여 가로 세로에 항당
newWidth = ImgView.frame.width*scale
newHeight = ImgView.frame.height*scale
// 버튼의 텍스트를 "축소"로 변경
btnResize.setTitle("축소", for: .normal)
}
//CGSize 메서드를 이용하여 이미지뷰의 프레임 크기를 변경
ImgView.frame.size = CGSize(width: newWidth, height: newHeight)
isZoom = !isZoom
}
@IBAction func switchImageOnOff(_ sender: UISwitch) {
if sender.isOn {
ImgView.image = imgOn
}
else {
ImgView.image = imgOff
}
}
}
-
-
확대 및 축소시 이미지 확대 및 축소,
스위치 버튼 누를시 램프 on/off
'iOS' 카테고리의 다른 글
#2 Swift/ios - 1장 (0) | 2022.12.19 |
---|---|
#1 Swift 시작 (0) | 2022.12.19 |