
기본 코드
struct Book {
let title: String
let author: String
}
struct bookReading: View {
let book = Book(title: "Apple", author: "Happy virus")
@State var isReading = false
var body: some View {
VStack {
Text(self.book.title)
.font(.title)
.foregroundColor(self.isReading ? .blue: .white)
Text(self.book.author)
.font(.footnote)
.foregroundColor(.secondary)
readingButton(isReading: $isReading)
}
.padding(isReading ? 100 : 30)
.background(self.isReading ? Color.yellow : Color.blue)
.cornerRadius(self.isReading ? 60 : 10)
}
}
struct readingButton: View {
@Binding var isReading: Bool
var body: some View {
Button(action: {
withAnimation {
self.isReading.toggle()
}
}) {
if(self.isReading) {
Image(systemName: "pause.fill")
.foregroundColor(Color.blue)
} else {
Image(systemName: "play.fill")
.foregroundColor(Color.black)
}
}
.font(.system(size: 30))
.padding(12)
}
}
let read = bookReading()
struct ContentView: View {
var body: some View {
VStack {
Spacer()
bookReading()
.padding(40)
Spacer()
}
.frame(maxWidth: .infinity)
.background(Color.yellow)
.edgesIgnoringSafeArea(.all)
}
}
실행 화면


'Language > Swift' 카테고리의 다른 글
SwiftUI 에서 Binding이란 무엇인가? (0) | 2023.01.09 |
---|---|
SwiftUI 에서 데이터 다루기 (@State, @ObservedObject) (0) | 2023.01.05 |
SwiftUI 에서 Typealias으로 타입 별칭 정하기 (0) | 2023.01.04 |
SwiftUI에서 Markup과 Documentation을 추가하는 방법 (0) | 2023.01.03 |
SwiftUI에서 sheet 와 FullScreenCover 이용하기 (0) | 2023.01.02 |
댓글