๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UI

[iOS/UI] UIBezierPath ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ (feat. EasierPath)

by Fomagran ๐Ÿ’ป 2022. 5. 30.
728x90
๋ฐ˜์‘ํ˜•

 

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

 

์˜ค๋Š˜์€ ์ง์ ‘ ์›ํ•˜๋Š” ๋ชจ์–‘์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” UIBezierPath๋ฅผ ๋”์šฑ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” EasierPath์— ๋Œ€ํ•ด์„œ ์•Œ์•„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

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


UIBezierPath

 

ํ‰์†Œ์— ์›ํ•˜๋Š” ๋ชจ์–‘์„ ์ง์ ‘ ์ปค์Šคํ…€ํ•ด์„œ ๊ทธ๋ฆด ๋•Œ UIBezierPath๋ฅผ ์ž์ฃผ ์‚ฌ์šฉ ํ–ˆ์—ˆ๋Š”๋ฐ์š”.

 

๋จผ์ € UIBezierPath๋ฅผ ์ด์šฉํ•ด์„œ ํ•ญ์•„๋ฆฌ ๋ชจ์–‘์„ ๊ทธ๋ฆฌ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

1. UIBezierPath ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ๊ทธ๋ ค์ค€๋‹ค.

 

        let bezierPath:UIBezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: 100, y: 100))
        bezierPath.addLine(to: CGPoint(x: 200, y: 100))
        bezierPath.addCurve(to: CGPoint(x: 200, y: 300), controlPoint1: CGPoint(x: 250, y: 150), controlPoint2: CGPoint(x: 175, y: 250))
        bezierPath.addLine(to: CGPoint(x:100,y:300))
        bezierPath.addCurve(to: CGPoint(x: 100, y: 100), controlPoint1: CGPoint(x: 125, y: 250), controlPoint2: CGPoint(x: 50, y: 150))
        bezierPath.close()

 

2. CAShapeLayer์˜ path๋ฅผ UIBezierPath.cgPath๋กœ ์ง€์ •ํ•˜๊ณ , ์›ํ•˜๋Š” ์Šคํƒ€์ผ์„ ์ง€์ •ํ•œ๋‹ค.

 

        let caShapeLayer:CAShapeLayer = CAShapeLayer()
        caShapeLayer.path = bezierPath.cgPath
        caShapeLayer.strokeColor = UIColor.white.cgColor
        caShapeLayer.fillColor = UIColor.systemPink.cgColor
        caShapeLayer.lineWidth = 3

 

3. view์˜ subLayer์— ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

 

        view.layer.addSublayer(caShapeLayer)

UIBezierPath์˜ ๋‹จ์ 

 

์œ„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ UIBezierPath๋Š” ๋ช‡ ๊ฐ€์ง€ ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

1. ์„ ์„ ์ถ”๊ฐ€ํ•  ๋•Œ์˜ ๋ ์ง€์ ์„ ๋จธ๋ฆฟ ์†์œผ๋กœ ๊ธฐ์–ตํ•ด์•ผ ํ•œ๋‹ค.

 

์„ ์ด๋‚˜ ๊ณก์„ ์„ ๊ทธ๋ฆด ๋•Œ ์ „์— ์–ด๋Š ์ง€์ ์—์„œ ๋๋‚ฌ๋Š”์ง€๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ๊ทธ์— ๋งž๊ฒŒ ์ง€์ ์„ ์ •ํ•ด ์ค˜์•ผ ํ–ˆ๋Š”๋ฐ์š”.

 

์ด๊ฒƒ์„ ๊ทธ๋ฆด ์–‘์ด ๋งŽ์œผ๋ฉด ๋งŽ์„ ์ˆ˜๋ก ๊ธฐ์–ต ํ•ด์•ผํ•  ์–‘์ด ๋งŽ์•„์ง€๊ณ , ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํ™•์ธ ํ•˜๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ๋ถˆ๋ถ„๋ช… ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

2. ์‹œ์ž‘ ์ง€์ ์ด ๋‹ฌ๋ผ์ง€๋ฉด ๋ชจ๋“  ์ง€์ ์„ ์ˆ˜์ •ํ•ด ์ค˜์•ผ ํ•œ๋‹ค.

 

๊ฐ ์„ ์ด๋‚˜, ๊ณก์„ ์„ ๊ทธ๋ฆฌ๋Š” ์ง€์ ์€ ์‹œ์ž‘ ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ ์‹œ์ž‘ ์ง€์ ์ด ๋‹ฌ๋ผ์ง„๋‹ค๋ฉด ๋ชจ๋“  ์„ ์˜ ์ง€์ ์„ ๋ณ€๊ฒฝํ•ด ์ค˜์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„  ํŠน์ • ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ’์„ ๋‹ด์€ ๋‹ค์Œ์— ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋˜์—ˆ์ง€๋งŒ ์ด๊ฒƒ ๋˜ํ•œ ๋ฒˆ๊ฑฐ๋กœ์šด ์ผ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

 

3. CAShapeLayer๋‚˜ UIView๋ฅผ ์ƒ์„ฑํ•ด ์ค˜์•ผ ํ•œ๋‹ค.

 

View์— UIBezierPath๋ฅผ ๋„์šฐ๊ธฐ ์œ„ํ•ด์„  CAShapeLayer๋ฅผ ์ƒ์„ฑํ•ด ๋งŒ๋“  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด ์ฃผ๊ฑฐ๋‚˜,

 

UIView๋ฅผ ์ƒ์„ฑํ•˜์—ฌ draw๋ฉ”์„œ๋“œ์— ์ง์ ‘ ๊ทธ๋ ค์ค˜์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.


EasierPath

 

EasierPath๋Š” ์œ„ UIBezierPath์˜ ๋‹จ์ ๋“ค์„ ๋ณด์™„ํ•˜๊ณ  ๋”์šฑ ์ง๊ด€์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„๋ก ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.

 

(์‚ฌ์šฉํ•ด ๋ณด์‹œ๊ณ  Starโญ๏ธ  ํ•œ๋ฒˆ์”ฉ๋งŒ ๋ถ€ํƒ ๋“œ๋ฆฝ๋‹ˆ๋‹ค ใ…Žใ…Ž)

 

 

GitHub - UISugar/EasierPath: To make it easier to use UIBezierPath โšก๏ธ

To make it easier to use UIBezierPath โšก๏ธ. Contribute to UISugar/EasierPath development by creating an account on GitHub.

github.com

 

์œ„์—์„œ ๊ทธ๋ฆฐ ํ•ญ์•„๋ฆฌ ๋ชจ์–‘์„ EasierPath๋ฅผ ์‚ฌ์šฉํ•ด ๊ทธ๋ ค ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

1. EasierPath ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ๊ทธ๋ ค์ค€๋‹ค.

 

 let easierPath = EasierPath(100,100)
            .right(100)
            .curve(to: .down(200), .bezier(.rightDown(50,50), .leftDown(25,150)))
            .left(100)
            .curve(to:.up(200), .bezier(.rightUp(25,50), .leftUp(50,150)))

 

2. ์›ํ•˜๋Š” ์Šคํƒ€์ผ์„ Layer๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

    let layer = easierPath.makeLayer(lineWidth:3,lineColor: .white,fillColor:.systemPink)

 

3. view์˜ subLayer์— ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

 

        view.layer.addSublayer(layer)

UIBezierPath ๋‹จ์  ๋ณด์™„

 

1. ์„ ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๋ ์ง€์ ์„ ๊ธฐ์–ตํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

์œ„ EasierPath๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ „์— ์–ด๋Š ์ง€์ ์—์„œ ๋๋‚ฌ๋Š”์ง€๋ฅผ ๊ตณ์ด ๊ธฐ์–ตํ•˜์ง€ ์•Š๊ณ , ์–ด๋Š ๋ฐฉํ–ฅ์œผ๋กœ ์„ ์„ ๊ทธ๋ฆด ๊ฒƒ์ธ์ง€๋งŒ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

2. ์‹œ์ž‘ ์ง€์ ์ด ๋‹ฌ๋ผ ์ง€๋”๋ผ๋„ ์ฝ”๋“œ๋Š” ๊ทธ๋Œ€๋กœ๋‹ค.

 

์‹œ์ž‘ ์ง€์ ์ด ๋‹ฌ๋ผ์ง€๋”๋ผ๋„ ํ•ด๋‹น ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ทธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์ง€์ ์„ ๋ณ€๊ฒฝํ•ด ์ค„ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

 

3. makeLayer ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด Layer๋ฅผ ๋งŒ๋“ ๋‹ค.

 

๊ตณ์ด CAShapeLayer๋‚˜ UIView๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ์ง€ ์•Š์•„๋„ EasierPath ๋‚ด์˜ makeLayer ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด style์„ ์ง€์ •ํ•ด ์ฃผ๋ฉด layer๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ์˜ค๋Š˜์€ UIBezierPath์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•ด ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ œ๊ฐ€ ์ง์ ‘ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ EasierPath์— ๋Œ€ํ•ด ๊ธ€์„ ์ •๋ฆฌํ•ด ๋ณด์•˜๋Š”๋ฐ์š”.

 

EasierPath๋ฅผ ๋”์šฑ ์‰ฝ๊ณ , ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋˜๋Š” ๊ณ ์น  ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“  Issue์™€ Pull Request๋Š” ํ™˜์˜์ž…๋‹ˆ๋‹ค!!

 

๋˜ํ•œ UI๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ณ , ํšจ์œจ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€๋Š” UISugarโšก๏ธ Organization์„ ๋งŒ๋“ค๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ์š”.

 

ํ•จ๊ป˜ ํ•˜๊ณ  ์‹ถ์€ ๋ถ„์ด ๊ณ„์‹œ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋˜๋Š” pull request๋ฅผ ๋‚ ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€