๐ŸŽ iOS/UI

[iOS/UI] UIBezierPath์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ (feat. ๋ชจ์–‘,๊ณก์„ ,์ง์„  ๊ทธ๋ฆฌ๊ธฐ)

Fomagran ๐Ÿ’ป 2022. 3. 21. 11:45
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ์›ํ•˜๋Š” ๋ชจ์–‘์„ ์ง์ ‘ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” UIBezierPath์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์ „๋ถ€ํ„ฐ ๋‹ค๋ฃจ๊ณ  ์‹ถ์€ ์ฃผ์ œ์˜€๋Š”๋ฐ ๋“œ๋””์–ด ์ •๋ฆฌํ•˜๊ฒŒ ๋˜๋„ค์š”..!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


UIBezierPath๋ž€?

 

UIBezierPath๋Š” UIKit์—์„œ ๊ทธ๋ฆฌ๊ธฐ(Drawing)์— ์†ํ•ด์žˆ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์–ด๋–ค ๊ฒƒ์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ๋“ค์ด ์†ํ•ด์žˆ๊ฒ ์ฃ ?

 

 

์• ํ”Œ ๊ณต์‹ ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

A path that consists of straight and curved line segments that you can render in your custom views.

 

ํ•ด์„ํ•˜๋ฉด view์—์„œ ๋ Œ๋”๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ์ง์„ ๊ณผ ๊ณก์„ ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ๋กœ๋ผ๊ณ  ํ•˜๋„ค์š”.


Initializer

 

BezierPath๋ฅผ ๋งจ ์ฒ˜์Œ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ฒƒ ์ฆ‰, BezierPath๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฒ•๋„ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ์š”.

 

๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•œ UIView BezierView๋ฅผ ๋งŒ๋“ค๊ณ  BezierViewController์—์„œ ๋ชจ์–‘์„ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

BezierView

 

class BezierView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
	//์ด ๊ณณ์—์„œ ๊ทธ๋ฆฌ๊ธฐ๊ฐ€ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.
    }
}

 

BezierViewController

 

class BezierViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let bezierView:BezierView = BezierView(frame:view.frame)
        bezierView.backgroundColor = .clear
        view.addSubview(bezierView)
    }
}

 

1. ์‚ฌ๊ฐํ˜•

 

๊ฐ€์žฅ ๋จผ์ € ๊ธฐ๋ณธ์ด ๋˜๋Š” ์‚ฌ๊ฐํ˜•์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ด ๊ณณ์—์„œ UIBezierPath(rect:)๋กœ ์‚ฌ๊ฐํ˜• ๊ฒฝ๋กœ๋ฅผ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 override func draw(_ rect: CGRect) {
        let path = UIBezierPath(rect: CGRect(x: bounds.midX-50, y:bounds.midY-50, width: 100, height: 100))
        UIColor.systemRed.setFill()
        UIColor.systemYellow.setStroke()
        path.lineWidth = 10
        path.stroke()
        path.fill()
    }

 

์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด ๊ทธ๋ ค์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

2. ํƒ€์›ํ˜•

 

๊ทธ ๋‹ค์Œ์œผ๋กœ ํƒ€์›ํ˜• ๊ฒฝ๋กœ๋ฅผ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

UIBezierPath(ovalIn:)์œผ๋กœ ํƒ€์›ํ˜• ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

(draw ๋ถ€๋ถ„์˜ ๋‚˜๋จธ์ง€๋Š” ๊ฐ™์œผ๋ฏ€๋กœ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.)

 

let path = UIBezierPath(ovalIn: CGRect(x: bounds.midX-50, y:bounds.midY-50, width: 100, height: 200))

 

์•„๋ž˜์™€ ๊ฐ™์ด ํƒ€์›ํ˜• ๋ชจ์–‘์ด ๊ทธ๋ ค์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

3. ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•

 

๋‹ค์Œ์œผ๋ก  ๋ ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ธ€๊ฒŒ ๋ผ์šด๋”ฉ ์ฒ˜๋ฆฌ๋œ ์‚ฌ๊ฐํ˜•์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

UIBezierPath(roundedRect:,cornerRadius:)์œผ๋กœ Rect์™€ ์–ผ๋งŒํผ ๋‘ฅ๊ธ€๊ฒŒ ํ• ๊ฑด์ง€ cornerRadius๋ฅผ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

  let path = UIBezierPath(roundedRect: CGRect(x: bounds.midX-50, y:bounds.midY-50, width: 100, height: 200),cornerRadius: 20)

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

4. ํŠน์ • ๋ถ€๋ถ„๋งŒ ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•

 

์œ„์—์„œ๋Š” ๋ชจ๋“  ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค์–ด์•ผ ํ–ˆ๋Š”๋ฐ์š”.

 

UIBezierPath(roundedRect:,byRoundingCorners:,cornerRadii:)๋ฅผ ํ†ตํ•ด์„œ ์–ด๋Š ๋ถ€๋ถ„์„ ์–ด๋Š ์ •๋„๋กœ ๋‘ฅ๊ธ€๊ฒŒ ํ•  ๊ฑด์ง€ ์ง€์ •ํ•˜๊ณ  ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

        let path = UIBezierPath(roundedRect: CGRect(x: bounds.midX-50, y:bounds.midY-50, width: 100, height: 200),
                                byRoundingCorners: [UIRectCorner.topRight, UIRectCorner.topLeft],
                                cornerRadii:  CGSize(width: 20, height: 20))

 

 

5. ์›์˜ ํ˜ธ

 

์‹œ์ž‘ ๊ฐ๋„์™€ ๋ ๊ฐ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ์›์˜ ํ˜ธ๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

UIBezierPath(arcCenter:,radius:,startAngle:,endAngle:,clockwise:)๋ฅผ ์ด์šฉํ•˜์—ฌ ์›์˜ ์ค‘์‹ฌ๊ณผ ๋ฐ˜์ง€๋ฆ„์„ ์ •ํ•ด์ฃผ๊ณ  ์‹œ์ž‘ ๊ฐ๋„์™€ ๋ ๊ฐ๋„๋ฅผ ์ •ํ•ด์ฃผ๊ณ  ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ๊ทธ๋ฆด๊ฑด์ง€ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•ด์„œ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

      let path = UIBezierPath(arcCenter:center,
                                radius: 50,
                                startAngle: 0,
                                endAngle: CGFloat.pi,
                                clockwise: true)

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜๋ฐ• ๋ชจ์–‘์ด ๊ทธ๋ ค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 


Constructing a Path(๊ฒฝ๋กœ ๊ตฌ์„ฑํ•˜๊ธฐ)

 

์œ„์™€ ๊ฐ™์ด ์ด๋ฏธ ์ฃผ์–ด์ง„ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ์–‘์„ ๊ทธ๋ฆด ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ง์ ‘ ์›ํ•˜๋Š” ๋ชจ์–‘์„ ํ•˜๋‚˜ ํ•˜๋‚˜ ๊ทธ๋ ค์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํฌ๊ฒŒ๋Š” ์ง์„ ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ณก์„ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. ์‹œ์ž‘ ์ง€์  ์ •ํ•˜๊ธฐ

 

๊ฒฝ๋กœ๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„  ๊ฐ€์žฅ ๋จผ์ € ์–ด๋Š ์ง€์ ์—์„œ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ์š”.

 

move(to:)๋ฅผ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋จผ์ € ๋นˆ UIBezierPath๋ฅผ ์ƒ์„ฑํ•ด์ค€ ๋’ค UIView์˜ ๊ฐ€์šด๋ฐ๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

	let path = UIBezierPath()
	path.move(to: center)

 

2. ์ง์„  ์ถ”๊ฐ€ํ•˜๊ธฐ

 

addLine(to:)์— ๋„์ฐฉ ์ง€์ ์„ ์ •ํ•ด์ฃผ๋ฉด ํŠน์ • ์ง€์ ๋ถ€ํ„ฐ ์›ํ•˜๋Š” ์ง€์ ๊นŒ์ง€ ์ง์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ํŠน์ • ์ง€์ ์€ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๊ตฌ์„ฑ๋œ ๊ฒฝ๋กœ์˜ ๋๋ถ€ํ„ฐ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค.

 

x์ถ•์— 100๋งŒํผ ์ด๋™ํ•œ ์ง์„ ์„ ๊ทธ๋ ค์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

        let path = UIBezierPath()
        path.move(to: center)
        path.addLine(to: CGPoint(x:center.x + 100, y: center.y))


์•„๋ž˜์™€ ๊ฐ™์ด ์ง์„ ์ด ๊ทธ๋ ค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

3. ๊ณก์„  ์ถ”๊ฐ€ํ•˜๊ธฐ(3์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ )

 

addCurve(to:,controlPoint1,controlPoint2:) ์œผ๋กœ 3์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

UIBezierPath ์ด๋ฆ„ ์ž์ฒด๊ฐ€ ๋ฒ ์ง€์–ดํŒจ์Šค์ธ๋งŒํผ ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ํ•ต์‹ฌ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ์š”.

 

* Bezier Curve๋Š” ํ”„๋ž‘์Šค์˜ ์ž๋™์ฐจ ์ œ์กฐ์‚ฌ ๋ฅด๋…ธ์‚ฌ์˜ ๊ธฐ์ˆ ์ž์ธ ํ”ผ์—๋ฅด ๋ฒ ์ง€์—๊ฐ€ 1962๋…„ ์ž๋™์ฐจ ๋ชธ์ฒด ๋””์ž์ธ์— ์‚ฌ์šฉํ•˜๋ฉด์„œ๋ถ€ํ„ฐ ๋„๋ฆฌ ์•Œ๋ ค์กŒ์œผ๋ฉฐ controlPoint๋ฅผ ์ด์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณก์„ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

 

 

์ง์„ ์˜ ๋๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ๋œ S์žํ˜• ๊ณก์„ ์„ ๊ทธ๋ ค์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

        path.move(to: center)
        path.addLine(to: CGPoint(x:center.x + 100, y: center.y))
        path.addCurve(to: CGPoint(x:center.x + 100, y: center.y+100),
                      controlPoint1: CGPoint(x:center.x + 50, y: center.y+25),
                      controlPoint2: CGPoint(x:center.x + 150, y: center.y+75))

 

์•„๋ž˜์™€ ๊ฐ™์ด S์žํ˜• ๊ณก์„ ์ด ๊ทธ๋ ค์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

4. ๊ณก์„  ๊ทธ๋ฆฌ๊ธฐ(2์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ )

 

์œ„์—์„œ ์ปจํŠธ๋กค ํฌ์ธํŠธ๊ฐ€ 2๊ฐœ์ธ 3์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์„ ๊ทธ๋ ค์ฃผ์—ˆ๋‹ค๋ฉด ์ด๋ฒˆ์—” ์ปจํŠธ๋กค ํฌ์ธํŠธ๊ฐ€ ํ•˜๋‚˜์ธ 2์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์„ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

addQuadCurve(to:,controlPoint:)๋กœ ์›ํ•˜๋Š” ๊ณณ์œผ๋กœ ๋‘ฅ๊ธ€๊ฒŒ ๊ณก์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

S์ž ๊ณก์„ ์˜ ์™ผ์ชฝ์œผ๋กœ ์œ„๋กœ ๋‘ฅ๊ทผ ์ฟผ๋“œ ๊ณก์„ ์„ ๊ทธ๋ ค์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

        path.move(to: center)
        path.addLine(to: CGPoint(x:center.x + 100, y: center.y))
        path.addCurve(to: CGPoint(x:center.x + 100, y: center.y+100),
                      controlPoint1: CGPoint(x:center.x + 50, y: center.y+25),
                      controlPoint2: CGPoint(x:center.x + 150, y: center.y+75))
        path.addQuadCurve(to: CGPoint(x:center.x, y: center.y+100),
                          controlPoint: CGPoint(x:center.x + 50, y: center.y+50))

 

์•„๋ž˜์™€ ๊ฐ™์ด ์œ„๋กœ ๋‘ฅ๊ทผ 2์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์ด ๊ทธ๋ ค์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

5. ๋ชจ์–‘ ์™„์„ฑ

 

์›ํ•˜๋Š” ๋ชจ์–‘์„ ๋‹ค ๊ตฌ์„ฑํ•˜์˜€๋‹ค๋ฉด ๋‹ค ๊ทธ๋ ธ๋‹ค๊ณ  ์ฝ”๋“œ๋กœ ์•Œ๋ ค์ค˜์•ผ๊ฒ ์ฃ ?

 

๋ฐ”๋กœ close() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๊ฐ€ ์™„์„ฑ๋˜์—ˆ์Œ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

 

        path.close()

 

6. ํ˜„์žฌ ์œ„์น˜

 

currentPoint ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ทธ๋ ค์ค€ ๋ชจ์–‘์˜ ํ˜„์žฌ ์œ„์น˜๊ฐ€ ์–ด๋””์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

        print(path.currentPoint)//(195.0, 422.0)

Drawing Properties & Paths

 

์œ„์—์„œ ์›ํ•˜๋Š” ๋ชจ์–‘์„ ์–ด๋–ค ์ƒ‰์œผ๋กœ ์ฑ„์šธ์ง€, ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ์–ด๋–ค ์ƒ‰์œผ๋กœ, ์–ผ๋งŒํผ์˜ ๋‘๊ป˜๋กœ ํ• ๊ฑด์ง€๋ฅผ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ด ์™ธ์—๋„ ์„ ์˜ ๋ชจ์–‘, ๋ถ€๋“œ๋Ÿฌ์›€ ์ •๋„ ๋“ฑ์„ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. ์„  ๊ทธ๋ฆฌ๊ธฐ

 

๋จผ์ € ์„ ์„ ๊ทธ๋ฆด์ง€๋ฅผ ๊ฒฐ์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ์š”.

 

stroke ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

        path.stroke()

 

2. ์„ ์˜ ๋‘๊ป˜

 

์„ ์˜ ๋‘๊ป˜๋Š” ์œ„์—์„œ ๋‹ค๋ค˜๋‹ค์‹œํ”ผ lineWidth๋กœ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

        path.lineWidth = 10

 

3. ์„  ์ƒ‰๊น” ์ •ํ•˜๊ธฐ

 

์›ํ•˜๋Š” ์ƒ‰๊น”์— setStroke ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ์ƒ‰์œผ๋กœ ์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

        UIColor.systemYellow.setStroke()

 

์œ„ ์„ธ๊ฐœ ๋งค์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘๊ป˜๊ฐ€ 10์ด๊ณ  ๋…ธ๋ž‘์ƒ‰์ธ ์„ ์ด ๊ทธ๋ ค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

4. ์„ ์˜ ๋ ๋‘ฅ๊ธ€๊ฒŒ ํ•˜๊ธฐ

 

lineCapStyle๋กœ ์„ ์˜ ๋์˜ ๋ชจ์–‘์„ ์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

 

์ด butt,round,square๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ butt,square์€ ๋ณดํ†ต ์„ ์ด ๊ทธ๋ ค์งˆ ๋•Œ์ฒ˜๋Ÿผ ๊ฐ์ง„ ๋ชจ์–‘์œผ๋กœ ํ‘œ์‹œ๋˜๊ฒŒ ๋•Œ๋ฌธ์— ํŒจ์Šคํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

        path.lineCapStyle = .round

 

์•„๋ž˜์™€ ๊ฐ™์ด ์„ ์˜ ๋ ์ ์ด ๋‘ฅ๊ธ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (์ฃผ์˜ํ•  ์ ์€ path.close() ์ „์— ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)

 

 

์œ„์—์„œ ๋งŒ๋“ค์–ด ์ค€ ๋ชจ์–‘์— ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋๊ณผ ์‹œ์ž‘๋งŒ ๋‘ฅ๊ธ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

6. ๋ชจ๋“  ๋ชจ์„œ๋ฆฌ ๋‘ฅ๊ธ€๊ฒŒ ํ•˜๊ธฐ

 

์œ„์—์„œ lineCapStyle์ด ์‹œ์ž‘๊ณผ ๋ ์ ๋งŒ ๋‘ฅ๊ธ€๊ฒŒ ํ•œ๋‹ค๋ฉด lineJointStyle์€ ๋ชจ๋“  ์—ฐ๊ฒฐ๋˜ ๋ถ€๋ถ„์„ ๋‘ฅ๊ธ€๊ฒŒ ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

lineJointStyle์€ ์ด miter,bevel,round 3๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. (miter๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ทธ๋ ค์ง€๋Š” ์„  ์Šคํƒ€์ผ์ž…๋‹ˆ๋‹ค.)

 

 

๋จผ์ € round๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ๋“  ๋ถ€๋ถ„์ด ๋‘ฅ๊ธ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

        path.lineJoinStyle = .round

 

 

bevel์€ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ(?) ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

        path.lineJoinStyle = .bevel

 

7. ์ƒ‰ ์ฑ„์šฐ๊ธฐ

 

๋ชจ์–‘์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ์•ˆ์„ ์›ํ•˜๋Š” ์ƒ‰์œผ๋กœ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

 

๋ฐ”๋กœ fill ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์›ํ•˜๋Š” ์ƒ‰.setFill()๋กœ ์ƒ‰์„ ์ง€์ •ํ•ด์ฃผ๊ณ , fill() ๋ฉ”์„œ๋“œ๋กœ ์ƒ‰์„ ์ฑ„์›Œ์ค๋‹ˆ๋‹ค.

 

        UIColor.systemBlue.setFill()
        path.fill()

 

 

fill(with:,alpha:) ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ธ”๋ Œ๋“œ ๋ชจ๋“œ์™€ ํˆฌ๋ช…๋„๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

        path.fill(with: CGBlendMode.darken, alpha: 0.5)

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ „์ฒด์ ์ธ ์ƒ‰์˜ ํˆฌ๋ช…๋„๋Š” ์˜…์–ด์ง€๊ณ , ๊ฒน์นœ ๋ถ€๋ถ„์€ ์ง„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

8. ๋Œ€์‰ฌ ์Šคํƒ€์ผ ์„ 

 

์„ ์„ ๋Œ€์‰ฌ ์Šคํƒ€์ผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

 

setLineDash(pattern:,count:,phase:) ์œผ๋กœ ๋Œ€์‰ฌ ์Šคํƒ€์ผ์„ ์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

 

๋จผ์ € pattern์€ ์ง์ˆ˜ ๋ฒˆ์งธ๋Š” ๋Œ€์‰ฌ์˜ ๊ธธ์ด, ํ™€ ์ˆ˜๋ฒˆ์งธ๋Š” ์—ฌ๋ฐฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

์ฆ‰ [10,1,5,2] ๋Š” 10๊ธธ์ด์˜ ์„ ๊ณผ 1์˜ ์—ฌ๋ฐฑ 5๊ธธ์ด์˜ ์„ ๊ณผ 2๊ธธ์ด์˜ ์—ฌ๋ฐฑ์ด ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.

 

        path.setLineDash([10,1,5,2], count: 4, phase: 0)

 

 

๊ทธ ๋‹ค์Œ count๋Š” pattern์˜ ์–ด๋””๊นŒ์ง€ ์ ์šฉํ• ๊ฑด์ง€๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ์š”.

 

์œ„์—์„  4๋กœ ์ ์šฉํ–ˆ์œผ๋ฏ€๋กœ 10,1,5,2๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ 2๋กœ ์„ค์ •ํ•˜๋ฉด 10,1๊นŒ์ง€๋งŒ ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

 

        path.setLineDash([10,1,5,2], count: 4, phase: 0)

 

phase๋Š” ์–ด๋Š ์ง€์ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ๊ฑด์ง€๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์œ„ ํŒจํ„ด์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด 10๊ทธ๋ฆฌ๊ณ  1 ๋„๊ณ  5 ๊ทธ๋ฆฌ๊ณ  2 ๋„๊ณ  ... ๋ฐ˜๋ณต์ด๊ฒ ์ฃ ?

 

๋งŒ์•ฝ phase๋ฅผ 4๋กœ ์ง€์ •ํ•œ๋‹ค๋ฉด 4๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ (10-4)6๊ทธ๋ฆฌ๊ณ  1 ๋„๊ณ  5 ๊ทธ๋ฆฌ๊ณ  2๋„๊ณ  10 ๊ทธ๋ฆฌ๊ณ ...๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

phase๋ฅผ 10์œผ๋กœ ์ง€์ •ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด (10-10)0 ๊ทธ๋ฆฌ๊ณ  1๋„๊ณ  5๊ทธ๋ฆฌ๊ณ  2๋„๊ณ  10 ๊ทธ๋ฆฌ๊ณ ... ๊ฐ€ ๋ฐ˜๋ณต๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

        path.setLineDash([10,1,5,2], count: 4, phase: 10)

 

1 ๋„๊ณ  ๋ถ€ํ„ฐ ์‹œ์ž‘์ด๋ฏ€๋กœ ์‹œ์ž‘ ๋ถ€๋ถ„์ด 1 ์—ฌ๋ฐฑ์œผ๋กœ ์‹œ์ž‘๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ค๋Š˜์€ ๊ทธํ† ๋ก ์ •๋ฆฌํ•˜๊ณ  ์‹ถ์—ˆ๋˜ UIBezierPath์— ๋Œ€ํ•ด ์•Œ์•„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์™„๋ฒฝํžˆ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค๋ฃฌ ๊ฑด ์•„๋‹ˆ์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ•œ 80~90%๋Š” ์ •๋ฆฌํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ๋ฟŒ๋“ฏํ•˜๋„ค์š”...ใ…Ž

 

๊ถ๊ธˆํ•˜์‹  ์ ์ด๋‚˜ ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•