기능
- 여러개의 리스트로 된 이미지들을 하나씩 이미지 뷰어를 통해 보여준다.
- 이전 버튼을 클릭 시 이전 이미지로, 다음 버튼 클릭 시 다음 이미지로 간다.
- 축소, 확대 버튼 클릭 시 그에 해당하는 명령을 수행한다.
코드
import UIKit
class ViewController: UIViewController {
var numImage = 1
var maxImage = 6
var isZoom = false
@IBOutlet var imgView: UIImageView!
@IBOutlet var nextButton: UIButton!
@IBOutlet var previousButton: UIButton!
@IBOutlet var resizeButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
imgView.image = UIImage(named: "1.png")
}
@IBAction func touchUpPreviousBtn(_ sender: UIButton) {
if numImage <= 1{
print("이전으로 돌아갈 수 없습니다.")
return
}else{
numImage = numImage - 1
let preImage: String = String(numImage) + ".png"
imgView.image = UIImage(named: preImage)
}
}
@IBAction func touchUpNextBtn(_ sender: UIButton) {
if numImage >= maxImage{
print("다음으로 갈 수 없습니다.")
return
}else{
numImage = numImage + 1
let nxtImage: String = String(numImage) + ".png"
imgView.image = UIImage(named: nxtImage)
}
}
@IBAction func touchUpRisizeButton(_ sender: UIButton) {
let scale : CGFloat = 2.0
var width : CGFloat
var height : CGFloat
if isZoom{
resizeButton.setTitle("확대", for: .normal)
width = imgView.frame.width / scale
height = imgView.frame.height / scale
}else{
resizeButton.setTitle("축소", for: .normal)
width = imgView.frame.width * scale
height = imgView.frame.height * scale
}
isZoom.toggle()
imgView.frame.size = CGSize(width: width, height: height)
}
}
- 현재 이미지의 번호를 저장하는 변수와, 최대 이미지의 개수를 저장하는 변수를 선언한다.
- 이전 버튼을 클릭시
UIImage(name:)
메소드를 이용해 이미를 바꾸고, 이후 버튼을 클릭시 마찬가지로 바꾼다.
- 확대 축소 버튼을 클릭시
CGFloat
타입의 변수를 선언하고, imageView.frame.size = CGSize()
메소드에 인자로 전달해 크기를 조절한다.