[ 2022-08-16 ๐Ÿซ  TIL ]

Burkeyยท2022๋…„ 8์›” 16์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
14/157

๊ฐœ๊ฐ•์ด ๋‹ค๊ฐ€์˜ค๊ณ  ์žˆ๋‹ค...๐Ÿซ (์•„์ง ๋” ๋†€์•„์•ผํ•˜๋Š”๋ฐ..)
์ด๋ฒˆ ๋ฐฉํ•™ ๋ญ”๊ฐ€ ํ•œ๊ฒŒ ์—†๋Š”๊ฒƒ ๊ฐ™์€ ์ด๋Š๋‚Œ ๋ญ์ง€..

์ด๋Ÿฐ ์ƒ๊ฐํ•  ์‹œ๊ฐ„์— ๊ณต๋ถ€๋‚˜ ํ•ด์•ผ์ง€..

์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ์€ ํ„ฐ์น˜ ์ด๋ฒคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์„ ๊ตฌํ˜„์˜€์Šต๋‹ˆ๋‹ค.

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๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€