일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Concurrency
- withAnimation
- combine
- ios
- Animation
- authentication
- stateobject
- SwiftUI
- 네트워크
- GCD
- dataflow
- 접근성제어
- gesture
- firebase
- auth
- RxSwift
- 최적화
- view
- arkit
- WWDC
- iphone
- Performance
- CS
- swift
- avsession
- Network
- toolbarvisibility
- 달력
- state
- UIKit
Archives
- Today
- Total
XLOG
[SwiftUI] Button 의 highlighted 감지하기 본문
기존 UIKit의 UIButton 의 경우 setTitleColor 를 통해 highlighted에 따라 색상을 변경할 수 있었다. 하지만 SwiftUI 에선 자동으로 Button에서 처리가 되기도 하지만 Custom Label의 경우는 그것이 쉽지 않다.
방법은 간단하다. ButtonStyle 을 구현하는 것이다.
ButtonStyle 은 Protocol 로 Makebody 함수를 통해서 View 를 return 한다.
또한 makebody에 configuration 에는 label, isPressed 가 있다. 여기서 isPressed 가 바로 highlighted 와 비슷하다고 보면 된다.
구현은 간단하다.
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundStyle(configuration.isPressed ? .red : .blue)
.scaleEffect(configuration.isPressed ? 0.8 : 1)
}
}
Button(action: {
print("Something")
}, label: {
RoundedRectangle(cornerRadius: 8)
})
.buttonStyle(CustomButtonStyle())
.frame(width: 200, height: 50)
'Swift > SwiftUI' 카테고리의 다른 글
[SwiftUI] 밀리의 서재 책정보 Sheet 애니메이션 아이디어 및 구현 (0) | 2024.06.24 |
---|---|
[SwiftUI] View 의 Size 구하기 (0) | 2024.05.19 |
[SwiftUI] Infinity Carousel 구현 (0) | 2023.12.01 |
[SwiftUI] Camera Shutter Button Animation (0) | 2023.09.18 |
[SwiftUI] Animation 적용 기본 이론편 (0) | 2023.08.07 |