ViewModifier와 self의 차이점 #61
limhyoseok
started this conversation in
Blog
Replies: 1 comment 1 reply
-
좋은 discussion 감사합니다 !! 십년 묵은 체증이 내려가는 듯하군요 !!!! 이번에는 modifier만을 위해 구현을 했던 거라 사용되지는 않겠지만, +) 추가적으로 property wrapper는 아니지만 View를 extension해서 쓰는 방법도 변수를 가질 수 있어요 !
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
지난번에 river가 ViewModifier에 대해 공유해줬는데
self에 modifier 붙인 것과 뭐가 다르냐라는 의견이 있었습니다.
아래 두 코드는 결과가 동일한데 self가 더 간결하기도 해서 ViewModifier의 이점을 알 수가 없었습니다.
하지만 찾아본 결과...
self와 다른 점은 ViewModifier는 struct로 변수를
가질 수 있다는 점입니다.저장할 수 있다는 점입니다만약 클릭할 때마다 text의 색깔이 toggle되는 modifier를 만들라고 하면 viewModifier은 변수를 가질 수 있어 쉽게 구현이 가능합니다.
(self로는 외부로부터 변수로 끌어오거나 하는 등 내부에서 구현이 어려울 것 같은데 방법이 있다면 댓글로 남겨주세요!)
var tapped 라는 변수에 이전 값을 저장해 보관할 수 있고, @State같은 property wrapper도 사용 가능해집니다.
![image](https://user-images.githubusercontent.com/59302419/179505337-213f71dc-15ae-4733-9254-41438dc0b442.png)
또한 viewModifier는 변수를 가질 수 있다는 특징으로 애니메이션 구현에 많이 사용된다고 합니다.
참고
리버 노션
Difference between creating ViewModifier and View extension in SwiftUI
Beta Was this translation helpful? Give feedback.
All reactions