그래픽 라이브러리를 활용한 스케치 어플을 만들어 보았다.
import UIKit
class ViewController: UIViewController {
@IBOutlet var imgView: UIImageView!
var lastPoint: CGPoint! //터치하거나, 이동한 위치
var lineSize:CGFloat = 2.0 //선 두깨
var lineColor = UIColor.red.cgColor //선 색상
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//버튼을 눌렀을 때 이미지 뷰 초기화
@IBAction func btnClearImageView(_ sender: UIButton) {
imgView.image = nil
}
//터치가 시작되었을 때
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
lastPoint = touch.location(in: imgView)
}
//터치된 위치가 움직였을 때
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
UIGraphicsBeginImageContext(imgView.frame.size)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round) //라인 끝 모양을 라운드로 설정
UIGraphicsGetCurrentContext()?.setLineWidth(lineSize)
let touch = touches.first! as UITouch //발생한 터치 이벤트 가져옴
let currPoint = touch.location(in: imgView) //터치된 위치 설정
imgView.image?.draw(in: CGRect(x: 0, y: 0, width: imgView.frame.size.width, height: imgView.frame.size.height)) //이미지 뷰 크기로 그림
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: currPoint.x, y: currPoint.y)) //시작위치 이동
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currPoint.x, y: currPoint.y)) //현재위치까지 선 추가
UIGraphicsGetCurrentContext()?.strokePath()
imgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
lastPoint = currPoint //변수 할당
}
//터치가 끝났을 때
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
UIGraphicsBeginImageContext(imgView.frame.size)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(lineSize)
imgView.image?.draw(in: CGRect(x: 0, y: 0, width: imgView.frame.size.width, height: imgView.frame.size.height))
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.strokePath()
imgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake { //폰을 흔들었을 때
imgView.image = nil //이미지 뷰 초기화
}
}
}