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

[Swift] insetBy ์•Œ์•„๋ณด๊ธฐ

by ํ‹ด๋”” 2022. 4. 23.
๋ฐ˜์‘ํ˜•

insetBy(dx:dy)

  • Core Graphic์˜ CGRect์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ
  • offset์ด point๋ผ๋ฉด inset์€ ์‚ฌ์ด์ฆˆ๊ฐ€ ์žˆ๋Š” ๊ณต๊ฐ„. ์•ˆ์ชฝ์œผ๋กœ ๋งˆ์ง„์„ ์คŒ
  • ์ค‘์•™์ ์„ ๊ธฐ์ค€์œผ๋กœ dx, dy ๊ฐ’์„ ํ†ตํ•ด ์กฐ์ •๋œ, ์›๋ณธ ์ง์‚ฌ๊ฐํ˜• ๋ณด๋‹ค ๋” ํฌ๊ฑฐ๋‚˜ ๋” ์ž‘์€ ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜

Parameters

dx

  • x ์ขŒํ‘œ ๊ฐ’
  • ์›๋ณธ ์ง์‚ฌ๊ฐํ˜• ๋ณด๋‹ค ํฌ๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์Œ์ˆ˜๋ฅผ, ๋” ์ž‘๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์–‘์ˆ˜๋ฅผ ์ง€์ •ํ•จ

dy

  • y ์ขŒํ‘œ ๊ฐ’
  • ์›๋ณธ ์ง์‚ฌ๊ฐํ˜• ๋ณด๋‹ค ํฌ๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์Œ์ˆ˜๋ฅผ, ๋” ์ž‘๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์–‘์ˆ˜๋ฅผ ์ง€์ •ํ•จ

์˜ˆ์‹œ

newView.bounds = originalView.bounds.insetBy(dx: 20, dy: 0)

inset by ์ „ : (0.0, 0.0, 100.0, 40.0)
inset by ํ›„ : (20.0, 0.0, 60.0, 40.0)
  • 100์ด์—ˆ๋˜ ์‚ฌ๊ฐํ˜•์— dx๋ฅผ 20์œผ๋กœ ์ฃผ๋ฉด ์–‘์ชฝ์œผ๋กœ 20 ์”ฉ ์ค„์–ด๋“  60 ๋ฐ˜ํ™˜ 
  • ์–‘์ˆ˜๋Š” ์›๋ณธ ์‚ฌ๊ฐํ˜•๋ณด๋‹ค ๋” ์ž‘์€ ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜ ๋ฐ›์Œ

newView.bounds = originalView.bounds.insetBy(dx: -10, dy: 0)

inset by ์ „ : (0.0, 0.0, 100.0, 40.0)
inset by ํ›„ : (-10.0, 0.0, 120.0, 40.0)
  • 100 ์ด์—ˆ๋˜ ์‚ฌ๊ฐํ˜•์— dx์˜ ๊ฐ’์„ -10 ์ฃผ๋ฉด ์–‘์ชฝ ๊ฐ 10 ์”ฉ ๋Š˜์–ด๋‚œ 120 ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜
  • ์Œ์ˆ˜๋Š” ์›๋ณธ ์‚ฌ๊ฐํ˜• ๋ณด๋‹ค ๋” ํฐ ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜ํ•จ
  • ์ง€์ •ํ•œ ๊ฐ’์œผ๋กœ ์–‘ ์ชฝ์ด ๋Š˜์–ด๋‚˜๊ฑฐ๋‚˜ ์ค„์–ด๋“ฌ 

 

  • new width = (-dx * 2) + original width

 

newView.bounds = originalView.bounds.insetBy(dx: 0, dy: -20)

inset by ์ „ : (0.0, 0.0, 100.0, 40.0)
inset by ํ›„ : (0.0, -20.0, 100.0, 80.0)
  • dy ๋˜ํ•œ dx์™€ ๋™์ผํ•˜๊ฒŒ ์Œ์ˆ˜์˜ ๊ฐ’์€ ์›๋ณธ๋ณด๋‹ค ๋” ํฐ ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜ํ•จ. 
  • -20 ์ด๋ฏ€๋กœ ์œ„ ์•„๋ž˜ ๋™์ผํ•˜๊ฒŒ 20 ์”ฉ ์ปค์ ธ ์›๋ณธ ๋†’์ด 40 ์—์„œ 40 ์ปค์ง„ 80์ด ๋ฐ˜ํ™˜๋จ
  • bounds ๋˜ํ•œ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Œ

 

  • new height = (-dy * 2) + original height

 

newView.bounds = originalView.bounds.insetBy(dx: -20, dy: -20)

inset by ์ „ : (0.0, 0.0, 100.0, 40.0)
inset by ํ›„ : (-20.0, -20.0, 140.0, 80.0)
  • ์œ„ ์•„๋ž˜ ์˜ค๋ฅธ์ชฝ ์™ผ์ชฝ์ด 20 ์”ฉ ์ฆ๊ฐ€ํ•˜์—ฌ ์›๋ณธ ์‚ฌ๊ฐํ˜• ๋ณด๋‹ค ๋” ํฐ ์‚ฌ๊ฐํ˜•์„ ๋ฐ˜ํ™˜ํ•จ

์ฐธ๊ณ  ์‚ฌ์ดํŠธ ๋ฐ ๋„์„œ

 

Apple Developer Documentation

 

developer.apple.com

 

- ๋ฌธ์ œ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ์ด์ƒํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๋‚ด์šฉ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์˜ฎ๊ธฐ์‹ค ๋•Œ ๊ผญ ์ถœ์ฒ˜ ๋‚จ๊ฒจ ์ฃผ์„ธ์š”

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€