๊ฐ๊ฐ์ด ๋ค๊ฐ์ค๊ณ ์๋ค...๐ซ (์์ง ๋ ๋์์ผํ๋๋ฐ..)
์ด๋ฒ ๋ฐฉํ ๋ญ๊ฐ ํ๊ฒ ์๋๊ฒ ๊ฐ์ ์ด๋๋ ๋ญ์ง..
์ด๋ฐ ์๊ฐํ ์๊ฐ์ ๊ณต๋ถ๋ ํด์ผ์ง..
์ค๋ ๊ณต๋ถํ ๊ฒ์ ํฐ์น ์ด๋ฒคํธ๋ฅผ ์ด์ฉํ์ฌ ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์๋ ํ๋ฉด์ ๊ตฌํ์์ต๋๋ค.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var imgView: UIImageView!
@IBOutlet var tfLineWidth: UITextField!
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.
tfLineWidth.delegate = self
}
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: lastPoint.x, y: lastPoint.y))
//์ด์ ์ ์์ง์ธ ์์น๋ฅผ ์์ ์์น๋ก ์ง์
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currPoint.x, y: currPoint.y))
//๋ง์ง๋ง ์์น์์ ํ์ฌ ์์น๊น์ง ์ ์ ์์ฑ
UIGraphicsGetCurrentContext()?.strokePath()
imgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
lastPoint = currPoint//ํ์ฌ์์น๋ฅผ ๋ง์ง๋ง ์์น๋ก ์ง์
}
//ํฐ์นํ์ฑ๋ก ์์ง์ด๋ฉด ์์ ํจ์๊ฐ ๊ณ์ ์คํ์ด๋๋ค.
//์์ง์ด๋ฉด์ ์ ์ด ๋๊ธฐ์ง ์๋๋ก ๋ณด์ฌ์ค์ผ ํ๊ธฐ ๋๋ฌธ์
//currentPoint์ ์ขํ๋ก lastpoint๋ก ๊ฐฑ์ ์์ผ์ฃผ๋ ๊ฒ์ด๋ค.
override func touchesEnded(_ 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: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currPoint.x, y: currPoint.y))
UIGraphicsGetCurrentContext()?.strokePath()
imgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
//ํฐ์น๋ฅผ ๋๋ผ๋ ์คํ์ด๋๋ค.
//๋๋ ๋ ํ๋ฒ๋ง ์คํํ๊ธฐ ๋๋ฌธ์ lastPoint๋ฅผ currPoint๋ก ๊ฐฑ์ ํ ํ์ ์๋ค.
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
imgView.image = nil
}
}
//ํ๋ค๋ฉด ํ๋ฉด์ด ์ง์์ง๋ค.
@IBAction func btnClearImgView(_ sender: UIButton) {
imgView.image = nil
}
//ํ๋ฉด์ ๊ทธ๋ฆฐ๊ฒ๋ค ์ ๊ฑฐํ๋ ๋ฒํผ
@IBAction func btnChageColorBlack(_ sender: Any) {
lineColor = UIColor.black.cgColor
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
}
@IBAction func btnChangeColorYellow(_ sender: Any) {
lineColor = UIColor.yellow.cgColor
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
}
@IBAction func btnChangeColotRed(_ sender: Any) {
lineColor = UIColor.red.cgColor
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor)
}
//์์ ์๊น์ ๋ฐ๊พธ๋ ๋ฒํผ ๋ค
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let width = Float(tfLineWidth.text!)
//ํ
์คํธํ๋์ ๊ฐ์ string์์ floatํ์ผ๋ก ๋ณํ
lineSize = CGFloat(width!)
//ํ์
์ CGFloatํ์ผ๋ก ๋ณํ
UIGraphicsGetCurrentContext()?.setLineWidth(lineSize)
return true
}
//textField์์ return์ ๋๋ฅด๋ฉด ๋ฐํ์ด ๋๋ ํจ์
}
๊ฒฐ๊ณผ๋ฌผ๐
TextField์์ ์
๋ ฅ ํ return ๊ฐ์ ๋๋ ์ ๋ ์ด๋ฒคํธ๊ฐ ์คํ๋๊ฒ ํ๊ธฐ ์ํด์๋
UITextFieldDelegate๋ฅผ ์์๋ฐ์์ผ ํฉ๋๋ค.