[SwiftUI] Drawing Paths and Shapes
์๋ฌธ: ํํ ๋ฆฌ์ผ ํ์ด์ง
Drawing Paths and Shapes
combining path์ shapes๋ฅผ ๊ฐ์ง๊ณ ๋ง๋ค์ด๋ณด๋ badge. ๊ทผ๋ฐ ๋ณดํต ์์ด์ฝ์โฆ ๋์์ด๋๋ถ๊ป ๋ฐ์์ ์ฐ์ง ์๋์โฆ? ์๋๊ฐ
Create Drawing Data for a Badge View
์ฐ์ ์ ์ก๊ฐํ์ ๋ฐฐ์ง๋ฅผ ๋ง๋ค์ด๋ณด์. ํํ ๋ฆฌ์ผ ํ์ด์ง์ ๋์จ ์ด๋ฏธ์ง๋ ๋ค์๊ณผ ๊ฐ๋ค.
- HexagonParameters.swift ์์ฑ (Swift file)
- ์ก๊ฐํ ๋ชจ์์ ์ ์ํ ์ ์๋ structure ๊ด๋ฆฌ๋ฅผ ์ํด HexagonParameters.swift ํ์ผ์ ์์ฑํ๋ค.
- HexagonParameters ๊ตฌ์กฐ์ฒด ๋ด๋ถ์ ์ก๊ฐํ badge ๋ชจ์์ ์ํด ํ์ํ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
struct HexagonParameters {
struct Segment {
let line: CGPoint
let curve: CGPoint
let control: CGPoint
}
static let adjustment: CGFloat = 0.085
static let segments = [
Segment(
line: CGPoint(x: 0.60, y: 0.05),
curve: CGPoint(x: 0.40, y: 0.05),
control: CGPoint(x: 0.50, y: 0.00)
),
Segment(
line: CGPoint(x: 0.05, y: 0.20 + adjustment),
curve: CGPoint(x: 0.00, y: 0.30 + adjustment),
control: CGPoint(x: 0.00, y: 0.25 + adjustment)
),
Segment(
line: CGPoint(x: 0.00, y: 0.70 - adjustment),
curve: CGPoint(x: 0.05, y: 0.80 - adjustment),
control: CGPoint(x: 0.00, y: 0.75 - adjustment)
),
Segment(
line: CGPoint(x: 0.40, y: 0.95),
curve: CGPoint(x: 0.60, y: 0.95),
control: CGPoint(x: 0.50, y: 1.00)
),
Segment(
line: CGPoint(x: 0.95, y: 0.80 - adjustment),
curve: CGPoint(x: 1.00, y: 0.70 - adjustment),
control: CGPoint(x: 1.00, y: 0.75 - adjustment)
),
Segment(
line: CGPoint(x: 1.00, y: 0.30 + adjustment),
curve: CGPoint(x: 0.95, y: 0.20 + adjustment),
control: CGPoint(x: 1.00, y: 0.25 + adjustment)
)
]
}
CGPoint๋ Foundation์ ํฌํจ๋์ง ์๊ธฐ ๋๋ฌธ์
CoreGraphics
๋ฅผ ์ถ๊ฐํด์ค๋ค.
Draw the Badge Background
SwiftUI์ graphics API๋ฅผ ์ฌ์ฉํด custom badge ๋ชจ์์ ๊ทธ๋ ค๋ณธ๋ค.
- BadgeBackground.swift ์์ฑ (SwiftUI View)
- body์์ haxagonParameters์ Path ๊ฐ์ ์ถ๋ ฅโฆ. ํ์ง๋ง ์ดํดํ ์ ์์๋คโฆ
struct BadgeBackground: View {
var body: some View {
GeometryReader { geometry in
Path { path in
var width: CGFloat = min(geometry.size.width, geometry.size.height)
let height = width
let xScale: CGFloat = 0.832
let xOffset = (width * (1.0 - xScale)) / 2.0
width *= xScale
path.move(
to: CGPoint(
x: width * 0.95 + xOffset,
y: height * (0.20 + HexagonParameters.adjustment)
)
)
HexagonParameters.segments.forEach { segment in
path.addLine(
to: CGPoint(
x: width * segment.line.x + xOffset,
y: height * segment.line.y
)
)
path.addQuadCurve(
to: CGPoint(
x: width * segment.curve.x + xOffset,
y: height * segment.curve.y
),
control: CGPoint(
x: width * segment.control.x + xOffset,
y: height * segment.control.y
)
)
}
}
.fill(.linearGradient(
Gradient(colors: [Self.gradientStart, Self.gradientEnd]),
startPoint: UnitPoint(x: 0.5, y: 0),
endPoint: UnitPoint(x: 0.5, y: 0.6)
))
}
.aspectRatio(1, contentMode: .fit)
}
static let gradientStart = Color(red: 239.0 / 255, green: 120.0 / 255, blue: 221.0 / 255)
static let gradientEnd = Color(red: 239.0 / 255, green: 172.0 / 255, blue: 120.0 / 255)
}
Draw the Badge Symbol
ํํ ๋ฆฌ์ผ ํ์ด์ง์ ํ๋ก์ ํธ ํ์ผ์์ AppIcon์ ๊ฐ์ ธ์๋ค. ์ด์ ์ด๊ฑธ๋ก badge symbol์ ๋ง๋ ๋ค.
-
BadgeSymbol ํ์ผ ์์ฑ (SwiftUI View)
-
RotatedBadgeSymbol ํ์ผ ์์ฑ (SwiftUI View)
Combine the Badge Foreground and Background
- Badge ํ์ผ ์์ฑ (SwiftUI View)
RotatedSymbol์ 8๊ฐ ๋๋ ค์ ๋งํฌ๋ฅผ ๋ง๋ค๊ณ , badgeBackground ์์ ์น์ ์ ์๋๋ก ZStack์ผ๋ก ๊ฒน์น๊ธฐ
๋ง๋ฌด๋ฆฌ
์ฌ์ค ์ด ์ฑํฐ๋ ๋ค์ ์ฅ์ ์ํด ๋์ถฉ ์ ๋ฆฌํ๋ ๊ฒโฆ ํ์ง๋ง ์ดํด๋ ํ๊ณ ์ถ์๋ฐ ์ธ์ ์ฏค ๋ ๊น..? ์ซ์๋ง ๋์ค๋ฉด ๋จธ๋ฆฌ์ ์ฅ๊ฐ ๋๋ค.. ๐ญ
Leave a comment