Danny의 iOS 컨닝페이퍼
article thumbnail

[TIL #16] 2023 / 04 / 18

버튼에 그림자 효과를 주고 싶어 Layer의 그림자 설정을 사용하고 있는데, 보라색 경고를 마주쳤습니다.

 

일단 코드부터 보시죠.

func configButton() {
    button.layer.cornerRadius = button.bounds.width / 2

    button.layer.shadowColor = UIColor.red.cgColor
    button.layer.shadowOffset = CGSize(width: 0, height: 1)
    button.layer.shadowOpacity = 0.9
    button.layer.shadowRadius = 10
}

 

이와 같이 사용하니, 아래와 보라색 경고가 발생했습니다.

The layer is using dynamic shadows which are expensive to render.

If possible try setting `shadowPath`, or pre-rendering the shadow into an image and putting it under the layer.

 

번역을 돌려보니,

"도면층은 렌더링 비용이 많이 드는 동적 그림자를 사용합니다.

가능한 경우 'shadowPath'를 설정하거나 그림자를 이미지로 미리 렌더링한 후 레이어 아래에 배치합니다."

 

번역을 보니 shadowPath로 그림자 영역을 그려주면 되는 것 같아 보이네요.

 

 

한번 shadowPath를 추가해서 사용해 봅시다.

 

shadowPath를 사용하기 위해, UIBezierPath를 통해 영역을 그려줍시다.

func configButton() {
    button.layer.cornerRadius = button.bounds.width / 2
    
    // 👉 UIBezierPath로 path 생성
    button.layer.shadowPath = UIBezierPath(roundedRect: button.bounds,
                                           cornerRadius: button.layer.cornerRadius).cgPath
    button.layer.shadowColor = UIColor.red.cgColor
    button.layer.shadowOffset = CGSize(width: 0, height: 1)
    button.layer.shadowOpacity = 0.9
    button.layer.shadowRadius = 10
}

 

이렇게 간단히 경고를 해결했습니다.

 

그런데 버튼이 무슨 일식 같아보이네요 ㅎㅎㅎ

 

반응형
profile

Danny의 iOS 컨닝페이퍼

@Danny's iOS

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!