-
[SwiftUI] clipShapeSwiftUI 2023. 8. 7. 10:56
๋ทฐ์ ๋ํ ํด๋ฆฌํ ๋ชจ์์ ์ค์ ํ๋ค.
func clipShape<S>( _ shape: S, style: FillStyle = FillStyle() ) -> some View where S : Shapeshape ํ๋ผ๋ฏธํฐ๋ ๋ทฐ์ ์ฌ์ฉํ ํด๋ฆฌํ ๋ชจ์์ผ๋ก, ๊ฐ๋ก ์ธ๋ก ๋น์จ์ ์ ์งํ๋ฉด์ ๋ทฐ์ ํ๋ ์์ ์ฑ์ด๋ค. shape ํ๋ผ๋ฏธํฐ๋ Shape ํ๋กํ ์ฝ์ ์ฑํํ๋ค.
style ํ๋ผ๋ฏธํฐ๋ ๋ชจ์์ ๋์คํฐํํ ๋ ์ฌ์ฉํ fill ์คํ์ผ๋ก, ๊ธฐ๋ณธ๊ฐ์ด ์กด์ฌํ๋ค.
์ฐธ๊ณ ๋ก FillStyle์ ๋ฒกํฐ ๋ํ์ ๋์คํฐํํ๊ธฐ ์ํ ์คํ์ผ๋ก, ๊ตฌ์กฐ์ฒด์ด๋ค. ์ฌ๊ธฐ์ ๋์คํฐํ๋, ๊ทธ๋ํฝ์ ๋ ๋๋งํ๊ธฐ ์ํ ๋ฐฉ์ ์ค ํ๋๋ก, ํ๋ฉด์ ์ค๋ธ์ ํธ๋ฅผ ํฝ์ ๋ก ๋งคํํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
style์ ์ฌ์ฉํด์ ๋ํ์ ๋์คํฐํ๋ฅผ ์ ์ํ๊ณ ๋ทฐ์ shape๋ฅผ ํด๋ฆฌํํ๋ค.
์ ๊ณต๋ shape๋ก clipShape(_:style:)์ ์ฌ์ฉํ์ฌ ๋ทฐ๋ฅผ ํด๋ฆฌํํ๋ค. ๋ทฐ์ ํด๋ฆฌํ shape๋ฅผ ์ ์ฉํ๋ฉด, ๋ทฐ์ ๋ค๋ฅธ ๋ถ๋ถ์ ์ ๊ฑฐํ๋ฉด์ shape๋ก ๋ฎ์ธ ๋ทฐ์ ์ผ๋ถ๋ ๋ณด์กดํ ์ ์๋ค. ํด๋ฆฌํ shape ์์ฒด๋ ํ์๋์ง ์๋๋ค.
์๋ฅผ ๋ค์ด ๋ค์ ์ฝ๋๋ Text ๋ทฐ์ ์ํ ํด๋ฆฌํ shape๋ฅผ ์ ์ฉํ๋ค.
Text("Clipped text in a circle") .frame(width: 175, height: 100) .foregroundColor(Color.white) .background(Color.black) .clipShape(Circle())๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ ๋ฒ์ ๋ด์ ์๋ ํ ์คํธ ๋ถ๋ถ๋ง ํ์๋๋ค.

shape ํ๋ผ๋ฏธํฐ์ ๋ค์ด๊ฐ๋ ๋ทฐ๋ค์ ๋ชจ๋ Shape ํ๋กํ ์ฝ์ ์ฑํํ๊ณ ์๋ค.
var body: some View { VStack(spacing: 20) { Color.pink .frame(width: 200, height: 50) Color.pink .frame(width: 200, height: 50) .clipShape(Circle()) Color.pink .frame(width: 200, height: 50) .clipShape(Ellipse()) Color.pink .frame(width: 200, height: 50) .clipShape(Capsule()) } }
FillStyle
FillStyle์ ๋ฒกํฐ ๋ํ์ ๋์คํฐํํ๊ธฐ ์ํ ์คํ์ผ๋ก, ๊ตฌ์กฐ์ฒด์ด๋ค. ์ง์ ๋ ์ค์ ์ผ๋ก ์๋ก์ด fill style์ ์์ฑํ๋ค.
์ฌ๊ธฐ์ ๋์คํฐํ๋, ๊ทธ๋ํฝ์ ๋ ๋๋งํ๊ธฐ ์ํ ๋ฐฉ์ ์ค ํ๋๋ก, ํ๋ฉด์ ์ค๋ธ์ ํธ๋ฅผ ํฝ์ ๋ก ๋งคํํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
clipShape(_:style:) | Apple Developer Documentation
Sets a clipping shape for this view.
developer.apple.com