Translation of www.hackingwithswift.com/books/ios-s…

For more content, please follow the public account “Swift Garden”.

Like articles? How about πŸ”ΊπŸ’›βž• company 3? Follow this column, follow me πŸš€πŸš€πŸš€

Design card View

In this project, we wanted to present users with flash cards containing prompts, such as “What is the capital city of Sweden?” . When the user clicks on the card, the answer is revealed, showing “Stockholm”.

For most projects, we start with the data model. For simplicity, we provide only hint strings and answer strings, and create static properties of the sample card to facilitate previewing and prototyping.

Create a card.swift with the following code:

struct Card {
    let prompt: String
    let answer: String

    static var example: Card {
        Card(prompt: "Who played the 13th Doctor in Doctor Who?", answer: "Jodie Whittaker")}}Copy the code

For the SwiftUI view, we’ll use a slightly more complicated approach here: we’ll put two text labels on a white card background and make sure the labels and edges are white. For the label, use a VStack and put in a ZStack with a white RoundedRectangle.

I don’t know if you’re familiar with flashcards, but the typical shape is a chunky rectangle: we write more than two lines of text.

Most apps take device orientation into account, but we can make this app work only in horizontal mode, which gives us more room to draw cards and is more suitable for gestures that will be introduced later.

Create a new SwiftUI view called “CardView” as follows:

struct CardView: View {
    let card: Card

    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 25, style: .continuous)
                .fill(Color.white)

            VStack {
                Text(card.prompt)
                    .font(.largeTitle)
                    .foregroundColor(.black)

                Text(card.answer)
                    .font(.title)
                    .foregroundColor(.gray)
            }
            .padding(20)
            .multilineTextAlignment(.center)
        }
        .frame(width: 450, height: 250)}}Copy the code

Tip: The 450 width setting is no accident: since the smallest iPhone device has 480 dots in horizontal mode, this ensures that the card is fully visible on all devices.

In the meantime, fix a compilation error for CardView_Previews:

struct CardView_Previews: PreviewProvider {
    static var previews: some View {
        CardView(card: Card.example)
    }
}
Copy the code

Looking at the preview, you won’t see that this is a card — it has a white background, but it doesn’t stand out relative to the default background of the view. This can be a problem when we need to stack multiple cards because the white backgrounds will blend together.

A simple solution is to add a shadow to RoundedRectangle for a slight depth effect. This way the card stands out from the white background. As the number of stacked cards increases, the shadow effect becomes more pronounced.

Add modifier after fill(color.white) :

.shadow(radius: 10)
Copy the code

Now, prompts and answers are presented at the same time, which obviously doesn’t help users learn. So, by default, the answer needs to be hidden, but only shown when the card is clicked.

Add a new @state property to CardView:

@State private var isShowingAnswer = false
Copy the code

Then use it to control the presentation of answers:

if isShowingAnswer {
    Text(card.answer)
        .font(.title)
        .foregroundColor(.gray)
}
Copy the code

The final step is to add onTapGesture() modifier to ZStack:

.onTapGesture {
    self.isShowingAnswer.toggle()
}
Copy the code

This completes the code for the card view, now go back to contentView.swift and replace the body property:

var body: some View {
    CardView(card: Card.example)
}
Copy the code

Run the code and see what happens.


My official account here Swift and computer programming related articles, as well as excellent translation of foreign articles, welcome to pay attention to ~