ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [iOS] clipsToBounds 와 masksToBounds
    iOS/πŸ€– App 2021. 10. 19. 23:45

    μ•ˆλ…•ν•˜μ„Έμš”πŸΆ

    이번 μ‹œκ°„μ—λŠ” clipsToBounds μ™€ masksToBounds λ₯Ό μ•Œμ•„λ³΄λ €κ³  ν•©λ‹ˆλ‹€.


     

    UIView 와 CALayer

     

    λ·° 계측과 λ ˆμ΄μ–΄ 계측이 λ™μΌν•œ ꡬ쑰λ₯Ό κ°€μ§€λŠ” 것을 λ³Ό 수 μžˆλ‹€.

    CALayer λž€?

    iOS μ—μ„œ λͺ¨λ“  UIView μΈμŠ€ν„΄μŠ€λŠ” 기본적으둜 ν•˜λ‚˜μ˜ CALayer μΈμŠ€ν„΄μŠ€λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€.

    CALayer λŠ” μ‹œκ°μ μΈ 역할을 λ‹΄λ‹Ήν•˜λŠ” μ˜€λΈŒμ νŠΈμž…λ‹ˆλ‹€. ( μ½˜ν…μΈ  캐싱, μ• λ‹ˆλ©”μ΄μ…˜ μ„±λŠ₯ μ„œν¬νŠΈ λ“±)

    뷰의 λ ˆμ΄μ–΄ μΈμŠ€ν„΄μŠ€λŠ” layer 속성을 ν†΅ν•΄μ„œ μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    λ ˆμ΄μ–΄λŠ” μ‹œκ°μ μΈ 역할에 맞게 λ‹€μ–‘ν•œ κ΄€λ ¨ 속성을 μ œκ³΅ν•˜κ³ , μ»€μŠ€ν…€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    예λ₯Ό λ“€μ–΄ 뷰의 λͺ¨μ„œλ¦¬, 그림자, νšŒμ „κ³Ό 같은 μ• λ‹ˆλ©”μ΄μ…˜μ„ ν‘œν˜„ν•  λ•Œ layer 의 ν•˜μœ„ 속성을 μ‚¬μš©ν•©λ‹ˆλ‹€.

     

     

     

    clipsToBounds λŠ” UIView 의 ν•˜μœ„ 속성 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€.

    masksToBounds 은 CALayer 의 ν•˜μœ„ 속성 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€.

    λ¬Έμ„œμ˜ μ„€λͺ…을 보면 ν•˜λ‚˜λŠ” " 뷰의 Bounds λ₯Ό λ²—μ–΄λ‚œ μ„œλΈŒ λ·°λ₯Ό ν΄λ¦½ν•œλ‹€ " κ³  λ‚˜μ™€μžˆκ³ 

    ν•˜λ‚˜λŠ” " λ ˆμ΄μ–΄μ˜ Bounds λ₯Ό λ²—μ–΄λ‚œ μ„œλΈŒ λ ˆμ΄μ–΄λ₯Ό ν΄λ¦½ν•œλ‹€ " κ³  λ‚˜μ™€μžˆμŠ΅λ‹ˆλ‹€.

    μ„€λͺ…ν•΄μ£ΌλŠ” λ‹¨μ–΄λŠ” λ‹€λ₯Έ 것 같은데, μœ„ 두 속성 μ„€μ •μ˜ 결과물은 μ–΄λ–¨κΉŒμš”?


     

    두 속성 μ„€μ •μ˜ κ²°κ³Όλ¬Ό : κ°™λ‹€

     

    두 속성 쀑 ν•˜λ‚˜λΌλ„ true 둜 μ„€μ •ν•˜λ©΄, view bounds λ₯Ό λ„˜μ–΄κ°„ μ‹œκ°μ  μ½˜ν…μΈ λŠ” μž˜λ €μ„œ 보이지 μ•Šκ²Œλ©λ‹ˆλ‹€.

    κ±±μ •ν•˜μ§€λ§ˆμ„Έμš”! λ‘˜ 쀑에 ν•˜λ‚˜λ§Œ 섀정해도 100% 같은 κ²°κ³Όκ°€ λ‚˜μ˜΅λ‹ˆλ‹€.

    μ΄μœ λŠ” μ•„λž˜ λ‚΄λΆ€λ™μž‘μ— πŸ‘‡πŸ»

     

     


     

    λ‚΄λΆ€ λ™μž‘

     

    두 속성은 항상 같은 값을 κ°€μ§‘λ‹ˆλ‹€.

    즉 view.clipsToBounds 값을 true 둜 μ„€μ •ν•˜λ©΄, view.layer.masksToBounds 값도 true 둜 λ³€κ²½λ©λ‹ˆλ‹€.

    λ°˜λŒ€λ‘œ view.layer.masksToBounds λ₯Ό false 둜 μ„€μ •ν•˜λ©΄, view.clipsToBounds 값도 false둜 λ³€κ²½λ©λ‹ˆλ‹€.

     

    캑처둜 남겨 놓긴 ν•˜μ˜€μ§€λ§Œ, 직접 브레이크 포인트λ₯Ό μ°μ–΄μ„œ ν™•μΈν•΄λ³΄μ…¨μœΌλ©΄ μ’‹κ² μŠ΅λ‹ˆλ‹€.


     

    layer 속성은 λŒ€μ²΄ λΆˆκ°€ν•  λ•Œλ§Œ μ‚¬μš©ν•˜μž

     

    UIView 의 μ†μ„±μœΌλ‘œ λ³€κ²½ν• μˆ˜ μ—†λŠ” κ²½μš°μ—λ§Œ layer μ†μ„±μœΌλ‘œ λ³€κ²½ν•˜λŠ” 것이 μ˜³μŠ΅λ‹ˆλ‹€.

    λ‹€μŒμ€ Core Animation λ¬Έμ„œμ˜ 일뢀 μž…λ‹ˆλ‹€.

    가급적  View 속성 λŒ€μ‹  Layer 속성을 λ³€κ²½ν•˜λŠ” 것을 ν”Όν•˜λΌκ³  μΆ”μ²œν•˜κ³  있으며,

    κ·Έλ ‡κ²Œ ν•˜μ§€ μ•Šμ„ 경우 μ˜λ„ν•˜μ§€ μ•ŠλŠ” κ²°κ³Όκ°€ λ‚˜μ˜¬μˆ˜ μžˆλ‹€κ³  λ§ν•΄μ€λ‹ˆλ‹€.

     

     For layer-backed views, it is recommended that you manipulate the view, rather than its layer, whenever possible. In iOS, views are just a thin wrapper around layer objects, so any manipulations you make to the layer usually work just fine. But there are cases in both iOS and OS X where manipulating the layer instead of the view might not yield the desired results. Wherever possible, this document points out those pitfalls and tries to provide ways to help you work around them.

     

     


    μ˜€λŠ˜λ„ λ‚˜λŠ” μ„±μž₯ν–ˆλ‹€!!πŸ”₯

     

    πŸ€–πŸŸ’[우짱의 iOS λΈ”λ‘œκ·Έ]πŸ”΅πŸ’»

    iOSλ₯Ό κ³΅λΆ€ν•˜λ©΄μ„œ 배운 λ‚΄μš©μ„ κΈ°λ‘ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

     

    [참고 링크]

    https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514-CH1-SW1

    https://stackoverflow.com/questions/39466001/maskstobounds-vs-clipstobounds

    λŒ“κΈ€

μ–΄μ œλ³΄λ‹€ λ°œμ „ν•œ λ‚˜